If you are really curious about the full potential of vi or vim I suggest you start with this link.
Otherwise continue reading for basic vim commands and helpful tips:
To start vim type vi or vim followed by the file name you wish to open. Note: this does not have to be an existing file, vim will automatically create files that do not exist with a given name.
$ vim example.c
When you enter vim you will be in command mode, to start typing press either the 'i' key or 'I' (capital i), this will enter you into 'insert' mode.
To leave 'insert' mode press the 'escape' key
When you wish to save and exit vim you have a few options:
In command mode type ':w' then ':q' to write and quit
Or ':wq' to writequit
Or ':x' to exit and write if buffer contents are unchanged
To copy and paste in vim first enter into command mode, then press the 'v' key to enter 'visual' mode. This will allow you to highlight lines. Select the lines you wish to copy and press 'y' to yank (copy) the selected text. Move to where you wish to paste and press 'p'.
To undo something you can press the 'u' key. Note: you can only undo up to the last write. To redo something press both 'ctrl' and 'r' together.
:w -> write (save)
:q -> quit (exit vim)
:q! -> exits without saving
:x -> save only if buffer contents are updated and quit
i -> enter insert mode
v -> enter visual mode
r -> enter replace mode
esc -> enter command mode
y -> yank (copy)
x -> cut
p -> paste
u -> undo previous change
ctrl + r -> redo previous change
dd -> cut entire line
V -> enter visual mode and select entire line
Vy -> copy entire line
home -> move cursor to beginning of line
end -> move cursor to end of line
page up -> move up one page at a time
page down -> move down one page at a time
:e -> reloads the document
:e! -> force reload the document
To find and replace in vim we use the substitute command ':s'. The general syntax of the command is as follows
:<range>s/<string to replace>/<replacement string>/<modifier> <count>
See this site for a more in depth look.
:s/func/my_func/ -> replace the first occurrence of func with my_func on current line
:s/func/my_func/g -> replace all occurrences of func with my_func on current line
:%s/func/my_func/ -> replace the first occurrence of func on every line with my_func
:%s/func/my_func/g -> replace all occurrences of func with my_func in the file
g -> replace all occurrences
i -> case insensitive
c -> check each case before modification
You can also use regular expressions in your substitution strings
:<some number> -> moves cursor to line specified by <some number> (e.g. :101 moves cursor to line 101)
f -> moves cursor to position of next character typed, e.g. typing f; would move cursor to next semicolon
F -> moves cursor to position of previous character typed, e.g. typing F; would move to previous semicolon
% -> when cursor is over a bracket this key stroke will move to the matching bracket, e.g. pressing % while highlighting { would move to the matching }
:n -> you can open multiple files simultaneously in vim by specifying them in the vim command (e.g. vim test.c otherfile.c), this key moves to the next file in the list (e.g. if you are in test.c, pressing :n will move to otherfile.c)
:N -> moves in the reverse direction (e.g. if you are in otherfile.c, pressing :N will move to test.c)
:wn -> save and move to next file
:wN -> save and move to previous file
a -> moves cursor to the right by one and enters insert mode (append)
A -> moves cursor to the end of the line and enters insert mode
gg -> moves cursor to the top of the file
G -> moves cursor to the bottom of the file
h -> moves cursor left one
j -> moves cursor down one
k -> moves cursor up one
l -> moves cursor right one
<some number><h, j, k, l> -> moves <some number> of spaces in the direction specified by the letter key (either h, j, k, or l)
<some number>dd -> cuts <some number> of lines from the file
:!<shell command> -> runs the <shell command> on the terminal without exiting vim