I have spent a lot of time peering into a Vim window, and correspondingly, a lot of time testing different configurations. These are the best non-standard options I’ve found or stolen from others over the years; listed below in order of descending usefulness — though I think everything in this article is worth skimming — are tips that should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing.
Note: no plugins are covered here, just vanilla Vim.
.vimrc configuration items
For whatever reason, the following options aren’t set by default, but they should be.
- Turn on
Don’t worry about the name. What this does is allow Vim to manage multiple buffers effectively.
- The current buffer can be put to the background without writing to disk;
- When a background buffer becomes current again, marks and undo-history are remembered.
Turn this on.
These are very similar keys. Typing
'awill jump to the line in the current file marked with
`awill jump to the line and column marked with
It’s more useful in any case I can imagine, but it’s located way off in the corner of the keyboard. The best way to handle this is just to swap them:
nnoremap ' ` nnoremap ` '
leadercharacter is your own personal modifier key, as
gis Vim’s modifier key (when compared to
vi). The default
\, but this isn’t located standardly on all keyboards and requires a pinky stretch in any case.
let mapleader = ","
<SPACE>is also a good choice. Note: you can of course have several “personal modifier keys” simply by mapping a sequence, but Vim handles the
leaderkey more formally.
- Keep a longer history
By default, Vim only remembers the last 20 commands and search patterns entered. It’s nice to boost this up:
- Enable extended
%key will switch between opening and closing brackets. By sourcing
matchit.vim— a standard file in Vim installations for years — the key can also switch among e.g.
if/elsif/else/end, between opening and closing XML tags, and more.
runtimeis the same as
sourceexcept that the path is relative to the Vim installation directory.
- Make file/command completion useful
By default, pressing
<TAB>in command mode will choose the first possible completion with no indication of how many others there might be. The following configuration lets you see what your other options are:
To have the completion behave similarly to a shell, i.e. complete only up to the point of ambiguity (while still showing you what your options are), also add the following:
.vimrc configuration items
Most people like these.
- Use case-smart searching
These two options, when set together, will make
/-style searches case-sensitive only if there is a capital letter in the search expression.
*-style searches continue to be consistently case-sensitive.
set ignorecase set smartcase
This is usually the most useful combination.
- Set the terminal title
gvimwill always have a window title, but when
vimruns within an xterm, by default it inherits the terminal’s current title.
This gives e.g.
| page.html (~) - VIM |.
- Maintain more context around the cursor
When the cursor moves outside the viewport of the current window, the buffer scrolls a single line to keep the cursor in view. Setting the option below will start the scrolling three lines before the border, keeping more context around where you’re working.
zzis also handy; it centers the window on the cursor without moving the cursor. (But watch out for
ZZ, which does something entirely different!)
- Store temporary files in a central spot
Swap files and backups are annoying but can save you a lot of trouble. Rather than spread them all around your filesystem, isolate them to a single directory:
$ mkdir ~/.vim-tmp # or whatever
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
This is especially valuable after an unexpected reboot — you don’t have to track down all the leftover temp files. However: if you are editing files on a shared file system, it’ll be easier to clobber concurrent modifications, as other users’ Vim processes won’t see your swaps.
- Scroll the viewport faster
<C-y>scroll the viewport a single line. I like to ratchet this up:
nnoremap <C-e> 3<C-e> nnoremap <C-y> 3<C-y>
- Enable limited line numbering
It’s often useful to know where you are in a buffer, but full line numbering is distracting. Setting the option below is a good compromise:
Now in the bottom right corner of the status line there will be something like:
529, 35 68%, representing line 529, column 35, about 68% of the way to the end of the buffer.
- A bunch of stuff your OS should already do
If you are running Windows or OS X or a sloppy Linux distribution, you may not be using these:
" Intuitive backspacing in insert mode set backspace=indent,eol,start " File-type highlighting and configuration. " Run :filetype (without args) to see what you may have " to turn on yourself, or just set them all to be sure. syntax on filetype on filetype plugin on filetype indent on " Highlight search terms... set hlsearch set incsearch " ...dynamically as they are typed.
filetypelines enable type-specific configuration, such as knowledge of syntax and indentation. E.g. foo.c will be opened with Vim’s pre-configured C settings, and bar.py will be opened with Python settings.
If the search term highlighting gets annoying, set a key to switch it off temporarily:
nmap <silent> <leader>n :silent :nohlsearch<CR>
- Catch trailing whitespace
The following will make tabs and trailing spaces visible when requested:
set listchars=tab:>-,trail:·,eol:$ nmap <silent> <leader>s :set nolist!<CR>
By default whitespace will be hidden, but now you can toggle with
- Stifle many interruptive prompts
Press ENTER or type command to continue” prompt is jarring and usually unnecessary. You can shorten command-line text and other info tokens with, e.g.:
:help shortmessfor the breakdown of what this changes. You can also pare things down further if you like.
- Stop distracting your co-workers
Vim is a little surly, beeping at you at every chance. You can either find a way to turn off the bell completely, or more usefully, make the bell visual:
Instead of hearing that obnoxious beep, you’ll see a brief window flash. This is similar to
screen‘s interpretation of the bell in its default configuration.
Thanks to Adam Katz and Chris Gaal for their comments and suggestions.