VI (vee-eye) / VIM (Vi IMproved) editor. How to work in VI and VIM.

Made with vi Many of the VI commands / keys in this article enclosed in single quotes, for example:.
'f'
This is done for ease inline commands / keys search.
If you interested to find specific command or key just put it in single quotes and search it with the browser's Ctrl+F hot key.
You should type just f (without single quotes) in such case when working in VI.
So,
'f'x equals just typing fx in Vi.
Most of VI command keys comes from Linux ed - line-oriented text editor (see man 1 ed).
So, ed is precursor of vi. If you interested in roots you also can learn Ed.

Starting

Read, Create and Edit file vi "file_name"
Read (read-only) vi -R "file_name" or :set ro
Open file at the end of file vi + "file_name"
Open file on line 1787 vi +1787 "file_name"
Open file on pattern vi +/"pat tern" "file_name"
Go to command mode "ESC" key
Show current mode, command or insert :set showmode

Cursor Movements inside the text

The cursor to the beginning of the file 'gg' or [[
Cursor to end of file (Go To) 'G' or ]]
 
Move back to the previous line character 'h' or Ctrl+h or Backspace
Move forward to the next line character 'l'
Move UP to the previous line 'k'
Move DOWN to the next line 'j'
 
To the first visible symbol of the curent line '^' or '0' or Shift+-
To the 'n' position of the current line 'n'|
To the end of the current line $
Go to the line number 1787 :1787 or 1787G
 
Move back to the beginning of the previous sentence '('
Move forward to the beginning of the next sentence ')'
Move back to the beginning of the current paragraph '{'
Move forward to the beginning of the next paragraph '}'
Move back to the beginning of the current section Shift+'['
Move forward to the beginning of the next section Shift+']'
 
Move BACK to the beginning of a word 'b' ('B')
Move FORWARD to the beginning of the word 'w' ('W')
Move forward towards the end of a word 'e' ('E')
Move to the beginning/end of the block due to the symbol on which the cursor %
b2 w2 e2 y{ 400G :400 6 5j

Screen related movements

Redraw screen Ctrl+'l' or Ctrl+'r'
Show line count and file position Ctrl+'g'
 
Move cursor back by history of movements Ctrl+'o'
Move cursor forward through the history of movements Ctrl+'i'
 
One screen forward Ctrl+'f'
One screen backward Ctrl+'b'
One half-screen forward (down) Ctrl+'d'
One half-screen backward (up) Ctrl+'u'
 
Move the current line to the top of the screen and scroll 'z'[return]
Move current line to the center of the screen and scroll 'z.'
Move current line to the bottom of the screen and scroll 'z-'
 
Move the cursor to the first line of the screen (hight) 'H'
Move the cursor to the middle of the screen (middle) 'M'
Move cursor to last line of the screen (low) 'L'
3L 3H cH dL yM 5e

Editing: Paste

Insert at the cursor position 'i'
Insert at the beginning of the current line (Insert) 'I'
Insert at position after cursor ('a'fter) 'a'
Insert at the end of the current line (Append) 'A' or '$a'
Insert a paragraph below the current line 'o'
Insert paragraph above current line 'O'
25a*- [ESC]

Editing: delete

Delete character under cursor 'x'
Delete character before cursor 'X'
Delete word (if cursor at the beginning of the word) 'dw'
Delete line under cursor 'dd'
Delete characters from cursor to the end of line 'D' or 'd$'
Delete characters from cursor to the end of file 'dG'
Delete characters from cursor to the begin of file 'dgg'
d2b 2dd d$(D)

Editing: delete + insert

Replacement. Delete whole line and go into edit mode 'сс' or 'S'
Replacement. Delete line from current position till the and of line and go to edit mode 'С'
Delete character after cursor and go into edit mode 's'
с$(C) c0 c2w c2bw c2W

Editing: replacement

Single character replacement 'r'
Replacing characters after cursor (large replace) 'R'

Editing: text selection + cut + paste

Character highlighting 'v'
Line selection 'V'
Rectangular selection of lines (visual block) 'Ctrl+v'
Copy selected to buffer 'y'
Cut selection to buffer 'd'
Recover deleted text and paste after the cursor 'p'
Recover deleted text and paste before the cursor 'P'
Copy entire line to buffer (yank) 'yy' or 'Y'
v5ec

Editing: registers (:help registers)

View registers :registers
Copy (append) 7 lines to the buffer named 'd' "d7yy ("D7yy)
Recover text from buffer 'd' after the cursor "dp
Recover text from buffer 'd' before the cursor "dP

Editing: moving text

Join a lines. Current and next lines will be merged to one 'J'
Read text from a file name :f "file_name"
3p yw y$ y0 4yy "a5dd  "Zy)

Editing: Text Substitution

Increase the numeric value by one Ctrl+'a'
Decrease numerical value by one Ctrl+'z'
Change case to opposite '~'

Search and mark text

Word search by pattern /word or /"wo rd"
Repeat word search forward 'n'
Repeat word search backward 'N'
 
Go forward to the first 'x' character on the current line 'f'x
Go back to the first 'x' character on the current line 'F'x
 
Set marker named 'a' at the current line ma (you can use: a-zA-Z)
Go to beginning of the line marked with 'a' 'a
 
Search as you typing your search text :set incsearch
Ignore case when searching :set ignorecase
dfx

Saving, reading and printing files

Save the buffer of the current window to file :w
Save the buffer of the current window to a file name :w "new_file_name"
Exit without saving (quit!) :q!
Exit with saving (write quit) :wq or ZZ
Force exit with saving (even in "ro" (write quit!) :wq!
Append current window buffer with the contents of the file
Append current window buffer with the output of the command :r !cal -m3
Override current window buffer with the contents of the file :e "file name"
Override current window buffer with the contents of currenty opened file :e!
Print file :!lp %
Print custom file !lp "file_name"
Switch to next file (if vi invoked with more than one file name) :n
:230,$w newfile :340,$w >>newfile :185r data :$r data :0 data :/pattern/r data

Error correction

Undo Recent Changes (undo) 'u'
Restore recent changes (redo) Сtrl+'r'
List of undo history :undolist
Restore 2nd undo from latter "2p
Go backward in time for 4 min :earlier 4m
Go forward in time for 45 sec :later 45s
Undo last 5 changes :undo 5
Restore line to previous state 'U'
Restore latter deletion 'p'
Repeat latter command '.'
Recover file after system interrupt vi -r "file_name"
Suffers from a single undo level? :set nocompatible
"1pu.u.u

Folds :help folding

Folds activation :set foldmethod=indent
Set cursor on text to collapse fold 'zc'
Set cursor on text to unfold fold 'zo'
Set cursor on text switch between collapse / unfold 'za'

Many of Buffers :help buffer-list

Load a buffer into the current window :b1(2..)
List of buffers and its associated files :buffers :ls
Delete buffer :bd 1(2..)

Many of Windows :help windows

Create a horizontal window with a new buffer :new
Create a horizontal window with a buffer equal to the current Ctrl+'w','s' or ':sp'
Create a vertical window with a buffer equal to the current Ctrl+'w','v' or ':vsp'
Cycle movement between windows Ctrl+'w', Ctrl+'w'
Directional movement between windows Ctrl+'w', arrow keys or /h/j/k/l
Rotation window places Ctrl+'w','r'
Make current window on top Ctrl+'w','K'
Resize the current window to 10 lines :resize 10
Maximize current window size Сtrl+'w','_'
Align dimensions of all windows Сtrl+'w','='

Many of Tabs :help tabpage

Open new tab :tabnew
Go to next tab gt
Go to previous tab gT
Move current tab to position 0 :tabmove 0
Close tab ':tabc' or ':q'

Additionally

Execute shell command :! "cmd"
Run shell :!sh
Set line length (wrapmargin) :set wm=n
Show line numbers (numbers) :set nu / :set nonu / n,n#
Preserve buffers (dont delete .swp on exit) :pre
Show current options :set
Show all options :set all
Shift+K invoke help on-cursor command

Native funcitons

Total lines number :=
Current line number :.=
Current line number and content :#
Print line match the pattern (its number with =) /pattern/=
Current line :.
List file line :$
Every line in the file :%
Display line :# or p
Copy co
Move m
Command separator |

Native funcitons examples

 :.,$d :20,.m$ :%d :%t$ :.,.+20d :226,$m.-2 :.,+20# :-,+t0 1,3#
 :/pa/+d :/pa1/,/pat2/d :.,/pattern/m23 :/pattern/;+10p :g/pattern :g/pattern
 :60,124g/pattern/p :1,3d|s/thier/their/ 160,224m23 13,29co100
 :g /^Chapter/ .+2w >>begin (as same .+2|+2|++) :/^Part 2/, /^Part 3/g /^Chapter/ .++w>> begin | +t$
:% s/pattern/subs/g -- for every line substitute pattern by subs globally (all line long)