Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate if a environment variable is set in vimrc

Tags:

function

vim

How to evaluate if an bash environment variable is set

for example

function! Myfoo(arg)
  if $SomeVar is set/exist ----> how to eval the SomeVar has been set
     ....
  endif
endfunction
like image 258
SLN Avatar asked Feb 26 '19 13:02

SLN


People also ask

How do I check if an environment variable is set?

In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

How do I set environment variables in Vimrc?

Vim can read and write environment variables within your current shell session. Use a $ prefix to identify an environment variable, as in the following examples. Insert the contents of the PATH environment variable into the buffer: Press i to enter insert mode, press Ctrl-r then =$PATH and press Enter.

How are environment variables set?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


1 Answers

You've (intuitively?) used the correct syntax; as :help expression-syntax explains (under :help expr-env), the syntax is $VAR.

You can compare with an empty string (if $SomeVar != "") or use empty() (if !empty($SomeVar)) to check whether a (non-empty) value has been supplied. It's not so easy to differentiate between empty environment variable and non-existing environment variable, so this is best avoided. (This distinction also is rarely used in shell scripts itself, neither.)

like image 109
Ingo Karkat Avatar answered Sep 21 '22 12:09

Ingo Karkat