netarky.com

Basic vim commands

Updated 26 Feb 2014

Starting vim * Command mode * Insert mode * Visual mode * Ex mode * vimdiff

 

Starting vim

vim is a very powerful and highly configurable text editor. Once installed, the config file is /etc/vimrc. You can edit this to enable all sorts of fancy things, or use a ready-made config file such as this one.

Open a file in vim:

vim somefile.txt

vim has four different modes:

Access or return to command mode at any time by pressing Esc.

 

Command mode

The editor always starts in command mode. Commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them subsequently switch the editor into insert mode. All commands are case sensitive. Sometimes the uppercase versions are blunter versions (s will replace a character, whereas S will replace a line), other times they are completely different commands (j will move down, but J will join two lines).

You can move the cursor with the arrow keys, but this isn't the vim way. You'd have to move your right hand from the standard typing position all the way to the arrow keys, and then back. So...

These commands will quickly move you around the screen:

Here are some must-learn commands for quickly navigating around your text:

Search for strings in your text:

The following commands do basic editing without leaving command mode:

Commands in vim can be juxtaposed together into something more powerful. For example, 2} will advance the cursor two paragraphs while 6x deletes the next six characters. d tells vim that you want to delete something. Here are some possible ways to use d:

Note that deleted text is saved to the clipboard, so d can be used to cut text.

The following commands move the cursor and then switch directly into insert mode:

 

Insert mode

In insert mode you type in your text like in any other word processor. vim can be configured with all sorts of useful options and plugins to better the experience, such as code completion, syntax highlighting and much more.

 

Visual mode

Pressing v from command mode will put you into visual mode. Here you can move around to select text. Pressing V instead will put you into Visual Line mode. It works much the same way except that entire lines are automatically selected as you move the cursor up or down. Ctrl+v puts you into Visual Block mode. This time blocks of text are automatically selected as you move up or down.

Once you have selected some text:

To paste, make sure you're in command mode then press p to paste after the cursor, or P to paste before.

Visual mode is good for selecting specific text to cut or copy. For basic cutting, d is probably faster (whenever you delete something, that something is placed inside a buffer and is available for pasting).

 

Ex mode

Ex mode commands begin with a colon. Here are the most commonly used:

 

vimdiff

One really cool feature of vim is its diff editor. vimdiff opens a horizontally multi-paned view that colourfully highlights differences between files, each pane containing one of the files to be examined/edited. Start vimdiff from the command line:

sudo vimdiff file1 file2

And here are the most important commands specific to vimdiff:

 

Sources:
Vim
vimrc