Vim

Some Frequently Used Commands

Cursor Movement

e   jump forwards to the end of a word
E   jump forwards to the end of a word including punctuation
b   jump backwards to the end of a word
B   jump backwards to the end of a word including punctuation
$   jump to the end of the line
0   jump to the start of the line
G   go to the last line of the document

'.   jump back to the last modified line
`.   jump to exact position in last modified line

Ctrl-o   jump cursor backwards
Ctrl-i   jump cursor forwards

Text Selection

v   start visual mode
V   start line-wise visual mode
Ctrl-v   start visual block mode
aw   mark a word
ib   mark a block inside ()
ab   mark a block with ()
iB   mark a block inside {}
aB   mark a block with {}

Delete Text

D or d$   delete from cursor to end of line
c$   delete from cursor to end of line and start insert mode
d0   delete to start of line
dB or dT〈Space〉   delete backward to the first space
dt   delete forward to the first space (if there is no space then it will be not applied)
cw or cW   delete forward not including following white space and start insert mode
dw or dW   delete forward including following white space)
cc or S  delete line and start insert mode
s   delete character and start insert mode

Format Text

J   join two lines (line below to the current one)

Setting textwidth (tw) will auto line break during typing:
set tw=80

To break a long line into multiple lines
gqgq   in normal mode or
gq   in visual mode

~   swap upper/lower case
U   change selected text to upper case
u   change selected to lower case
:s/\<[a-z]/\u&/g   capitalize words in the current line
xp   swap the current character with next one
Xp   swap the current character with previous one
ddp   swap the current line with next one
ddkP   swap the current line with previous one

>   shift text right
<   shift text left

Text Substitution

r   replace the character under the cursor

:%s/old/new/gc   replace all occurrences of the "old" text with the "new" one asking for confirmation

To replace some text in visually selected part:
:s/old/new/g

:1,10s/old/new/c   substitute in the lines 1-10 conforming each substitution.

For more info, see article at thegeekstuf.com

Working with Lines

Jump to a line number
:any_desired_number

dG   delete everything from the current line to the end of file

dgg   delete everything from the current line to the top of the file

Remove all lines that started with a dollar sign: $
:g/^\$/d

Sort, removing duplicate lines
:%sort u

Sort only specific lines using ranges, for instance, from 5 to 10 (inclusive)
:5,10sort

Remove blank lines in selection
:'<,'>s/^\n//g

Undo/Redo

u   undo
Ctrl-r   redo
.   repeat last command

Tabs

:tabe   open a new tab with an empty window, after the current tab
:tabnew   same as :tabe
:tabclose
:tabs   list all tabs
:tabm   move current tab to last
:tabm 0   move current tab to first
:tab ba   rearrange the screen to open one window for each buffer in the buffer list

To open file in a new gvim tab
$ gvim --remote-tab-silent 〈file〉

Highlighting

:set cursorline   highlight the current line in every window
:set hlsearch   activate highlighting
:nohl   de-activate
:set hlsearch!   toggle search

Spell check

:set spell   activate spell check
:set spelllang=en   set up English language
z=   see suggestions
zg   add word to dictionary
]s   jump to the next spelling mistake
[s   jump to the previous spelling mistake

Filename Completion

Ctrl-X Ctrl-F   show pop-up listing of files in the current directory (insert mode)

Special Characters

To enter special characters it is needed to set the digraph option
:set dg
and to enter £ type
P Ctrl-H d
more info at Vim Wiki

Configuration

To make Vim settings permanent on start-up it is need to create/modify $HOME/.vimrc

Cursor Movement

By default cursor stops to move at the end or at the beginning of the line. To go automatically to the next/previous line, add
set whichwrap+=<,>,[,]

Copy and Paste

Copy:
vnoremap <C-Insert> "+y

Cut:
vnoremap <S-Del> "+x

Paste:
map <S-Insert> "+gP
imap <S-Insert> <SPACE><ESC><S-Insert>i<Del>

Word Wrap

To wrap long lines, but not split words in the middle (wrap only at characters defined in breakat)
:set nolist wrap linebreak breakat

Tabstops

set tabstop=2
set shiftwidth=2

Changing Double-Click Behavior

set iskeyword+=:,.,-,/,$
double click selects the current word. By default, a 'word' excludes special characters. Sometimes, it could be useful to specify which characters are included in a word.

Hot Keys

inoremap <F2>  <Esc>:update<CR>a   save file and return to insert mode.

nnoremap <F12>c  :exe ':silent !chromium-browser %'<CR>   load active windows in chromium

:verbose map <C-k>  check if key Ctrl-k is mapped or not

For more guidance on mapping, see, for instance, vim questions on stackoverflow.com

Custom Menu

amenu <silent>Tools.Preview\ &HTML :exe ':silent !chromium-browser %'<CR>

Statusline

Show Invisibles

set list listchars=tab:»\ ,trail:·,nbsp:¬,eol:¶

»  U+00BB   right-pointing double angle quotation mark
·  U+00B7   middle dot
¬  U+00A0   non-breaking space
¶  U+00B6   pilcrow sign

To add an Unicode character:   Ctrl-v u xxxx,   where xxxx is the Unicode 4-digit number. The information about Unicode characters can be found at Wikipedia or FileFormat.Info

To change the default color of these characters, add into .vimrc after colorscheme (if you use non-default color scheme):
highlight NonText guifg=#4a4a59
highlight SpecialKey guifg=#4a4a59

Be sure, that your favorite font is able to show these characters. For example, a nice Inconsolata font does not have the "Black right-pointing small triangle" (HTML hex-Entity: &#x25b8;)
set guifont=DejaVu_Sans_Mono:h9 "Consolas,Monaco,Inconsolata

Initial Window Size

set lines=50 columns=84

Multi-Window

If window is split, to move cursor to the window below current one: Ctrl-w,↓ or Ctrl-w,j.

Vim Scripts

UltiSnips: snippet solution for python enabled Vim

Distinguished: dark vim color scheme

session.vim: Extended session management for Vim

256 Colors in Vim

To enable 256 colors:

set t_Co=256

Additional Information

Vim documentation