Beginners are often scared away from vi and vim from the start because the first thing they usually do is open it, try to start typing, and then see all kinds of random things happen, none of which type text to the screen. They even have trouble trying to quit! Well, fear no more, I've got the basics outlined here for you. For the record, vimtutor should come with your vim and it is the best place to start.
Command Mode vs Insert Mode
You start in command mode, and can get back to command mode by hitting ESC. To enter insert mode, there are a number of commands, but the most straight forward is to press i.
Loading a File
Type :read FILENAME in command mode to open a file.
Saving and Quitting
- :w - Write
- :wq - Write and Quit
- :q - Quit
- :q! - Force quit, don't save changes
In command mode you use j, and k to move down and up lines, and use h and l to go left and right.
- gg - Jump to beggining of file
- G - Jump to end of file
- 44G - Jump to line 44
- a - Insert after cursor
- A - Start at end of line
- i - Insert before cursor
- I - Capital i, Insert at beginning of line
- o - Start new line below
- O - Capital o, Start new line above
- dd - Delete current line
- d (DIRECTION) - D followed by an up arrow, down arrow, j, or k will delete the current line and the specified line (above or below)
- d3d - Delete a variable number of lines, 3 can be replaced with any number
- D - Delete rest of line after cursor
- v - Start selection at cursor
- V - Start selection on line (and select whole lines)
- c - Cut
- y - Copy (yank)
- P - In command mode, pastes
- :u - Undo last command
- . - Redo
- / (SEARCHTEXT) - Search for
- / - / By itself again will search the last expression again
I hope this is enough to help others get started with vi/vim. This is only the beginning and doesn't even give a taste for how powerful it can be. After you learn enough you might find it useful to alter your .vimrc to customize your editor.