vim

Modes

vim operates in modes. The default mode in which one opens a file in vim is "normal" mode. The mode in which you are determines the meaning of the keybindings.

To enter insert mode, hit i.

To enter visual mode, hit v.

To get back to the normal mode from any other mode, just hit ESC.

Exiting vim

An old programmer anecdote goes: "How do you create a random stream of characters? Let someone try to exit vim."

You exit vim from normal mode by typing :q. If you have unsaved changes, vim will warn you and ask you to confirm that you want to force quit (thereby losing the changes you have made) with q!. Alternatively, you can write (i.e. save) the file with :w. You can combine saving & quitting into one command: :wq.

Copying, cutting, and pasting

Note

For a more complete overview on copy, cut, and paste options in different vim modes, refer to this linuxize blog post.

In vim-speak, copying is yanking, cutting is deleting, and pasting is putting.

In normal mode, yy will copy the whole line, dd will cut the whole line, and p will paste (after the cursor).

You can yank or delete multiple lines at once by prepending the command with the number of lines you want to yank/delete, e.g. 3dd.

Setting textwidth

:set tw=80, then gg to go to top, then gqG to format the text accordingly.

See also