For most people, VI/Vim is just an extremely difficult text editor in a terminal.

introduce

Let’s start with an interesting graph:

Because of its steep learning curve, VI is extremely polarised, being called the god of editors by one group and ** by those who use other editors.

Weird way to operate

Vi uses countless strange key combinations to do everything you can expect from an editor.

In other editors, we usually use CTRL + C/V to copy and paste, CTRL + S to save, and use ↑↓←→ or the mouse to locate the cursor position.

In VI, every key on your keyboard is a shortcut.

Also, don’t try to use the mouse in VI.

Simple interface

This interface will probably discourage most people who want to learn VI. It’s so easy, it feels like you don’t know where to start.

Also, if you’re using VI for the first time, you might wonder: Why can’t I control the cursor? Why can’t I type what I want?

Conventions about key description

This article uses the characters in the square extension [] to identify the key you need to press.

Press A key on the keyboard alone, and it will be marked as [A], while press SHFT +A key will be marked as [A], and similarly, press SHFT +4 will be marked as [$].

If you press the A key twice in A row, it is marked as [AA].

The text start

From now on, do not use the ↑↓←→ or even the backspace key.Copy the code

Insert mode and normal mode

I’ll start with two keys that are extremely important in VI: Esc and the lowercase I.

After pressing I and Esc, see what happens?

After pressing the letter I, vi enters Insert mode, in which all your input is written to the text as is.

Esc reverts from insert mode to normal mode, where your entire keyboard is a shortcut.

Move the cursor

Moving the cursor in VI should be in normal mode. If you're not sure what mode you're in, press Esc.Copy the code

Vi shortcut key design has the following two habits:

  • Use as few keystrokes as possible
  • Try to keep your hand always in the main key area

Therefore, it is absolutely impossible for you to reach the ↑↓←→ key that is so far away.

Find the letters next to each other on your keyboard: [h] [j] [k] [L].

H and J are moving left and right, jk is moving up and down.

Isn’t it too slow to always move one by one?

Use the number [0] and the character [$].

0 moves to the beginning of the line and $moves to the end of the line.

And up and down?

Try pressing g twice in a row.

That’s right, press [gg] to move to the first line of the file, and uppercase [G] to move to the last line.

Or not enough? Rest assured there’s more to come.

There is also a very useful key: [%], enter a few brackets in your VI, to enter normal mode press [%] to see how it movesCopy the code

delete

Similarly, deletion needs to be in normal modeCopy the code

So far we’ve learned how to move the cursor to where you want it. Next, learn how to delete unwanted characters.

Try lower case [x] and upper case [x] : x deletes a character under the current cursor, and large x deletes the character before the cursor.

However, only one character can be deleted by pressing [x] once. If you want more flexible deletion, you need to use [d]

Use [d] + position to delete, e.g. [d$] deletes everything from the current cursor to the end of the line.

To delete an entire line, press the lowercase letter D: [dd] twice.

Copy and paste

Copying requires multiple keys, which makes sense:

First, press [y] to tell VI that you want to copy from the current cursor position. Then, you need to tell VI where to copy to.

For example, if we copy only the current character, press the key [L] used to move the cursor to the right.

Do this by copying the current character with [yl].

Paste is very simple, directly press the [P] key to complete.

For copying, there is another common key combination: [yy], for copying the current line.

Undo, undo, and replay

Undo the previous step is [u], and the corresponding reverse undo is [u].

Replay is to repeat the last operation and press [.]. This feature works great in some cases!

Substitution, case conversion

Replace a single character by pressing [r] and then entering the character to replace. For example, replace the character under the current cursor with a: [ra]

However, [r] can only replace a single character. What if you want to replace multiple characters? Try pressing capital [R] to go into replacement mode! Press Esc to return to normal mode.

Turn upper case to [gU] and lower case to [gU]. Press and enter the end position of the upper/lower case.

For example, to uppercase the current position to the end of the line, press [gU$].

Almighty number

In normal mode, almost all keys can be preceded by a number to indicate n repetitions.

Such as:

  • [10K] : Move the cursor up 10 lines
  • [2p] : Paste twice
  • [5x] : Delete 5 characters left
  • [5YY] : Copy the cursor 5 lines down
  • [2U] : Undo two steps
  • . .

With this in mind, we can put together some more gameplay:

  • [gU2l] : Move the cursor two letters to the right to uppercase
  • [d2j] : Deletes 2 lines down from the current cursor
  • [yG] : copies everything from the current line to the last line
  • . .

To sum up: we can see that in VI, there is a design where operator + action command = operation.

Move the cursor faster

[w] : Move right to the beginning of the next word

[B] : Move left to the beginning of the last word

[e] : Move right to the end of the next word

Save and Exit

In fact, save and exit belong to command mode.

First press Esc to return to normal mode.

Press [:] to enter the command mode. In this mode, press W again to save, that is, [:w], and press q to exit.

[:wq]

other

Here only introduces the most basic part of vi, some extended keys such as [A], [f] [O], etc., and even visual mode, command mode, find, macro, etc.

What’s more, another fascinating aspect of VI is its great customizability and extensibility.

The next article, if any, will cover some advanced uses of VI.