Configuring Vim right
Published November 6th, 2008I’ve spent a lot of time nerding into a Vim window, and ergo, a lot of time fooling around with 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 — is an omnium-gatherum of tips that should have value to any developer, no matter how they like to run Vim. That is, minimal editorializing.
Note: no plugins are covered here, just vanilla Vim.
Essential .vimrc
configuration items
For whatever reason, the following options aren’t 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
'a
will jump to the line in the current file marked withma
. However,`a
will jump to the line and column marked withma
.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 ` '
- Map
leader
to,
The
leader
character is your own personal modifier key, asg
is Vim’s modifier key (when compared tovi
). The defaultleader
is\
, 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 theleader
key 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
— 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.runtime macros/matchit.vim
Note:
runtime
is the same assource
except 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
gvim
will always have a window title, but whenvim
runs 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 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.
set scrolloff=3
Typing
zz
is also handy; it centers the window on the cursor without moving the cursor. (But bewareZZ
, 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
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’ll be easier to clobber concurrent modifications, as other users’ Vim processes won’t see your swaps.
- Scroll the viewport faster
<C-e>
and<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:
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 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.
The
filetype
lines 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
,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 shortmess
for the breakdown of what this changes. You can also pare things down further if you like.
- Stop distracting your co-workers
Vim is a sullen ally, beeping aspersions at every minor slip. You can either find a way to turn off the bell completely, or more usefully, make the bell visual:
set visualbell
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.
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 comments and suggestions.
Further discussion on the programming reddit
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
wildignore
ornojoinspaces
.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
confirm
orautochdir
— 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 expandtab
andset 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:
Cale, me too. I like to handle this using
autocmd Filetype
so 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.vim
has 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. […]
Thanks, I’ve been using vim for years and I’ve learned quite a few things from this post.
Thanks! This post is incredibly useful!
I just added several of these to my .vimrc. Thanks a bunch!
thanks for this post. just what I was looking for.
what does this part of your .vimrc do?
” “Very magic” regexes in searches
“nnoremap / /\v
“nnoremap ? ?\v
Those modify the /- and ?-style searches to use “very magic” regular expressions; see
:help magic
. Basically, you don’t need to escape some regex special characters like you usually do. It’s not perfect, though, so I keep it disabled these days.Thanks, some useful tips like the title and scrolloff 🙂
[…] .vimrc file that is stored in your home directory. There is a fair amount of things that can be configured. And with right configuration vim is godlike text […]
Thank you!
my eyes can finally stop hurting 🙂
[…] Configuring Vim Right […]
Thank you, this was really useful
If your a Rubyist I put the recommendations here in to my Vimfile: https://github.com/krisleech/vimfiles
You wrote clearly and accurately. That made the ideas easier to understand. Thanks for this very helpful tutorial.
Frinty
[…] Everyone has their own opinion about this, but I found this post to be useful. This was written by admin. Posted on Tuesday, December 27, 2011, at 6:41 pm. Filed under vim. Bookmark the permalink. Follow comments here with the RSS feed. Post a comment or leave a trackback. […]
What are some good resources for learning Vim?…
Okay, this iis going to be a complete hodgepodge of blag posts, command sheets, and Vim 101 posts, and editorials. Look at a whole variety of you’re just starting out, so you can be exposed to a huge variety of views. First thing: Learn about the sema…
thx for your article, it helps me a lot 🙂
[…] http://items.sjbach.com/319/configuring-vim-right I got that you were supposed to be able to use SPACE as the mapleader in vim. I’ve tried but […]
That’s a nice intro into setting up a vim config file for a beginner like me. If you are lazy to type it in all by yourself consider using an app like Vim Options.