In that case, you could simply use the good old exists()
:let foo = exists('$FOO') ? $FOO : 'default'
There is also empty()
, IMO it only makes sense if you want to undefine environment variables from within Vim. Indeed, as :unlet $FOO
isn't possible, yet, we have to use :let $FOO=''
To obtain all environment variables, you could use getcompletion()
on recent vim versions. The latest version of my obtain-whatever function looks like this:
" Function: lh#command#matching_askvim(what, lead) {{{3
if exists('*getcompletion')
function! lh#command#matching_askvim(what, lead) abort
return getcompletion(a:lead, a:what)
endfunction
else
function! lh#command#matching_askvim(what, lead) abort
let cleanup = lh#on#exit()
\.register('delcom LHAskVimMatchingCompletion')
try
exe 'command! -complete='.a:what.' -nargs=* LHAskVimMatchingCompletion :echo "<args>"'
if exists('*getcmdline')
call cleanup
\.restore('g:cmds')
\.restore_buffer_mapping('?', 'c')
\.restore_mapping_and_clear_now('<c-a>', 'c')
cnoremap <buffer> <expr> ? s:register()
function! s:register()
let g:cmds = split(getcmdline(), ' ')[1:]
return ''
endfunction
silent! exe "norm :LHAskVimMatchingCompletion ".a:lead."\<c-a>?"
return g:cmds
else
" The following may lead to problem with unescaped quotes => use
" getcmdline() when available
silent! exe "norm! :LHAskVimMatchingCompletion ".a:lead."\<c-a>\"\<home>let\ cmds=\"\<cr>"
return split(cmds, ' ')[1:]
endif
finally
call cleanup.finalize()
endtry
endfunction
endif
(It's complex in order to support mappings as well)
To obtain all environment variables, you can then use: echo lh#command#matching_askvim('environment', '')