Vi is an editor that appears in almost every UNIX related system.
Vi is based on ed editor.
We are going to list essential commands for all vi flavours (I am not interested in vim program).
Vi program has three working modes (insert, command and last line command modes)
In insert mode you can directly type text by using keys.
In command mode key presses translate into commands.
While in command mode, by pressing : we enter in last line command mode to execute commands like quit, etc.
Run vi program
$ vi foo.txt# opens foo.txt file.
Initially vi opens in command mode.
Insert text (change into insert mode)
i - insert keys starting at cursor
a - append after cursor
o - insert a new blank line
(To return to command mode press Esc key)
Esc key - change from insert into command mode.
Move the cursor
k - move the cursor up
j - move the cursor down
l - move the cursor to the right
h - move the cursor to the left
^ - move the cursor to beginning of current line
$ - move the cursor to end of current line
w - move to next word
b - move to previous word
Add numbers to commands
If we prepend a number to a command we execute the command that specified number of times:
3k - move cursor three lines up
2$ - move to end of next line (not current one)
Copy (yank) text
y$ - copy text from cursor to end of line
yy - copy whole current line.
3yy - copy three lines
x - delete character under cursor
dd - delete whole current line.
d$ - delete from current character under cursor until end of the line.
E.g: 3dd - delete three lines
Put (paste) text back
p - put back previously copied or deleted text.
u - undo previous command.
Search for some pattern in the text
/string - search forward for "string" pattern in the text.
?string - search backward for "string" pattern in the text.
n - jump to next occurrence of pattern match in the text.
N - jump to previous occurrence of pattern match in the text.
Write changes and exit the application
We enter in last line command mode by pressing : while in command mode. E.g:
:w - write changes to file
:q - quit
:wq - write changes and quit.
:q! - quit without writing changes.