vim modes - the three modes of vi/vim

The vi editor can be a little difficult to learn, so I've been writing some vi tutorials here recently. One of the first things to know about vi is that it typically functions in three different modes:

  1. Command mode
  2. Insert mode
  3. Last line mode

Here's a quick description of each vi mode.

Back to top

vi command mode

When you first start editing a file with the vi editor you will be in vi command mode. In this mode you can issue many vi commands, including commands like insert, append, and delete, and other search and navigation commands that let you move around your file.

Possibly the most important thing to know is that when you're in command mode you can't insert text immediately. You first need to issue an insert, append, or open command to insert text. These commands are actually fairly simple, and I've documented them in this vi insert commands tutorial.

Back to top

vi insert mode

Once you issue a vi insert, append, or open command, you will be in vi insert mode. If you're working with a modern vi or vim implementation, your vi editor is typically configured to show the current mode of operation, so when you go into insert mode, you'll see a text string like this on the last line of your vi editor window:

-- INSERT --

At this point you can (a) type text into your file and (b) use the arrow keys to navigate around your file just as you would do with any other text editor. (There may be some complications with older Unix systems, like HP-UX systems, but this statement is generally true.)

A very important concept to know is that when you're in vi insert mode, but you want to switch back to vi command mode, you easily move back to command mode by pressing the [Esc] key. This command is so important, I'll show it again:

[Esc]

This command is very common, and I often see expert vi users press the [Esc] key several times in a row. They usually do this (a) to be sure they hit the key and they're really back in command mode, and (b) to hear the beep from the computer, which happens when you press the [Esc] key when you're already in vi command mode. This seems to serve as a form of feedback which assures them that they're in command mode.

Back to top

vi last line mode

The last vi mode is known as vi last line mode. You can only get to last line mode from command mode, and you get into last line mode by pressing the colon key, like this:

:

After pressing this key, you'll see a colon character appear at the beginning of the last line of your vi editor window, and your cursor will be moved to that position. This indicates that vi is ready for you to type in a "last line command".

From this vi command prompt you can do all sorts of really amazing things. You can do simple things, like quitting your vi session, like this:

:q

or this:

:q!

or this:

:wq

From last line more you can also perform some amazing vi search commands or vim search and replace commands. Another cool thing is that you can issue Linux or Unix commands from within your vi editor session, like this simple ls command:

:!ls

It's really handy sometimes to be able to stay in your vi editing session but still be able to run Unix or Linux commands.

And finally, you can also issue many vi configuration commands, such as this command that tells vi to show lines numbers in your current editor window:

:set shownumber

There is a ton of power in this vi last line mode, and I've tried to share pieces of this power in a variety of different vi tutorials. (Just search this blog for "vi" or "vi editor" and you'll find a wealth of vi tutorials.)

One last note about the vi last line mode: If you're in last line mode, and you want to switch back to command mode, there are several different ways to do this. For consistency, one way to do this is to press the [Esc] key twice, like this:

[Esc][Esc]

(This is consistent with the method of moving from insert mode back to command mode, except you have to press the [Esc] key twice.)

A second way is to press the [Backspace] key until anything you typed and the initial ":" character are gone. At this point you'll be back in command mode.

Finally, if you haven't typed anything at all, and you're just looking at the ":" prompt on the last line, you can just press [Enter], and you'll be placed back in vi command mode.

Back to top

Last note: Showing your current vi mode

As a final vi/vim note, if you're working on an older Unix system, or your current system doesn't show the "-- INSERT --" prompt when you switch to insert mode, you can issue a vi configuration command to show the current mode of operation. This is a typical "vi set" command, and you issue it like this:

:set showmode

As you can see, this is a vi last line mode command, and it tells vi to show its current mode of operation. Really, all this does is show the "-- INSERT --" line when you're in vi insert mode, but that little piece of information is surprisingly helpful.

This little piece of information makes it easy to distinguish the three vi modes, because in insert mode you see that text on the last line; in command mode you see nothing on the last line; and in last line mode you see the ":" on the last line.

If you find that you need to issue this command, I need to note that it only takes effect for your current vi editing session. If you want to have vi always work like this, you'll need to save this command in a configuration file, which I begin to describe in this vi vimrc map macros tutorial.

Back to top

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.