Vim Tips
Vim is an amazing text editor.
Of course, when you start to use Vim it might seem quite difficult at the first glance. After a while though, when you know the basic functions to edit texts and get to know Vim's more advanced features you don't want to miss Vim any more.
I will not teach you how to use Vim - there are a lot of tutorials out there (see "More Information" below) - I will show you some configuration settings I find useful.
.vimrc
Here are some of the configuration settings that might not be standard (a line beginning with a quotation mark is a comment in .vimrc). You can also download my .vimrc[0] which includes even more settings. I have deleted bits of the file that are not interesting for you (like some abbreviations etc.).
-
When searching text can be found after entering the first letter:
" incremental search
set incsearch -
When searching for a string all occurrences are highlighted by default. I don't like that:
" when searching, do not highlight search results
set nohls
-
I usually don't like tab stops in texts, I rather have them replaced by spaces. When I do need them in a file I unset them via :set noexpandtab
" expand tab with spaces
" unset within vim:
" :set noexpandtab
set expandtab -
When editing ("BufRead", "BufNewFile") Makefiles tab stops must not be replaced by spaces. I don't want to set this manually, of course. Here's the automation:
autocmd BufRead,BufNewFile Makefile set noexpandtab
-
For programming most of us have a favoured tabulator width. This is how you can set it:
" set tabulator width
se tabstop=4
-
With the keys "<<" and ">>" you can shift a line leftwards or rightwards. This is how you can set the width:
" set width for << and >>
se shiftwidth=4 -
These are useful settings for writing comments:
" set formatting options for entering comments
" r - insert comment leader in a comment when a new line is inserted
" o - insert comment leader in a comment when a new line is created using O/o
set formatoptions=ro -
This is how to ROT13 some text by pressing <F3>:
"rot13 some text
map <F3> ggg?G
-
Abbreviations are great. For example, I have to write the DOCTYPE header for HTML files quite often. Since I don't want to do that manually I have defined an abbreviation. If I type "htmldt" Vim replaces that with the DOCTYPE header for me:
" abbreviations
ab htmldt <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
Vim can look up words in a dictionary file. This way I can write the beginning of a word, then press<ctrl>-<x> <ctrl>-<k> and Vim will cycle through the matching words. Each word in the dictionary file is in one line.
" dictionary file --> type beginning of word, then <ctrl>-<x> <ctrl>-<k> and word
" will be completed. words are listed in dictionary file, one word - one line
set dictionary=~/.vim/dictionary.txt -
When you want to paste text into Vim sometimes the autoindenting and some other features are a pain. Autoindenting itself is of course great. So, what I have in my .vimrc is:
" before pasting text set paste --> :set paste
" after pasting set nopaste --> :set nopaste
set autoindent
" smartindent is smart :-)
set smartindent
.vim/filetype.vim
When you use syntax highlighting and Vim doesn't recognize the filetype you can set it manually by entering the command :setfiletype $filetype where $filetype stands for the actual filetype, e.g. html.
For example, if you save your HTML files with the file ending myending instead of html Vim probably won't recognize those files as HTML files. You could set the filetype manually each time you edit a file or you could automate this process:
" my ~/.vim/filetype.vim file
augroup filetypedetect
au! BufRead,BufNewFile *.myending setfiletype html
" more of these lines can follow...!
augroup END
More Information
You can get excellent information in the online help of Vim itself (within Vim type :help). On the
official Vim Homepage there are also links to
Vim Documentation. Be sure to have a look at the Vim Book!
And there are even many more places - just browse the Internet!
To get started with Vim you should always begin with the
vimtutor. Just start it and do what you're told :-)
Learning vi - the "cheatsheet" technique is a good ressource, too - as is the
Vi Reference Card. Another
VIM Quick Reference Card is even available in multiple languages.
[0] In Linux a program called wget is installed quite often. You can download my .vimrc and save it in your home directory with:
wget -O ~/.vimrc http://www.flof.at/2004/dot-vimrc
You're here: Vim Tips.
Last update: January 28, 2007

(c) 2001-2008 Florian Fankhauser, http://www.flof.at/