Basic Vi Keystrokes
Vi is an old but powerful editor available on essentially all Unix systems.
It is actually the visual mode of an even older line editor, ex.
Because of its age, vi does not directly use a mouse.
The most important thing to realize about vi is that it has two modes, command
and insert. Whenever you are in doubt about which mode you are in, hit the escape key - that
will get you into command mode. Command mode is right for moving about a file,
copying or deleting a line, saving a file, etc. While in insert mode the text you
type is actually entered in the file.
Whenever you are in doubt, hit the escape key and you will be in command mode.
That's also the mode you are in when you start editing, e.g. by typing vi filename.
You enter insert mode by typing i or one of the other commands listed below.
Most commands can be repeated by prefacing them with a number; for example typing 5j
in command mode will move the cursor down 5 lines.
(These are for moving the cursor around.)
- h - move left one character
- j - move down one line
- k - move up
- l - move right
- $ - go to the end of the current line
- 0 - go to the beginning of the current line
- G - go to the last line in the file
- 15G - go to line 15
- control-F - forward one page
- control-B - backwards one page
- i - insert text before cursor position
- a - add text after cursor position
- A - append text at the end of the current line
- o - add text after the current line (open)
- O - add text before the current line
- x - delete one character
- r - repalce one character with the next character typed
- dd - delete entire line
- dw - delete word
- 3dd - deletes three entire lines
- p - put the last deleted item after the cursor or after the current line
- P - put the last deleted item before the cursor or before the current line
- "a3dd - delete the next 3 lines and store in buffer a
- "a3yy - yank the next 3 lines (without deletion) and store in buffer a
- "a3yw - yank the next 3 words (without deletion) and store in buffer a
- "ap - put the contents of buffer a
- There are 26 named buffers a-z.
- u - undo last command
- J - join the current line to the next one
Saving and Quitting
- :x- quit and save
- :w - save but don't quit
- :q! - quit but don't save
- :r file - read a copy of file into the current file
- /pattern < return > - find pattern forward and go there
- ?pattern < return > - find pattern backwards and go there
- n - repeat previous search