Even though it takes years of experience before using Vim becomes both comfortable and extremely efficient, we can reach a minimum level of knowledge in a relatively short time. It is important to master Vim sufficiently to be able to use it day to day.

First of all it is important to know that Vim has multiple working modes. In each mode certain types of actions can be performed. The most common Vim modes:

Exiting Vim

Even though it seems strange at first, exiting Vim is a stressful element for beginners. Two major cases:

ESC changes the current mode and switches to normal mode. : switches from normal mode to command line mode. w saves the changes made. q exits the vim program. ! ignores the warning when you want to exit but there are unsaved changes.

Normal Mode

Vim starts automatically in normal mode. Navigation uses:

Copy / Delete (all from normal mode):

All commands accept a numeric prefix: 5w jumps 5 words, 8dd deletes 8 lines, 2j moves 2 lines down.

Configuring Vim

One of the most important advantages of Vim is that it can be adapted to your own needs and preferences. Changes are made by editing ~/.vimrc or by installing plugins. :scriptnames displays all loaded configuration files.

For per-project configs, add the following at the end of ~/.vimrc. Vim will then load a local .vimrc from whichever directory you open it in.

set exrc
set secure    " prevent :autocmd_, shell or write

Useful tab and line settings:

set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set nosmarttab
set textwidth=80

Space as Leader

Leader is the key that starts custom key sequences. Using Space lets you press it with either hand, making sequences faster than a corner key like backslash.

let mapleader = "\<Space>"

Open and save shortcuts (requires the CtrlP plugin):

nnoremap <Leader>o :CtrlP<CR>
nnoremap <Leader>w :w<CR>

System clipboard copy and paste:

vmap <Leader>y "+y
vmap <Leader>d "+d
nmap <Leader>p "+p
nmap <Leader>P "+P
vmap <Leader>p "+p
vmap <Leader>P "+P

Enter visual line mode from normal mode:

nmap <Leader><Leader> V

Command-line Examples

Among the best methods of learning new commands is the detailed understanding of small examples. Here is a breakdown of :g/^/m0, which reverses the order of lines in the buffer:

Applied to every line in sequence, each line ends up at the top, reversing the whole buffer. To reverse only lines 10 to 20, prefix the range. If the tac utility is available, it can do the same via a shell pipe. Visual selection also works: select with SHIFT+V, then type the command.

:g/^/m0

:10,20g/^/m9

:%!tac

:10,20!tac