Quickly master the basic use of the vim editor

Table of contents

1. Introduction

Two or three modes

3. File opening, saving and closing

 Fourth, edit the file

1. Move the cursor

2. Delete and restore characters or lines

3. Search

4. Jump to the specified line

5. Set the line number

6. Copy and paste

7. Replacement


1. Introduction

The vim editor is a powerful editor in the Linux operating system. It has improved and added many features on the basis of the Vi editor, such as support for regular expression search, multi-file editing, etc.

This article will introduce the basic use of the vim editor to help novices quickly master and use it.

Two or three modes

Vim includes three modes: edit, command, and last line.

Edit mode: mode for editing content, including input, modification and deletion

Command mode: The default mode when entering vim. In this mode, you cannot input content to the file. The input on the keyboard defaults to a command

Last line mode: You can enter many commands, such as saving files, character replacement, etc.

Switch between different modes:

3. File opening, saving and closing

1. Vim is followed by the file path and file name. If the file exists, open the edit file window. If the file does not exist, create the file

vim "file path and file name (with suffix)"

Open and enter the command mode, as shown below:

Enter "i" and press Enter to enter edit mode

i //Add text to the left of the current cursor position

I //Add text at the beginning of the current line (the beginning of a non-null character)
A //Add text at the end of the current line
a //Add text to the right of the current cursor position
O //Create a new line above the current line One line
o //Create a new line below the current line
R //Replace (overwrite) the current cursor position and several texts after it
J //Merge the line where the cursor is and the next line (still in command mode)

 Press "Esc" to exit edit mode and return to command mode

 Press ":" (colon), enter wq, save the file and exit,

 In command mode:

:w //Save the file
: w vpser.net //Save to the vpser.net file
: q //Exit the editor, if the file has been modified, please use the following command
: q! //Exit the editor without saving
: wq //Exit the editor and save the file

 Fourth, edit the file

1. Move the cursor

In command mode:

1. Use the up, down, left, and right arrow keys

2. In command mode: h for left, j for down, k for up, l for right.


3. Space bar to the right, Backspace to the left, Enter to move to the beginning of the next line, - to move to the beginning of the previous line.

2. Delete and restore characters or lines

In command mode:

x //Delete the current character
nx //Delete n characters starting from the cursor
dd //Delete the current line
ndd //Delete n lines including the current line down
u //Cancel the previous operation
U //Cancel the current line All actions for

3. Search

command mode

/vpser //Search the vpser string down the cursor
? vpser //Search the vpser string up the cursor
n //Search down for the previous search action
N //Search up for the previous search action

4. Jump to the specified line

In command mode:

n+ //jump down n lines
n- //jump up n lines
nG //jump to line number n
G //jump to the bottom of the file

5. Set the line number

In command mode:

:set nu //display line number
:set nonu //cancel display line number

6. Copy and paste

In command mode:

yy //Copy the current line to the buffer, or use "ayy" to copy, "a is the buffer, a can also be replaced by any letter from a to z, and multiple copy tasks can be completed.
nyy //Copy the current line down n lines to the buffer, or use "anyy to copy, "a is the buffer, a can also be replaced by any letter from a to z, and multiple copy tasks can be completed.
yw //Copy the characters from the cursor to the end of the word.
nyw //Copy n words starting from the cursor.
y^ //Copy the content from the cursor to the beginning of the line.
y$ //Copy the content from the cursor to the end of the line.
p //Paste the content in the clipboard after the cursor, if you use the previous custom buffer, it is recommended to use "ap" to paste.
P //Paste the content in the clipboard before the cursor, if you use the previous custom buffer Custom buffer, it is recommended to use "aP for pasting.

7. Replacement

In command mode:

:s/old/new //Replace the first occurrence of old in the line with new
:s/old/new/g //Replace all occurrences of old in the line with new
:n,m s/old/new/g //Replace with new All old in lines from n to m
: %s/old/new/g //Replace all old in the current file with new

Guess you like

Origin blog.csdn.net/weixin_47930147/article/details/127119690