Vim is often described online as having a “steep learning curve” and a “high barrier to entry” and so on. Therefore, many people are afraid of Vim after they have a little understanding of it. They are afraid of affecting work efficiency or think that the input is not cost-effective compared with the output, so they do not really enter Vim.

Sure, Vim isn’t as handy as other editors, but it’s not nearly as scary. Spend an evening familiarizing yourself with the basics of a dozen commands, and install aVim plug-in on one of your favorite editors, such as Vscodevim or IdeaVim, to get Vim started.

After reading this introductory article, take an hour to familiarize yourself with what’s in this article, then go back to where you normally write code, put down your regular editor, and start editing with Vim. You’ll find that your first day of switching to Vim is not less productive, and may even be slightly more productive.

Vim opens the door and the learning curve is gentle and gentle. Edit Text at the Speed of Thought!

Create a new text file and fill it with some useless code or text. This is where you will practice using Vim.

Open the text file with VIm on the terminal, or activate the editor’s VIM mode and open the file through the plug-in mentioned earlier.

It is strongly recommended that you read this article while operating, the operation process is also the process of memory, read this article basically you have memorized a 7, 8, 8, a lot of commands immediately can be handy.

start

Right now, we’re in Vim’s normal mode.

Normal mode

In normal mode, you cannot edit text directly by typing, but you can move the cursor or execute commands.

Normal mode is Vim’s default mode and its most natural state of relaxation, which is what sets Vim apart from other editors.

As you learn more about Vim, you begin to understand why Vim is the way it is.

Move the cursor

In Vim, we can move the cursor by pressing four keys J ↓ K ↑ H ← L →.

J k L is right under the index, middle and ring fingers of the right hand, and the palm can be pressed without moving.

H is located on the left side of the index finger of the right hand, which may be inconvenient when you just touch Vim. However, after mastering some commands mentioned below and some more complex operations, you will find that most of the operations requiring more than one consecutive h or L press can be achieved by other commands. The ones that are used the most, are the two above and below jk.

Insert mode

Press I in normal mode to enter insert mode.

In insert mode, Vim works much like a regular editor, using arrow keys to move the cursor and typing on the keyboard.

With cursor movement and simple typing, you can edit text in a very basic way.

However, the power of Vim has yet to be fully revealed. Here’s what makes Vim efficient for text editing.

More actions to move the cursor

The aforementioned HJKL can only move the cursor one character at a time, but the following actions can make the cursor jump.

B and W move to the beginning of the front and back words respectively, representing backward and forward one word.

E ge moves to the end of the last word and the first word respectively, representing end.

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

^ (shift 6) is similar to 0, but moves to the first non-null character on the current line.

The command use
b mobileTo the current/previous wordAt the beginning
w mobileTo the next wordAt the beginning
e mobileTo the current/next wordAt the end
ge mobileGo to the last wordAt the end
0 The beginning of a line
$ End of each line
^

< 6 > ⇧
The first non-whitespace character

delete

Let’s look at the delete operation.

X deletes characters under the cursor and x deletes characters in front of the cursor.

D stands for delete in Vim, and deletion in Vim is accomplished by pressing D and adding the range to be deleted.

The range of deletions can be seen in the table below.

D + arrow key deletes a character in the corresponding direction (up or down deletes a line).

Incidentally, in Vim’s normal mode, the command can be executed n times by typing the number N followed by the command, for example, d5j, which means deleting 5 lines down.

The previously mentioned commands to move the cursor by word and to the beginning of the end of a line can also specify the range of deletions, such as db from the beginning of the cursor to the beginning of the word and D $from the cursor position to the end of the line.

To delete an entire line, press the dd command twice. Similarly, you can delete multiple lines at a time using a numeric command.

In addition, there are daw equal to delete a word, delete a word, and d{n}w equal to delete n words, delete multiple words.

The command use
dh Delete the previous character (D please)
dl Delete a character (equivalent tox or D -)
d5j Remove 5 lines
—- ————————
db Remove the cursor from the beginning of the word
d$ Deletes from cursor position to end of line
—- ————————
dd Delete 1 row
{n}dd Delete n lines
—- ————————
daw Delete a word
d{n}w Delete n words

Pattern into insert mode

Whereas the aforementioned I simply goes into insert mode at the current cursor position, Vim provides commands that allow us to move to the target position and insert more quickly.

A stands for append append, which goes into insert mode after the current cursor position.

Pressing O in normal mode creates a new line under the line where the cursor is currently located and enters insert mode, representing open.

S removes the character under the current cursor and enters insert mode.

These three commands also have matching uppercase commands to perform similar operations.

A will enter insert mode at the end of the current row, O will create A new row above the current row and enter insert mode, and S will delete the entire row and enter insert mode. I, which also has a matching I, goes into insert mode at the first non-null character of the current line.

The command Enter the position of insert mode
a In the currentAfter the cursor
A In the currentThe end
i At cursor position
I At the first non-null character of the current line
o inUnder thesurfaceInsert a new row
O inonsurfaceInsert a new row
s Deletes the character under the cursor
S Delete the entire line

Cancel the operation

Accidentally deleted text should not be deleted how to do? In Vim, u stands for undo, which can undo the most recent operation.

Command mode

If you’re editing from a terminal using vim commands, you’ll probably have to deal with vim command mode a lot.

Press: to enter the command mode of Vim, enter the command and press Enter to perform the corresponding operation.

:w stands for write and :q stands for quit.

You can also enter more than one command at a time, for example :wq – Save and exit.

Add after command! For example, q! – Forced exit without saving.

All in Vim!

Now that you’ve mastered the basics of insert, delete, and undo, you’re ready for simple everyday tasks.

In the next article, we’ll look at some more advanced actions to take your Vim to the next level.