Vim Hints 003 (HPR Show 1734)

Dave Morriss

Table of Contents

In this episode I want to look at how you move around the file you are editing in Vim. I also want to add some more elements to the configuration file we started building in the last episode.

Moving Around

One of the powerful features of Vim is the ease with which you can move around a file.

Simple movement

Some of the basic movements in Normal mode are:

Key Action
l or cursor-right Move right
k or cursor-up Move up
j or cursor-down Move down
h or cursor-left Move left
$ or End key Move to the end of the line
0 or Home key Move to the start of the line
^ Move to the first non-blank character of the line
- Move up to first non-blank character
+ Move down to first non-blank character

Note: In the Vim documentation there is an alternative annotation for these keys (and many others):

Vim Annotation Key
<Up> cursor-up
<Down> cursor-down
<Left> cursor-left
<Right> cursor-right
<Home> home
<End> end

We will use this form of annotation in these and future notes. These will also be important when we look at customisation.

If a key is used in conjunction with the Shift or Control (CTRL) keys the annotation is shown as <S-Right> (shift + cursor-right) or <C-Right> (CTRL + cursor-right).

Some of these motion commands hardly seem different from what is available in other editors. Many presses of the right cursor key will move the cursor to the right a number of columns in most editors. However, Vim allows these keys to be preceded by a number. So, typing:


will move the cursor 10 characters to the right, as will 10<Right>.

The same goes for 10h or 10<Left>, 10k or 10<Up> and so forth.

The only movement commands in this group which do not take a count are 0 / <Home> and ^.

The next movement commands (used in Normal mode) move the cursor in relation to words in the text. There two definitions of "word" in this context. We will use the Vim convention in these notes and refer to them as word and WORD.

These are the definitions from the Vim documentation:

word : a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, end of line). An empty line is also considered to be a word.

WORD : a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

Key Action
w or <S-Right> Move forward to the start of a word
W or <C-Right> Move forward to the start of a WORD
e Move forward to the end of a word
E Move forward to the end of a WORD
b or <S-Left> Move backward to the start of a word
B or <C-Left> Move backward to the start of a WORD

These movement commands may be preceded by a numeric count, as before, so 5w or 5<S-Right> will move the cursor forward by 5 words to the start of the 6th word from the current position.

The following list shows the effects of various word-related movements moving the cursor along the example log record. It contrasts the use of word versus WORD commands. The ^ characters represent the cursor positions after the various commands. All commands begin moving from the F of FAT. The last two move to the right 80 columns then backwards.

  FAT-fs (sdh): utf8 is not a recommended IO charset for FAT filesystems, filesystem will be case sensitive!
5w           ^
5W                          ^
7e                 ^
7E                                      ^
80l5b                                                ^
80l5B                                        ^

If this is unclear then here are the effects of these commands in text form:

  • 5w moves forward to the closing bracket
  • 5W moves forward to the first lower case 'a'
  • 7e moves forward to the '8'
  • 7E moves forward to the last 'd' of 'recommended'
  • 80l5b moves forward 80 columns to the right to the second 'e' of 'filesystem' then backwards to the 'f' of 'for'
  • 80l5B moves forward 80 columns to the right then backwards to the 'c' of 'charset'

There are many more movement commands which we will look at in forthcoming episodes.

More configuration settings

In the last episode we looked at some of the basic elements of the configuration file. So far the file contains the following:

set compatible
set backup
set undodir=~/.vim/undodir
set undofile

We can now add some more settings.

Adding a ruler

Vim will display a ruler at the bottom of the screen if this option is enabled. Add the following to the configuration file:

set ruler

This causes the line and column number of the cursor position to be shown at the bottom right of the screen, separated by a comma. When there is room, the relative position of the displayed text in the file is shown on the far right.

The relative position is Top when the first line of the file is visible, Bot when the last line is visible, All when both top and bottom lines are visible, and if none of the foregoing, N%, the relative position in the file.

The command can be abbreviated to se ru. I prefer to use the full form because it is easier to remember what it means!

The ruler can also be turned off with set noruler which you would prefix with a colon while in a Vim editing session to enter command mode:

:set noruler

It is possible to customise the contents of the ruler, but we will not be looking at this for the moment.

Note that some Linux distributions set this option for you. I run Debian Testing and a set ruler definition can be found in /usr/share/vim/vim74/debian.vim. It is a good idea to set it in your configuration file regardless, however, because you might need to transfer this file to another distribution in the future,

Adding a status line

By default the Vim window uses the whole terminal window except for the last line, as we saw in episode 1. The last line is used for displaying various messages, and the ruler, and for entering ":" commands.

It is possible to separate the status information from the command entry line with the following option:

set laststatus=2

This creates an inverse colour status line at the bottom of the screen followed by the command entry line. The status line contains the name of the file being edited, and the ruler (if enabled). The final line contains messages and is where commands are entered.

If the terminal you are using is small (like the 24 line by 80 column hardware terminals Vi was originally written for), stealing these lines from the Vim workspace may be a problem. In today's world it's unlikely to be so, and I always enable these.

This command can be abbreviated to se ls=2.

The status line can also be turned off with set laststatus=0.

Showing the mode

As we have seen, Vim is a modal editor with several modes, some of which we have yet to look at. By default, Vim does not indicate which mode it is in, but the following command in the configuration file will change this:

set showmode

As with set ruler, some Linux distributions set this for you, but I believe in setting this myself.

With showmode enabled a message such as -- INSERT -- (insert mode) is shown on the last line of the Vim window.

This command can be abbreviated to se smd.

The mode display can also be turned off with set noshowmode which you would prefix with a colon while in a Vim editing session to enter command mode:

:set noshowmode

Adding comments

The comment character used by Vim in configuration files and elsewhere is the double quote character '"'. See the summary below for an example.


The following screenshot shows Vim in an xterm window (24x80) editing the notes for this episode (written in enhanced Markdown, to be processed with pandoc). The configuration file used is the same as that shown below in the summary.

Vim screenshot
Picture: Vim with ruler and status line


  • Movement
    • h, j, k, l or cursor keys
    • $ or <End>
    • 0 or <Home>
    • ^, - and +
    • w or <S-Right>, W or <C-Right>
    • e, E
    • b or <S-Left>, B or <C-Left>
  • Configuration file - this time with comments
" Ensure Vim runs as Vim
set nocompatible

" Keep a backup file
set backup

" Keep change history
set undodir=~/.vim/undodir
set undofile

" Show the line,column and the % of buffer
set ruler

" Always show a status line per window
set laststatus=2

" Show Insert, Replace or Visual on the last line
set showmode
  1. Vim Hints Episode 1
  2. Vim Hints Episode 2