Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use undefined environment variables in .vimrc?

I would like to open NERDTree at vim startup with a specific directory root depending on an environment variable.

Set environment variables will correctly be expanded, like $HOME. The documentation states undefined variables will expand to an empty string.

So this one works correctly with NERD_TREE_ROOT set to an existing directory. But will not if it is undefined. Instead $NERD_TREE_ROOT will be used like a string.

autocmd VimEnter * NERDTree $HOME/$NERD_TREE_ROOT

How can I use undefined environment variables correctly as empty string?

EDIT: To clarify a bit. This is what I wanted to avoid:

if empty($NERD_TREE_ROOT)
    autocmd VimEnter * NERDTree $HOME
else
    autocmd VimEnter * NERDTree $HOME/$NERD_TREE_ROOT
endif

If that is not possible it will do though.

like image 776
matthias krull Avatar asked Feb 29 '12 09:02

matthias krull


1 Answers

Test whether it is empty before autocmd:

if !empty($NERD_TREE_ROOT)
    autocmd VimEnter * NERDTree $HOME/$NERD_TREE_ROOT
endif
like image 145
kev Avatar answered Oct 29 '22 14:10

kev