vi (vim) has 2 main modes, command mode and insert mode. In command mode, everything you type is a command, in insert mode what you type is written to the screen (file buffer). vi starts in command mode. The more complex commands start with : and appear at the bottom of the screen, you need to press ENTER to execute them.
Insert Mode
To enter insert mode you can press:
- i - to start inserting at the current cursor position
- I - to start inserting at the beggining of the line
- a - to start append after the current cursor position
- A - to start appending at the end of line
- o - append a new line and start editing
- O - insert a new line and start editing
To exit insert mode you press ESCAPE
Command Mode
To move around in command mode you can use the arrow keys in modern vi implementation. Otherwise the keys to move around are:
- h - left
- j - down
- k - up
- l - right
Other useful movement commands
- 0 - start of line
- $ - end of line
- :nn - goto line nn
- G - end of file
Some editing commands. They can be prepended by a number of times to repeat the command, i.e. 4dd will cut 4 lines.
- dd - cut line (delete)
- yy - copy line (yank)
- p - paste after cursor
- P - paste before cursor
File commands (remember to follow by ENTER)
- :w - save file
- :w filename - save as 'filename'
- :q - quit
- :q! - quit without saving
- :e file - open a file
- :r file - insert (read) a file
Other miscelaneous commands
- /pattern - search for pattern forward
- ?pattern - search for pattern backwards
- :s/orig/new/ - search and replace in current line
- :%s/orig/new/ - search and replace throughout the whole file
- !command - execute a shell command
Miscelaneous commands
- Ctrl-P and Ctrl-N (auto complete)
- gqap (command to justify the text on a paragraph)
vi vs vim
If you decide to use plain vi (not vim) you'll lose many of the nice features.
- full use of arrow keys
- syntax highlighting
- indentation
- screen spliting
- multiple buffers management
- tags