A few hints for Vim taken from the Linux Academy course (which I ought to use more).

Basics

normal mode = command mode -> ESC
insert mode -> i-key

:wq - write & quit
:q! - quit no matter of unsaved changes

h - left
j - down
k - up
l - right

b - back one word
w - forward one word
e - last letter of word

% - switch between matching brackets

  • numbers can modify (navigation) commands, e.g.

(modifier)5e - forward 5 words
(modifier)21l - 21 characters to the right
(modifier)4k - 4 lines down
(modifier)0h - go to beginning of line

A - append at end of line
a - append at position next to cursor
i - insert at position before cursor
I - insert at beginning of line

o - enter insert mode at the beginning of the next line

r - replace character under cursor
(modifier) 4r - replace next 4 characters from cursor position
x - delete characer under cursor

u - undo last command (command!)
. - redo last command
(modifier)4. - redo last command 4 times

d - begin deletion process
dd - delete whole line under cursor
dw - delete a word
(modifier)d3w - delete next 3 words
(modifier)d0 - delete the beginning of the line until cursor position
(modifier)d$ - delete the end of the line until cursor position

Copy, Paste, Search and Replace

y - copy / yank
yy - copy (yank) the current line
(modifier)2yy - copy the current plus next line
p - paste

v - enter mark mode
v3e - mark next three words

>> - insert indentation (default = 8 spaces)
5>> - indent next 5 lines
<< - remove indentation

/ + string + enter - search for string from top
n - go to next result
N - go to previous result
? + string + enter - search from string from botton

:%s/search/replace/gc - search all lines (%) for “search” and replace globally (g) with “replace”, ask for confirmation (c)

Executing External Commands

:!ls -al ~ - do a ls for home directory

:r !cat ~/.bash_history - read in the result of the command (cat in this case) at cursor position

:9,18 ! sort -Vr - sort lines 9 to 18 with the bash sort command (-r reverse, -V version sort meaning 1, 3, 10, 2 will work correctly as a human suspects 1, 2, 3, 10)

Files and Buffers

ZZ - write & quit (:wq equivalent)
:saveas - save a file under new name
:ls - show buffers (aka open files)

:bad text.txt - load buffer address (aka file in location)
:bn - switch to next buffer
:bn - switch to previous buffer
Ctrl+6 - cycle to next buffer