Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Updating your vimrc file on the fly

#24

Run time:

The vimrc file allows you to preserve your settings so that they are restored each time you launch Vim. But what if you want to update your vimrc file in the middle of an editing session? This episode demonstrates a couple of tricks that make it easy to customize Vim on the fly.

Shownotes

Sourcing the vimrc file

When you launch vim, it will automatically load and execute your vimrc file. If you modify the vimrc file whilst Vim is running you can apply those changes by running the command:

:source $MYVIMRC

$MYVIMRC is a constant which should work whether you are running Vim on a unix system or on Windows.

I would like Vim to automatically source the vimrc each time I save it. This can be achieved with the following autocommand:

" Source the vimrc file after saving it
if has("autocmd")
  autocmd bufwritepost .vimrc source $MYVIMRC
endif

Editing the vimrc file

You can always open the vimrc file by running the command:

:edit $MYVIMRC

I like to make it as easy as possible to open my vimrc file, so I keep the following mapping in my vimrc:

let mapleader = ","
nmap <leader>v :tabedit $MYVIMRC<CR>

Pressing ,v opens the vimrc file in a new tab.

Feel free to modify the mapping and the functionality to suit your preferences. You might want to experiment with replacing tabedit with edit (to open in the current window), or split (to open in a horizontal split), or vsplit (to open in a vertical split).

Comments

Browse similar content


Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..