Skip to the content of the web site.

3. Advanced navigation

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

We have described how to navigate through the document with hjkl and through searching. In this lesson, we will cover a few more techniques.

Warning: Under no circumstances try to memorize these. Instead, you should just familiarize yourself with them. Then, when you start using vi there will come times where you will want to do something which is quite frustrating with the basic commands you learned. At that point, you can come back to this page and learn the more efficient means of accomplishing the same goal.


1. Moving forward or backward by screens or half screens or moving within the current screen

The following four commands move forward or backward in the current document or move to a relative position on the current screen.

1.1 Jumping by half a screen: Ctrl-U Ctrl-D

Going up and down your document moves forward or backward by half a screen. When editing software, this is often useful, as what you're looking for is in the second half of the screen, but you'd like to see a little more. In this case Ctrl-D is the desired movement.

If in command mode you enter a number and then press Ctrl-U or -D, the document will move up or down that many lines, but leaving the new line in the same place on the screen as where you started. This is subtly different from if you type that same number and then press k or j, respectively.

1.2 Jumping by full screens: Ctrl-F Ctrl-B

Moving forward and backward in the document moves forward and backward by a full screen with the last line of screen ending up as the first line of the next screen forward, or the first line ending up as the last line of the next screen backward.

If in command mode you enter a number and then press Ctrl-F or -B, then you will go forward or backward by as many full screens, respectively.

1.3 Move to the top, middle or bottom of the screen: HML

If you press the H key, you move to the first line of the current screen, the M key moves you to the middle of the current screen, and the L key moves you to the bottom of the current screen.


2. Moving within the document

The following commands move to arbitrary points in the document.

2.1 First line of the document: gg

Typing gg immediately moves you to the first line of the document.

2.2 Last line of the document: G

The key G always moves you to the last line in the document.

2.3 nth line of the document: nG

We have seen how :n moves you to the nth line of the document. Another means of moving to that line is nG.


3. Moving to the start or end of the next or start of the previous word

The following six keys jump forward or backward by words in the document.

3.1 Move to the start of the next word: wW

Instead of using l to move forward, you can jump forward by an entire word at a time by using the key w in command mode. The cursor will appear at the start of the first character of the next word. If you experiment this you will see why the word "word" appears in italics. A word is defined either as a contiguous sequence of non-white-space characters (spaces, tabs, or Enter) of:

  • alphabetic characters, numbers or the underscore (_), or
  • punctuation characters (all characters not in the first category).

The key W moves forward to the next non-whitespace character.

3.2 Move to the start of the previous word: bB

In the same sense that w moves to the start of the next word, the key b moves the start of the previous word.

In a similar sense, the key B moves to the start of the previous sequence of non-whitespace characters.

3.3 Move to the end of the next word: eE

In the same sense that w moves to the start of the next word, the key e moves the end of the next word.

In a similar sense, the key E moves to the end of the previous sequence of non-whitespace characters.


4. Searching within a line

These commands allow you to quickly search for a character in the current line. These commands will not move the cursor beyond current line. If the character being searched for cannot be found in the direction of search, a bell will sound.

4.1 Finding the next instance of a character: fF

In command mode and press f followed by any character, the cursor will move forward to the next instance of that character.

If you press F followed by any character, the cursor will move backward to the previous instance of that character.

What do you think happens if in command mode you press a number and then press f or F followed by any character? Try this out.

4.1 Jumping just before the next instance of a character: tT

In command mode and press t followed by any character, the cursor will move forward to the character immediately before the entered character.

If you press T followed by any character, the cursor will move backward to the character immediately following the entered character.

What do you think happens if in command mode you press a number and then press t or T followed by any character? Try this out.

Repetition of these searches: ;,

Once you have performed one of these searches within a line, you can repeat the same search in the same direction by pressing ;

If you want to perform the same search but in the opposite direction, you may do so using ,.

What do you think happens if in command mode you have made a search within a line for a character, and then press a number after which you press ; or ,? Try this out.


5. Find matching delimiters: %

If you are programming in C, C++ or Java, or any other programming language that uses parentheses, brackets, angled brackets or braces as delimiters, it may be useful to find the matching closing bracket for a given opening bracket, or the matching opening bracket for a given closing bracket.

Move the cursor to an opening delimiter in a C++ source file and then press %. If there is a matching closing delimiter, the cursor will jump to it. If it cannot find a matching closing delimiter, a bell will sound.

Similarly, if you move the cursor to a closing delimiter in a C++ source file and press %, the cursor will search for a matching opening delimiter, and if one is found the cursor will be moved to that location.

If you are not on a delimiter, vi will search forward for the next delimiter of any style and immediately jump to its matching delimeter.


6. Moving by paragraphs: {}

In vi, a paragraph is separated by one or more blank lines; that is, lines that do not contain any characters, including no whitespace. To jump forward to the next blank line, press } while to jump to the previous blank line, press {. If there are multiple blank lines one after the other, vi will treat these as a single paragraph separator.


7. Moving the screen but leaving the cursor alone

Suppose you are on a line and you want to see what is beyond the top or bottom of the screen, but you don't want to move the cursor from where it currently is. In this case, Ctrl-E will reveal one more line at the bottom of the screen, while Ctrl-Y will reveal one more line at the top of the screen.