vim common operations

Vim working mode

1. Three basic working modes of vim

Vim has three basic working modes: command mode, last line mode, and editing mode. For an introduction to these three working modes, please see below.

1.1. Command mode

After using vim to open a file, first enter the command mode, which is the entry mode of the vim editor. In command mode, you can perform regular editing operations on files by using commands, such as copy, paste, delete, position, page turning, etc.

1.2. Last line mode

The last line mode is the exit of the vim editor. To exit vim, you must be in the last line mode. In the last line mode, perform operations such as exiting the file, saving and exiting the file, etc.

1.3. Edit mode

Normal text editing mode.

2. Switching operation between three modes

(1) When vim opens a file, it enters the command mode. The other two modes need to return to the command mode to enter each other.

(2) In the command mode, press any letter i, I, o, O, a, A, r, R to enter the editing mode. After entering the editing mode, the words INSERT or REPLACE will appear below. Press the Esc key to exit editing. mode to return to command mode.

(3) In the command mode, press any one of:, /, ? to enter the last line mode. The last line mode can operate on the document or vim environment, such as searching, reading, saving, replacing, leaving, and displaying lines. No. etc.

3. Schematic diagram of switching between three modes

Insert image description here
As shown in the picture above, as long as we can flexibly use three keys: the escape key esc, the colon key:, and the letter key i, we can freely switch between the three modes of vim.

Vim save and exit commands

1. Vim save command

1.1, :w command, normal save

The full English name is: write. The function of this command is to write the data in the memory buffer to the file specified when starting vim. Note that this command saves the edited file content but does not exit the vim editor.

1.2. :w! command, force saving

Force save command. If the access rights of the original file do not allow writing to the file, for example, the original file is a read-only file, you can use this command to force writing.

2. Vim save and exit the command

2.1, :wq command, save and exit

The full English name is: write & quit. The function of this command is to write the data in the memory buffer to the file specified when starting vim, and then exit the vim editor.

Note: Another alternative is to use the ZZ command. ZZ is often used to describe the sound of snoring while sleeping, indicating that a person is asleep. It is also very vivid when used in vim. Exit the vim editor and let the file "sleep" without disturbing it again.

2.2. :wq! command to force save and exit

Exit the vim editor after forcing the file contents to be saved. The function of this command is to force the data in the memory buffer to be written to the file specified when starting vim, and then exit the vim editor.

3. Vim exit command

3.1, :q command

You can use this command when you are ready to exit vim without doing any editing. If editing has been done, vim does not allow the user to exit using the ":q" command, and the following warning message will be output: No write since last change (:quit! overrides)

3.2. :q! command

Force quit the vim editor and discard the results of editing processing. If you really do not need to save the modified file content, you can enter the ":q!" command to forcefully exit the vim editor.

Vim page turning command

Tip: vim paging command needs to be in command line mode. Switching between the three modes of vim
is very simple. As long as we can flexibly use three keys: the escape key esc, the colon key:, and the letter key i, we can freely switch between the three modes of vim. For more introduction, please refer to: "Vim
Working Mode"

When we use the vim editor to view a script, pressing the up and down keys to view the document content is very inefficient. Especially when encountering emergencies, mastering vim's page turning shortcut keys is very helpful for work. The page turning shortcut key commands of vim are as follows:

1. Turn the whole page command

Ctrl + f key (the full English spelling of f is: forward)
Ctrl + b key (the full English spelling of b is: backward)

2. Turn half page command

Ctrl + d key (the full English spelling of d is: down)
Ctrl + u key (the full English spelling of u is: up)

3. Locate the header and footer

To directly view the first line of the script, please enter: 0, and then press Enter; to directly view the last line of the script, please enter: $, and then press Enter.

Here is the quote

Note: $ is often used to indicate the end.

Vim find and replace

vim has a powerful string search function. When we want to search for a string under vim, we usually enter / or ? plus the string we need to search for. For example, if we want to search for the word user, we can enter: /user or ?/user, both of which The difference is that the former searches from top to bottom, while the latter searches from bottom to top.

1. vim search string command

1.1. Search from the beginning

在命令模式下,输入 :/ + 你要查找的字符,按下回车,可以看到vim把光标移动到该字符串处。

1.2. Search from the end

