Detailed explanation of the three working modes of Linux Vim (command mode, input mode and editing mode)

All content in a Linux system is stored in the form of files. When changing file contents on the command line, a text editor is often used.

Our text editor of choice is Vim. When using Vim to edit files, there are three working modes, namely command mode, input mode and editing mode. These three working modes can be switched at will, as shown in the following figure:

Vim command mode

When using Vim to edit files, you are in command mode by default. In this mode, you can use the direction keys (up, down, left, right keys) or k, j, h, i to move the cursor position, and you can also copy, paste, replace, delete and other operations on the file content.

The figure below shows the state of Vim in command mode in CentOS 6.x system:

Vim input mode

In input mode, Vim can perform write operations on files, similar to typing in a document on a Windows system.

The way to put Vim into input mode is to enter i, I, a, A, o, O and other insertion commands in the command mode (the specific functions of each command are shown in the table below). When the file editing is completed, press the Esc key. Return to command mode.

Specific functions of each insert command
shortcut key Function description
i Inserts text entered subsequently at the current cursor position, and the text behind the cursor moves to the right accordingly.
I Insert the subsequently entered text at the beginning of the line where the cursor is located. The beginning of the line is the first non-blank character of the line, which is equivalent to moving the cursor to the beginning of the line and executing the i command.
o Inserts a new line below the line under the cursor. The cursor stops at the beginning of the empty line, waiting for text input
O Inserts a new line above the line where the cursor is. The cursor stops at the beginning of a blank line, waiting for text to be entered
a Inserts subsequently entered text after the current cursor position
A Insert the subsequently entered text at the end of the line where the cursor is located, which is equivalent to moving the cursor to the end of the line and then executing the a command.

Vim’s editing mode

Edit mode is used to perform operations such as saving, finding, or replacing specified content in a file.

The method to switch Vim to the editing mode is to press the ":" key in the command mode. At this time, a ":" symbol appears in the lower left corner of the Vim window, and you can then enter relevant instructions for operation.

After the instruction is executed, Vim will automatically return to the command mode. If you want to return directly to command mode, just press Esc.

The following figure shows the state of Vim after entering editing mode:

For newbies, it is often not clear what mode they are in. Whether you forgot or accidentally switched modes, you can press the Esc key once to return to command mode. If you hear a "beep" sound after pressing the Esc key several times, it means you are already in command mode.

Dark Horse Programmer's new version of Linux allows you to quickly get started with zero basic knowledge and then become proficient in it, covering all aspects of Linux system knowledge, deployment of common software environments, Shell scripts, cloud platform practices, and practical big data cluster projects, etc.

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/131301497