Aug 30, 2008

Configuration of the Vim Editor

vim editor screenshotVim is a fast and feature-rich text editor. It offers syntax highlighting for many programming and scripting languages, editing by regular expressions, and a lot more. In this post I give first a short introduction to vim then I show how to enable some of its powerful features by default on start of vim.

Introduction to Vim

Vim is free as in beer and speech and was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code and the name Vim stands for Vi IMproved. Vim is cross-platform, which means that it works on many different operating systems, including Windows, Linux, and Mac.

Handling of Vim is not completely intuitive, however vim can make you very fast when you edit text as this screencast demonstrates.


Vim gives you great power, which comes at the price of a steep learning curve. There is a tutorial (command :vimtutor), online documentation, you can find many cheatsheets on the internet. For a more accessible interface to vim, there is Cream, a descendant of vim.

The following video offers you a first introduction to the vim editor.


Configuration (vimrc)

In this section I describe my basic configuration for vim. We want to enable some of the powerful features that Vim offers. The initial settings depend on your system. You can check them by opening the file $VIM/vimrc (on Linux this is usually /usr/share/vim/vimrc, type :echo $VIM within vim to find out where you have them).

I want searching with smart case matching, I want to use the mouse to mark text and for jumping with the cursor, and filetype plugins, I want US American spell checking for txt, tex, and HTMLs. I also want an extended status line and have some WYSIWYG with latex which means instant compiling and viewing by pressing ctrl-k.

nocompatiblemost important option. nocompatible prevents vim from emulating the original vi's limitations.
autoindenttells vim to use the previous line's indent level to set the indent level of the new lines.
smartindentlets vim make an educated guess based on the content of the previous line (works for programming code).
tabstopdefines how many spaces correspond to a tabulator.
showmatchtells vim to show you matching opening or closing brackets when you step on a bracket with your cursor.
incsearchvim will search for text as you enter it.
syntax onsyntax highlighting on
filetype plugin indent onload filetype plugins/indent settings
ignorecasecase insensitive
smartcaseif there are capital letters, become case sensitive
wildmenuvim's command line completion
clipboard=unnamedVim yanks into the system clipboard and you can paste into other applications (in linux by middle click)

Try :help options in Vim to see all available options.

The spellfile option is for adding words to your user spell dictionary (move cursor over word and press zg).

Here goes my ~/.vimrc (exported to html by gvim):

set ignorecase smartcase
set nocompatible
set incsearch
set expandtab
set shiftwidth=2
set smarttab autoindent
syntax match Tab /\t/
hi Tab gui=underline guifg=blue ctermbg=blue 
set showmatch
set ru
set mouse=a
filetype indent on
set clipboard=unnamed
filetype plugin on
set wildmenu
autocmd BufNewFile,BufRead *.txt,*.tex,*.html,README set spell spelllang=en_us 
syntax on
set paste
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]
set laststatus=2
:map Q <Nop>
I like especially the status line (Note that ^K has to be entered within vim by pressing three times ctrl-k, and ^M by pressing ctrl-k once and ctrl-m twice). I found the status line in the book Hacking VIM" by Kim Schulz, which explains more about how to personalize vim. When you are programming/scripting and porting code from Windows to Unix it is important to convert carriage returns (CR) to linefeeds (LF) otherwise you'll get errors. The last part of the status line issues therefore a warning in red, if the file format is not unix. I found this hack in the article about conversion between unix and windows with vim at vim.wikia.com.

The last line disables the ex mode.

Further Reading:
In this excellent blog post, the author gives some very useful hints, including code folding and multiple clipboards. Also, you might want to read an introduction to personalization of vim.

More great vim commands you can find at CommandLineFu.

Image Credit:
vim screenshot at wikipedia.

4 comments:

  1. Very good information about this Vim Editor it looks very useful,and I like everything that can give me a easy way to do it

    ReplyDelete