Make Your .vimrc Trivial to Update

by chromatic

In a recent post on the Perl QA mailing list, David Golden showed off two lovely Vim bindings to load Vim's configuration file in a buffer for editing, then to reload that file in the current Vim process.

",v brings up my .vimrc
",V reloads it -- making all changes active (have to save first)

map ,v :sp $VIMRC<CR><C-W>_
map <silent> ,V :source $VIMRC<CR>:filetype detect<CR>:exe ":echo 'vimrc reloaded'"<CR>

I had to modify this to replace $VIMRC with the path to my configuration file (~/.vimrc). It worked beautifully.

It's much easier to experiment with writing new bindings in Vim itself, rather than through the ex interface. It's also immeasurably faster to be able to reload the configuration file itself, rather than quitting and restarting the program.

This tip has already made me much more productive. Thanks, David!


Andy Lester
2006-08-09 17:36:01
I'm not sure what it should be doing with the ,V. When I run it as is, I don't seem to get any action happening, and if I hit Enter, I'm told "E172: Only one filename allowed."

Are you missing some spaces? I see there's an extra _ after $VIMRC in the definition of ,v.

Jeremy Jones
2006-08-10 04:54:55
I changed mine to:

map ,v :sp ~/.vimrc^M
map ,V :source ~/.vimrc^M

with the ^M at the end of the line by doing CTRL-V ENTER and it appears to be working OK for me. This kind of reminds me of the "realias" hack in _Perl Hacks_. Great stuff.

Aristotle Pagaltzis
2006-08-10 05:29:09

You don't need a macro to reload. While editing the .vimrc, just say:

:so %

Knowing about % (expands to the filename of the active buffer) and postfixed variants like %:p:h is handy like that.

2006-08-10 09:38:30
If you don't want to fret with Windows/Linux compatibility, you can use platform-independant explicit line endings, like so:

map ,v :sp ~/.vimrc<Enter>

2006-08-11 12:59:18
Andy, I just double-checked the formatting. I'm sure it's correct now.