René Nyffenegger
René Nyffenegger

Reputation: 40509

How can I pass an argument to vimrc when I start (g)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?

Upvotes: 7

Views: 1869

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172608

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

Upvotes: 13

Related Questions