dslreports logo

    All FAQs Site FAQ DSL FAQ Cable Tech About DSL Distance DSL Hurdles »»


how-to block ads


Vi, or vim, (www.vim.org) is a very powerful text editor that is found on many *nix systems. It is a bit confusing to use at first, and as such, many elect to go with a more intuitive or 'notepad-like' application when editing their text files, e.g. pico. I would argue, however, that taking the time to learn vi/vim will benefit you in the long run if you plan on spending any extended amount of time working with text files.

Here I will do my best to include some of the most useful and powerful commands that vim has to offer, as well as going over some of the basics of using vim to accomplish all of your text editing needs. Ideally, this offfering would be comprehensive enough to help you when you forget something, but not so verbose that it is overwhelming.

Basic Modes

Vim has two basic user modes - command and insert.

Command mode is the mode in which you enter commands, and while in this mode your cursor will be a solid box. This is the default mode you enter vim in, and is also the mode you move your cursor in, delete, copy, and paste text, search for text, and perform various other editing commands.

Insert mode is one of the modes in which you insert text (surprise). You enter insert mode by pressing i, and you re-enter command mode by pressing esc.

Replace mode is another type of insert mode. To enter replace mode from the command mode, press r or R. The replace commands will write over the next character with subsequent input rather than inserting it between the current and next characters. r will replace one character and then automatically return to command mode, while R will continue to replace text until you press esc.

Getting Around

Moving around inside of vim is done from the command mode, so if you are not sure what mode you are in, press esc and you will be in command mode. (even if you were to start with)

The most basic way to move in vim is using the four letter keys that take you in the four directions. They are:

h - moves left one character
l - moves right one character
j - moves down one line
k - moves up one line

So, h and l are the outside two of the four, and they are your left and right. k and l are the inside two of the four, and j (the left-most of the two) takes you down a line, while k (the one on the inside right) takes you up a line. You can also use the cursor keys to move in these four directions, but this is not the way to go with vim. The key when using vim is to cut down on the time it takes to do things, and moving from the comfortable jkl:/; keys that your right hand sit on normally during typing is a step backward from that goal.

These four keys are the basic keys for movement, but if you need to move through a lot of text they can be a bit slow. The commands below allow you to cover more text in shorter time, depending on your needs.

w - moves the cursor forward one word (includes punctuation characters as words)
W- moves the cursor forward one word (only includes "regular" words)

b - moves the cursor back one word (including punctuation)
B - moves the cursor back one word (not including punctuation)

0 - moves the cursor the beggining of the current line
$ - moves the cursor the end of the current line

^ - moves the cursor to the first non-blank character of the current line

1G - moves you to the 1st line in the file (G is for go to)
xG - moves you to line number x
gg - moves to the beginning of the file
G - moves to the end of the file

:x - also moves you to line number x

^f - scroll forward one screen of text
^b - scroll backward one screen of text
^d - scroll forward half a screen
^u - scroll backward half a screen

H - move to the top line on the screen
M - move to the middle line on the screen
L - move to the last line on the screen

e - move to the end of the current word
E - move to the end of the current word (ignoring punctuation)

% - from any bracket (() [] {}), move to its matching bracket

Command Modification

Vim commands can be modified to become more complex, efficient, and powerful. Movement commands are commands like w, b, 0, and $, and they can take numeric arguments. For example, 4l will move the cursor four spaces to the right, and 3j will move the cursor down three lines. Many commands, such as delete and yank, require movement commands to work from the command mode so that they will know what selection of text they should operate on. For this reason, they can be used in visual mode without accompanying movement commands, since in that mode they already know what text to perform their task on.

In general, vim commands follow one of the two patterns below:

command number movement-command


number command movement-command

Number and command are not always needed, and without those two elements of a full command, you just have a movement command. If you add a number to a movement command, you now have a movement command that goes that number of spaces. If you then add a command like delete, yank, or change to that command, you then have an editing command. The sheer number of possibilites available utilizing these combinations makes vim extremely powerful.

In the command examples below, remember that only a few variations are displayed. Using these command modifications literally thousands of commands are capable of being created; only a few will be shown here so that you can get an idea of what can be done.

Selecting Text with Visual Mode

Modifying larger amounts of text is often needed when working with documents or code. Selecting the text you would like to modify in some way in Windows is usually done with the mouse. In vim, however, selection of text (and everything else) is done with the keyboard. This may seem a bit awkward at first if you are used to using a mouse for this task, but I think you will find that it is really quite fast and powerful once you get the hang of it.