In command mode, enter:? + the character you want to find, and press Enter to search for a string or character.

Note: It is worth noting that "/" searches downwards, while "?" searches upwards, and in the keyboard definition, "?" happens to be the superscript of "/".

1.3. Matching up and down

By default, the search will locate the closest matching result to the current cursor. If we want to go to the next/previous search result, use the following keys:

n – Find the next match

N – Find the previous matching result

1.4. Turn off highlighting

After searching, we opened other files and found that they were also highlighted. How to turn off the highlighting?

In command mode, enter: nohlsearch. Of course, it can also be abbreviated as: noh.

2. vim string replacement

Find and replace are a common and essential set of features in any text editor. Vim uses the following command structure to implement the replacement function.

replace

:s/old/new - 用new替换当前行第一个old。
:s/old/new/g - 用new替换当前行所有的old。
:n1,n2s/old/new/g - 用new替换文件n1行到n2行所有的old。
:%s/old/new/g - 用new替换文件中所有的old。
:%s/^/xxx/g - 在每一行的行首插入xxx,^表示行首。
:%s//xxx/g−在每一行的行尾插入xxx,/xxx/g−在每一行的行尾插入xxx,表示行尾。
所有替换命令末尾加上c,每个替换都将需要用户确认。 如:%s/old/new/gc,加上i则忽略大小写(ignore)。

g: Global replacement
gc: Ask for confirmation before each replacement
gn: Ignore the replacement function and highlight the find results.

Vim save file

1. Save the file in vim and set the file name

Sometimes we directly enter the vim command to enter the editing mode. After editing the file, when saving, it will prompt: E32: No file name

The reason is that we did not enter the file name when entering, and an error was reported when exiting. How to save unnamed vim files? Please see the introduction below:

Step 1: After completing the input, press esc to exit the input mode

Step 2: Switch from input mode to last line mode, that is, enter: w + file path and file name, and then press Enter. For example: :w test.txt

Reminder: vim will not automatically create non-existing folders, we need to create them manually.

2. Vim sets the default path

As shown above, after entering: w test.txt, it will be saved in the current directory by default. You can set it to save to a defined path.

Need to add in _vimrc file:

exec "cd " . fnameescape("/usr/tom/")

Replace /usr/tom/ with the path you need, so that after creating a new file in vim, just use the command: w + file name to automatically save it to the defined path.

Deleting and clearing Vim files

dd command

1. Single line deletion

In command mode,
the dd command can quickly delete the line where the cursor is located.
d+up and down arrow keys delete the line where the cursor is and the line above and below.
d+left and right arrow keys delete one character to the left and right of the cursor.

2. Delete multiple lines of files

The d command is a very commonly used command in Vim. It can delete lines, delete characters, cut, etc. When we need to clear the contents of a file, we can use the dd command to achieve this.

Delete multiple lines

:1,3d

Delete lines 1 to 3

Delete all lines before the current line

:1,.-1d

Delete all lines after the current line

:.+1,$d

Delete all lines after the current line

:.+1,$d

Delete the lines containing the text keyword, where /reg/ is a regular expression

:g/text/d

Delete comments starting with #.

:g/^#/d

3. Delete file content

To realize the clearing of file contents
1,$d

:1,$d

The meaning of the above command is: delete all the contents from the first line to the last line, that is, clear the contents of the entire file. In Vim, indicates the position of the last line. ∗ ∗ or 1, indicates the position of the last row. **or 1,Indicates the position of the last line.or 1 , dG**

:1,$dG

The meaning of the dG command is: delete the content from the line where the cursor is to the last line, and move the cursor to the first line to clear the entire file.
Or %d

:%d

% means all lines in the file, %d deletes all lines
or command lines dG
After pressing the esc key, first press gg (reach the top), then dG

File operation undo

Undo and restore file operations in command line mode through the shortcut key
u to undo the previous operation
Ctrl+r to restore the previous undone operation

Select, paste, copy, and cut file content

File paste copy

In the command line mode,
copy all: After pressing the esc key, first press gg, then ggyG, and the group will not be deleted. ggdG
Select all to highlight: After pressing the esc key, first press gg, then ggvG or ggVG
to paste: p, after the cursor Paste at a word position. If there is no content on the pasteboard, p means to add a blank line, dd means to delete the line where the cursor is, and yy means to add a new line.

