ViM (Vi iMproved) is a standard text editor for UNIX like systems. However, in its default
configuration it may be difficult to work with. Below are some of the options that I use
in my configuration file to tune it for more comfortable work. The ViM configuration file
is
~/.vimrc
. To enable any of the options listed below place the command
marked in blue in your ~/.vimrc
file or copy the whole file that I use to enable
them all (whole file is listed at the bottom of this page).
If you are not a long term Vi user and prefer the more flexible ViM you should turn off the
Vi compatibility. In this way ViM will behave more like a standard text editor.
set nocompatible
Turn on the syntax highlighting/coloring and pick the color code used by default for all
files including syntax recognized by ViM (e.g. c/c++/fortran/tex/html/python).
syntax on
colorscheme default
Turn on highlighting of the searched phrase. You can search your file forward and backward
using
/
and ?
respectively. Searched phrase may be highlighting in
the whole file after enabling it.
set hlsearch
Set a limit at which lines will be broken instead of wrapped. I use 150 columns which works
well for all coding that I do.
set textwidth=150
Use spaces instead of tabs. Every time you use
tab
ViM can expand it and replace
it with spaces. From my experience, replacing tabs
to spaces makes the code source
more portable between programmers and avoid confusion with indentation. The following options
enable replacing and set two spaces per tab limit.
set expandtab
set tabstop=2
Show the more that ViM is currently in. ViM has two main modes that you will be using most
often. The insert mode will be marked by showing
-- INSERT --
at the bottom left
corner.
set showmode
One of the most useful options of ViM is its capability to reopen the file keeping the
editing position at which the file was closed. This is the most useful if you are working with
very long files. I have experienced problems with this option by default and found a command that
always ensures the mentioned behaviour.
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
ViM will make a backup copy of every file you will be editing. Most of the time this option
may be rather disturbing since ViM will create file
foo~
when you edit file
foo
. However, the backup copy may safe a lot of work when you accidentally save
unwanted changes of delete something.
set backup
Default ViM coloring may not always work for all languages that you may work with. One can
define a different syntax coloring scheme for certain file types. The following option will use
desert
color code for TeX files and default color code for all other file types.
autocmd FileType tex colorscheme desert
Whenever you start a new file ViM may automatically switch to the insert mode by enabling
the following.
autocmd BufNewFile * startinsert
Editing a plain text file ViM will try to align the following lines based on the alignment of
the previous lines. For example, if you start a line with 5 spaces, type a word and hit enter,
the next line will also start with 5 spaced. To prevent ViM to auto-align the next line use the
following command.
set noautoindent
Although the indentation for plain text file may be not comfortable to work with, working
with various source codes makes the indentation specific for a given language very desirable.
One may enable a language specific indentation using ViM syntax definitions using the following
command.
filetype plugin indent on
ViM is able to show you what commands and key combinations have you just used. The bottom
right corner will show you any combinations like
^w
in case you hit
ctrl+w
. The option may be enabled by the following command.
set showcmd
ViM may display the actual cursor position in the file you are editing by giving you
a row and column number in the bottom right corner. To enable a ruler put the following
command in your
.vimrc
file.
set ruler
One may enable an incremental search in ViM allowing to see the results while you are
typing the phrase to be found. Otherwise the search will start after putting a whole phrase
and hitting enter.
set incsearch
If you want to have a case insensitive search treating equally phrases
ABC
,
abc
or aBc
.
set ignorecase
Set the limit for command history. In command line arrows up and down will show you
a history of the commands you have recently used.
set history=100
A status line with the name of the currently edited file will be always displayed after setting
laststatus=2
. Giving option 0
will result with never displaying and
1
with displaying it only if two or more files are open.
set laststatus=2
ViM differs from most text editors and some keys default action may be different from
their action in WYSIWYG editors. Backspace is one of them. To ensure a classic action of
backspace enable the following command.
set backspace=indent,eol,start
My full .vimrc
looks like this:
syntax on colorscheme default set hlsearch set textwidth=150 set tabstop=2 set expandtab set noautoindent set incsearch set ruler set history=100 set nocompatible set backspace=indent,eol,start set showmode set showcmd set laststatus=2 filetype plugin indent on autocmd BufNewFile * startinsert autocmd FileType tex colorscheme desert set backup set viminfo='10,\"100,:20,%,n~/.viminfo au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif