I use vi quite frequently (Ubuntu/Centos Linux and Mac) for many of my editing needs in addition of couple of other editors (eclipse, intellij, etc). It becomes quite hard to read the manual every now and then when we use very few commands (probably 10-20 or so) on a daily basic. Here are some commands which I used very often.
[ESC] | Escape key |
[CTR] | Control key |
VI – Search
Normal search (Case sensitive)
[ESC]/foo
Backward search (Case sensitive)
[ESC]?foo
Case insensitive search
[ESC]/foo\c (\c can come anywhere)
VI – Undo/Redo
Undo
[ESC]u
Redo (can cancel an undo also)
[CTR]r
VI – editing
Insert before a char
[ESC]i
Append after a char
[ESC]a
Delete current line
[ESC]dd
Delete multiple lines
Delete 300 lines (or whatever is left till end) starting from current
[ESC]30dd
Delete current word
Delete current word. If you are in the middle of the word then it deletes the word partially.
[ESC]dw
VI – saving, etc.
Save the file
[ESC]:w
Save file as
[ESC]:w /path/to/file (Use w! to override)
Quit without saving file
[ESC]:q!
Reload file undoing all unsaved changes
[ESC]:e!
VI – search replace and regular expressions (regex)
Simple global search replace
Simple global (all entries in a line) search replace for all lines from 1 to 10 (use $ for end)
[ESC]:1,10s/foo/bar/g
Remove beginning spaces
Remove 2 beginning spaces from all lines from 10th to last line
[ESC]:10,$s/^ //
Interactive global search replace
Simple interactive global search replace and get prompted for each replacement
[ESC]:1,$s/foo/bar/gc (This will ask you before replacing each match. Flags: g=>all entries on a line, c=>prompt before replacing)