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
gg   go to the first line of the document
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
dgg   delete from the current line to the top line of the document
dG   delete from the current line to the end of file

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

:filetype indent off/on   disable/enable file type based indentation for all file types

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.

Examples of Search and Replace in Vim

Vi and Vim Editor: 12 Powerful Find and Replace Examples - thegeekstuff.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

Cursor Movement in Multi-Windows

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

Configuration

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

Cursor Stops

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

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 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

If the font does not have some of the characters, try an other one. To set guifont:

set guifont=DejaVu\ Sans\ Mono\ 10
"DejaVu_Sans_Mono:h9 in MS Windows
"Some alternatives: Fira Code, Inconsolata, Consolas

Unicode Characters

To add a 4-hexchar Unicode sequence:   Ctrl-v u u xxxx, where xxxx is the Unicode 4-digit number [Stack Exchange]

To add an 8-hexchar Unicode sequence:   Ctrl-v u U xxxxx Space

gucharmap helps to find the charactecters

The information about Unicode characters can be found at Wikipedia or FileFormat.Info

Initial Window Size

To set the initial size of gvim window

if has("gui_running")
  set lines=26 columns=117
endif

Maximize or set initial window size - Vim Tips Wiki

Enable 256 Colors

To enable 256 colors:

set t_Co=256

Vim Scripts

UltiSnips: snippet solution for python enabled Vim

Distinguished: dark vim color scheme

session.vim: Extended session management for Vim

Additional Information

Vim documentation