Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an argument to vimrc when I start (g)vim?

Tags:

vim

I want to influence how my vimrc file is executed when I start vim and thought of using a global variable like so

if exists("g:UseEnv1")
   ....
else
   ....
endif

This variable would be set when starting vim like so

gvim -c "let g:UseEnv1=1" file-1 file-2 

However, this doesn't work because the -c ex commands are evaluated after the vimrc file is executed.

I could use environment variables

if $UseEnv1 == 1
   ...
endif

Yet I feel this is a bit problematic in case I forget to change the value of $UseEnv1 between two sessions. Ideally, I'd like to explicitly have to state that I want Env 1 when I start vim.

Are there other possibilities?

like image 977
René Nyffenegger Avatar asked Apr 17 '14 07:04

René Nyffenegger


1 Answers

The --cmd command-line argument is like -c, but it is executed before any other initialization. You can use that to set certain Vim variables that influence your ~/.vimrc.

Alternatives and their merits

If you plan to actually type those configurations in the command-line (vs. coding them into a shell alias or similar), the use of environment variables isn't actually so bad: In most (Unix) shells, you can set variables only for one command by prepending them. So instead of

$ gvim --cmd "let g:UseEnv1=1" file-1 file-2

you could write

$ UseEnv1=1 gvim file-1 file-2
like image 109
Ingo Karkat Avatar answered Oct 21 '22 18:10

Ingo Karkat