Single line copy: After pressing the esc key, then yy
Single line deletion: After pressing the esc key, then dd

Copy multiple lines
If you need to copy multiple lines, you can use y in conjunction with the move command. For example, if we need to copy three lines from the current line, we can use the following command.
In command line mode, enter

3yy

Among them, 3yy means copying the current line and the two lines below it.

If you need to copy the entire file, you can use the y command with the % symbol.

:%y

Among them: % means to select the entire file, and y means to copy the selected line.

File pasting and copying method 2

Copy and paste operations in Vim

Requires instructions:

v: 进入Visual模式
gg: 光标移动到文本第一行
G: 光标移动到文本的最后一行
y: 复制到vim寄存器中,非windows系统剪贴板。
p: 粘贴

Steps

1. Copy and paste the specified text.
Enter the Vim editor, type v to enter Visual mode,
move the cursor to select the content that needs to be copied.
Type y to copy, and "... line yanked" will be displayed in the lower left corner, indicating that the copy is successful.
Then type p in vim to paste the content.

2. Select all and copy

Enter the Vim editor and press v to enter Visual mode.
Type gg to reach the first line of text.
Type G to reach the last line of text and select all text.
Type y to copy into the register.
Type p to paste the contents of the register.

Note: The 'y' command in Vim means copying to the register, not the windows clipboard. You cannot use "Ctrl v" to paste in the Windows system, you can only use the p command to paste in the Vim editor.

File cut

The d and y commands are basically similar, so the usage of the two commands is the same, including the usage of numbers.
d cuts the selected block to the buffer;
dd cuts the entire line
d^ cuts to the beginning of the line
d$ cuts to the end of the line
dw Cut a word word
dG Cut the line where the cursor is to the end of the file

vim file content indentation

The > command is the indent command in Vim, which can be used to indent the content of one or more lines. When using the > command on an empty file, the file contents will be cleared and insert mode will be entered.

:>

The meaning of the above command is: indent the current line

Introduction to vimrc configuration file

1. What is .vimrc?

.vimrc is the configuration file of vim. All function switches related to the vim editor can be set through the .vimrc file.

Note: The "rc" in the file name comes from "run commands". The original source is the CTSS
system developed by MIT in 1965. There is a function to extract a series of commands from the file to execute, which is called run commands. This file is also called a
runcom. Nowadays, it is often referred to as configuration file.

2. .vimrc storage path

The .vimrc configuration file is divided into two types: system configuration and user configuration. The system .vimrc configuration file is stored in the vim installation directory, and the default path is /usr/share/vim/.vimrc. The user .vimrc configuration file is stored in the user's home directory ~/.vimrc.

Note: User configuration files take precedence over system configuration files. When vim starts, it will read the .vimrc file in the current user's root directory first. Therefore, personalized configurations related to individual users are generally placed in ~/.vimrc.

In the system command line, execute the vim --version command to view the saving path of the configuration file currently used by vim, as shown in the figure below: Above,
Insert image description here
we see several vimrc files listed, and there is a system vimrc file. , as well as the user's vimrc file, and the 2nd user vimrc file. In addition, for the purpose of compatibility with vi, vim also supports vi's exrc configuration file.

When vim starts, it will first try to execute the system's vimrc file, then search for the user vimrc in the above order, and use the configuration in the first user vimrc found, ignoring the rest of the user vimrc.

If you use the vim -u filename command to start vim, the filename you specify will be used as the vim configuration file (useful when debugging your vimrc); if you use the vim -u NONE command to start vim, no vimrc files will be read. , when you suspect that there is a problem with your vimrc configuration, you can use this method to skip the execution of vimrc.

3. Introduction to .vimrc

By default, the vim editor does not display line numbers, nor does it have syntax highlighting or smart indentation. For ease of use, basic vim configuration options generally include:

Show line number

set number

Highlight current column

set cursorcolumn

Show bracket matches

set showmatch

Set indentation and set Tab length to 4 spaces

set tabstop=4

Set comments
In vimrc, lines starting with double quotes will be ignored as comments.

Guess you like

Origin blog.csdn.net/tian830937/article/details/132438229