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:
- normal: for navigation and text manipulation at line level
- visual: for manipulating blocks of text
- insert: used for inserting/modifying text
- command-line: for executing commands, search, filters
Exiting Vim
Even though it seems strange at first, exiting Vim is a stressful element for beginners. Two major cases:
- exit saving changes:
ESC :wq - exit abandoning changes:
ESC :q!
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:
h j k l: one character left, down, up, right respectivelygg G: moves cursor to beginning/end of file^u ^d: scroll half a screen of text up/downw b: jump to beginning of next/previous word^ $: moves cursor to beginning/end of current linea A i I o O: enter insert mode at various positions around the cursor
Copy / Delete (all from normal mode):
yw yb yy y^ y$: copy word, back-word, line, to line start, to line enddw db dd d^ d$: delete equivalents (deleted text is saved for paste)x: delete current characterp P: paste below / above cursoru: undo;U: restore line
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:
:enters command line modegidentifies all lines matching the pattern and runs the command on each/^/matches the beginning of every line (i.e. every line)m0moves the matched line to position 0 (top of 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