Configuring Vim right
Published November 6th, 2008I have spent a lot of time looking at a Vim window, and correspondingly, a lot of time testing different configurations. These are the best non-standard options I have 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 which 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.
Essential .vimrc configuration items
For whatever reason, the following options are not set by default, but they should be.
- Turn on
hidden
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.
set hidden
- Remap
`to'
These are very similar keys. Typing
'awill jump to the line in the current file marked withma. However,`awill jump to the line and column marked withma.It is more useful in any case I can imagine, but it is located way off in the corner of the keyboard. The best way to handle this is just to swap them:
nnoremap ' ` nnoremap ` '
- Map
leaderto,
The
leadercharacter is your own personal modifier key, asgis Vim’s modifier key (when compared tovi). The defaultleaderis\, but this is not 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 theleaderkey is handled 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:
set history=1000
- Enable extended
%matching
The
%key will switch between opening and closing brackets. By sourcingmatchit.vim, it can also switch among e.g.if/elsif/else/end, between opening and closing XML tags, and more.runtime macros/matchit.vimNote:
runtimeis the same assourceexcept 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:set wildmenu
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:
set wildmode=list:longest
Recommended .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
A running
gvimwill always have a window title, but whenvimis run within an xterm, by default it inherits the terminal’s current title.set title
This gives e.g.
| page.html (~) - VIM |.
- Maintain more context around the cursor
When the cursor is moved outside the viewport of the current window, the buffer is scrolled by a single line. Setting the option below will start the scrolling three lines before the border, keeping more context around where you’re working.
set scrolloff=3
Typing
zzis also handy; it centers the window on the cursor without moving the cursor. (But watch out forZZ!)
- 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
And in
.vimrc: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 will be easier to clobber concurrent modifications, as other users’ Vim processes will not see your swaps.
- Scroll the viewport faster
<C-e>and<C-y>scroll the viewport a single line. I like to speed 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:
set ruler
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.
- 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.
The
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 it can be toggled with
,s.
- Stifle many interruptive prompts
The “
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.:set shortmess=atI
See
: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:
set visualbell
Instead of emitting an obnoxious noise, the window will flash very briefly. This is similar to
screen’s interpretation of the bell in its default configuration.
Here is my own .vimrc, which includes all these settings (and some more which are less generally useful). A fairly good source for other configuration tips is the Vim Tips Wiki.
Thanks to Adam Katz and Chris Gaal for their input on a draft of this article.
Oh great stuff.
vim is a great editor but the defaults stink.
Here are my settings:
http://www.pixelbeat.org/settings/.vimrc
Very nice – thanks!
Pádraig, very nice. I didn’t know about
wildignoreornojoinspaces.I don’t often take the time to leave a note in an article, but this time I had to. This is a good, useful post: some handy stuff about VIM I didn’t already know! Thanks!
[...] http://items.sjbach.com/319/configuring-vim-right : quelques astuces pour Vim. [...]
Plugins I can’t live without:
toggle comments
http://www.vim.org/scripts/script.php?script_id=665
auto completion by tab
http://www.vim.org/scripts/script.php?script_id=182
svn/cvs/git integration
http://www.vim.org/scripts/script.php?script_id=90
fast switching between most recently used files
http://www.vim.org/scripts/script.php?script_id=42
fast switching between h/cpp
http://www.vim.org/scripts/script.php?script_id=31
Nice to have plugins:
emacs style quick copy and paste
http://www.vim.org/scripts/script.php?script_id=2064
auto completion in command mode from current file
http://www.vim.org/scripts/script.php?script_id=2222
see the classes, methods, functions,
macros defined in the file
http://www.vim.org/scripts/script.php?script_id=273
Daniel, I didn’t know about
confirmorautochdir— those could certainly be useful.[...] items.sjbach.com » Configuring Vim right – These are the best non-standard options I have 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 which should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing. [...]
I also rather like
set expandtabandset smarttab, being someone who absolutely hates accidentally placing hard tab characters in source code.A big thank you for this!
Perhaps number 5 would be better written as:
nnoremap 3
nnoremap 3
Damn, I knew that wouldn’t format properly. How about this:
nice instruc
Cale, me too. I like to handle this using
autocmd Filetypeso that it’s set at a finer granularity.Alex, yes! I’ve modified the article. Thanks.
“The default leader is \, but this is not located standardly on all keyboards and requires a pinky stretch in any case. ”
On UK/Ireland keyboards it’s just between left shift and Z, doesn’t even need shift. It’s positioned pleasingly roughly symmetrically with / on the right.
Hi
Good list, here’s a few I add to that list.
http://blog.learnr.org/post/59098925/configuring-vim-some-more
Doesn’t include plugins too, which can be quite useful. I like yankring and autotag.
Nice! Thanks. I’ve added a few to my .vimrc file.
A small point, using
<SPACE>as a leader is a bit problematic if you use it to move to the next char.Hi,
The file $VIMRUNTIME/macros/matchit.txt says that matchit.vim is a plugin, and that putting it in ~/.vim/plugin will autoload it (no need to edit config file), and it also says how to install the associated help file.
Razee, that’s fine.
matchit.vimhas been distributed with Vim for over eight years, so it’s unlikely you’ll see an installation where it’s missing — but it doesn’t hurt to be sure.[...] vim configuration tips. Three I didn’t know and now use: set title "set terminal title to filename set wildmode=list:longest " for bash-like tab-completion set scrolloff=3 "keep 3 lines context when scrolling [...]
Great post! I’ve been playing around with all of the defaults you spoke about in your article, but I definitely missed a few important ones! It’s also quite nice to hear the opinion of a seasoned vim expert.
Spencer, glad the article was helpful!
Thanks for the tips. I really needed something like this to completely make the switch from a gui text editor to vim. I feel a lot more comfortable now. Hats off!
[...] Configuring Vim Right [...]
[...] Configuring Vim right [...]
[...] that the vimrc I provide via bzr (or via direct link) has been updated. Tom recently found a site or two that had some nifty settings I hadn’t stumbled across yet. Changes [...]
[...] Configuring Vim Right [...]
Nice writeup. I would recommend against your suggestion #2, however. Apostrophe is a line movement command, and Backtick is a text movement command. Since vim is both line editor and a text editor, it makes a big difference which you use. Personally, I use line commands way more often than text commands, as they are quicker to fire off. “
ma j j j d'a” will cut 4 lines of text, no matter what column your cursor is in. Likewise, you can paste these w/o regard for the cursor column and vim does the right thing (@"is marked as a line register from the cut). I guess this thing would depend on your usage pattern of course.Hi Jim, you have a point. I rely heavily on visual-mode, so I would more likely do this as “
V j j j d” — I tend to use marks only for navigating text, rather than for manipulating it. But this isn’t true of everyone and it’s good to get a different view.Vim is a good editor, made great with a custom .vimrc file.
My .vimrc file adds syntax highlighting, autocomplete with tab, folding and highlights the line you are on.
Feel free to use or modify it for your needs.
Here is my .vimrc file
And my guide to the best 15 vim plugins
[...] Configuring Vim right – items.sjbach.com have spent a lot of time looking at a Vim window, and correspondingly, a lot of time testing different configurations. These are the best non-standard options I have 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 which should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing. (tags: programming/vim vim, vimrc) [...]
Refined the gvimrc portion you inherited from me to better handle sizing (especially useful for gvimdiff), fixed (upgraded?) buggy matchit line:
[...] This site has lots of useful configuration options to consider to make vim more useful. [...]