Exploring the Vim Magic, Part 1 The Interface

In the previous post, I covered some reasons for using Vim. I think even if you work on Windows, you will discover that when working with Git, especially its bash shell, it comes with vim as its editor–which means if I never convinced you the last time, at least you may be interested this time around.

The Interface

To launch vim, you just type vim on the terminal. If it is not installed, you can still use vi or any of its clones.

After launching it, you will be surprised by its behaviour:

  • While it is a text editor, pressing keys will not result in characters being typed. Instead something else might happen.
  • Trying to quit the editor with say, q, may not do anything. Thus, even for an action like quitting, you find yourself stuck.

So what is happening here?

Introducing the Vi Modes

This has to do with vim’s layout. Vim has a concept of modes, which means a switch of functionality based on what you want.

The two modes you will work with most of the time are the command and the insert modes.

The Command mode
This is the mode in which you issue commands to vim. You just type a single key, usually the first letter of a command to get a particular action. For example d for delete. This is the initial mode you get to.
The Insert Mode
This is the mode where you use Vim like other text editors: in this mode, keys result in text entry. You get to the insert mode by hitting i or any of its variants that we will discuss below.
The ex mode
This is the mode you get into when you want to trigger the ex commands. You get into the ex by pressing the colon (:) key, then type the command you like. Most people get introduced to the ex via the quit and save commands. We will cover the ex mode in the next post.

Working in the Command Mode

At this stage, the most important keystrokes you may need to know are for moving around.

The command mode, which is sometimes called the normal mode, is what you get to soon after launching vim. In this mode, every keystroke you press is a command to vim to do something.

While you can press the usual up, left or Down and Right arrow keys, it is customary to use the keys on the main typing grid as follows:

To move to the left or right
Press the h to move left, and l to move to the right.
Line movement
Press the j to go to the next line, or k to go to the previous line.
Word movement
To move by word, press w to go to the next word, or b to go to the previous word. To go to the end of the next word, press e.
Paragraph movement
Press { to go to the start of a paragraph, or } to go to the end of a paragraph. the curly braces are produced by pressing the round bracket keys along with the Shift key.
Line Movement
Press 0 to go to the beginning of a line. Or use the caret symbol to go to the beginning of the text on a line. Press the dollar sign to go to the end of the line. These keystrokes are just like those in the regular expression syntax. For the caret symbol, you press Shift+6 on the numeric row. As for the dollar sign, you press Shift+4.
Document movement
Press g to go to any line in the document, or the percent sign to move to a particular percentage., Thus 10g will go to the 10th line. To quickly move to the document anchors, press gg (Thus double g in small caps) to go to the beginning of the document. Press uppercase G to go to the end of the document.

Working in the Insert Mode

When you are ready to enter text, that is to have vim behave like other text editors, just press the i key or any of its variants.

When you are in the Insert mode, any keystroke you type will produce text as usual. If you want to get back to the command mode, you just press the Escape key.

Ways of getting into the insert Mode

Because i command means “Insert”, it is easy to begin with this one. But if you are familiar with say Python’s list methods, you will soon realise that “insert” has to do with a given position.

This is the same with Vim: while the differences may be small, but it is important to see how getting into the Insert mode affects your text entry.

The table below is going to list the various insertion points via the letters pressed from the command mode:

Letter Meaning Position of insertion relative to cursor
i Insert Insert To the left of cursor
a append Insert text to the right of cursor
I insert Insert text at the beginning of the line
A append Insert text at the end of the current line
o open line Insert a new line below the current line
O open line Insert new blank line just above the current line

So pressing any of the letters in the first column in the table above will result in you getting into the insertion mode.

Quitting Vim

Now we want to stop here for this exploration. The obvious question is How do we quit this thing?

To quit, first make sure that you are not in the Insert mode. If you’re not sure, just press the Escape key.

Once you are in the command mode, press ZQ to quit and discard any changes you might have made to a text file, otherwise press ZZ to quit and save your changes. You can remember these keystrokes because Z is the last letter of the alphabet, and in regular expression, it often means the end of a search string.

In the next instalment when discussing the ex mode, we will discuss of yet another way to quit Vim. But as I said, the command mode is all about issuing commands in single or compound letters without any prefix.

Conclusion

While the vim keystrokes may seem difficult at first, once you get the hang of it, you will find them intuitive. Remember that you have to pay attention to the case of the letter you type, especially in Command mode.

In the next instalment, I will look at the ex mode, and the power behind its commands.