Typing v or V while in command mode will take you to visual mode. This will allow you to then move your cursor around and control what is selected as you move. Once you have highlighted what you want to select you can then use the commands found in the next section to make changes to or otherwise control the text you have selected. Remember, lowercase v allows you to select by character, and uppercase V allows you to select by line.

v - puts you in visual mode (selecting by character)
V - puts you in visual mode (selecting by line)

d - deletes selected text (the text is removed from the document and copied to the buffer. Think of this command as cutting.)
y - yanks selected text (the text is not removed from the document, but it is copied to the buffer. Think of this command as copying.)

Inserting Text

i - inserts text before the cursor
a - inserts text after the cursor

I - inserts text at the beginning of the current line
A - inserts text at the end of the current line

o - inserts text in a new line below the current line
O - inserts text in a new line above the current line

r - replaces the character at the current cursor position with the next character you type, and then returns to command mode
R - replaces existing text with what you type until you press esc

p - puts yanked or deleted text at current cursor location (Think of this command as pasting.)

Deleting Text

Deleting text removes it from the screen and copies it to the buffer. Deleting, like many other commands, can be combined with numeric or symbolic options to make more complex and efficient commands like those seen below.

x - deletes the character under the cursor
X - deletes the character before the cursor
xx - deletes x number of characters starting at the cursor and going forward
xX - deletes x number of characters starting before the cursor and going backward

dw - deletes current word
dd - deletes current line
xdd - deletes x lines
dxw - deletes x words
d0 - deletes from current position to beggining of current line
d$ - deletes from current position to end of line (same as D)
dG - deletes from cursor to the end of the buffer
d1G - deletes from cursor to the beginning of the buffer
ddp - swaps the current line and the line beneath it

Changing Text

Changing text is much like deleting it except when changing text you automatically enter insert mode after executing the command.

cw - change the text from the current cursor position to the end of the word
c2b - change the text from the current cursor position back two words
c$ - change the text from the current cursor position to the end of the line
c0 - change the text from the current cursor position to the begginning of the line

Modifying Text

U - changes all selected text to uppercase
u - changes all selected text to lowercase

> - indents selected text
< - unindents selected text

J - joins the current line and the next line together to make one line of text

Marking Text

It is often helpful to be able to set markers in a piece of text so that you can both come back to that spot or refer to it with another command. The command to do this is m followed by the name of the mark, like so:


To return to that location, or to refer to it, use the apostrophe character followed by the mark variable, like this:


Searching For/Replacing Text

/x - looks for x in the current file
n - finds the next instance of x
N - finds the previous instance of x
fx - finds and moves forward to x in the current line
Fx - find and moves backward to x in the current line
; - continues forward to the next instance of x in the current line
, - continues backward to the next instance of x in the current line

Basic Format for Substitution Command:


s - indicates that you want to substitute
x - old string (the thing you want to replace)
y - new string (the thing you are replacing it with)
o - options (see below)


g - global (substitutes all occurances on the line)
c - confirmation (prompts you for confirmation on all substitutions)
i - ignore case (ignores case when searching)


:/s/good/great/gci - substitutes good with great on the whole line and both ignores case and prompts for confirmation
:12,100/x/y/gc - substitutes x with y for within lines 12-100 and confirms each replacement
:/%s/great/better/i - substitutes great with better for the whole file and ignores case

You can also use Visual Mode to select text and then type : to take you to a special prompt, which looks like this:


At the prompt, type:


When you hit enter, every instance of x in your selection will be substituted with y.

Other Useful Commands

~ - changes the case of the letter under the cursor

. - repeats the last command given

K - looks up the word your cursor is on when you press it and displays the results

Executing Shell Commands from within vim

You can execute shell commands from within vim by simply entering :! while in command mode. So, by entering the following command you can compile the file named test_app.c:

:!gcc test_app.c

A shortcut for compiling the current file, by the way, is:

:!gcc %

Getting Help

Entering help at the colon prompt will give you access to the available online help. The documentation available is quite extensive.

Feedback received on this FAQ entry:
  • Your substitution examples are wrong. :/s/good/great/gci should be :s/good/great/gci :12,100/x/y/gc should be :12,100s/x/y/gc :/%s/great/better/i should be :%s/great/better/i

    2011-03-07 20:43:33

  • The link above is DEAD... http://www.vim.org/ should at least replace it... also, for the vi nuts http://code.google.com/p/macvim/ is a GUI version... ;)

    2011-01-25 10:55:18 (Da Geek Kid See Profile)

Expand got feedback?

by Daniel See Profile edited by tmpchaos See Profile
last modified: 2011-03-07 20:51:23