title: [Reprint] Powerful Editor Vim Usage on Linux
date: 2021-07-26 11:54:02
comment: false
category:
- Linux
tags: - Reprint
- Linux
- Vim
This article is a reprint from: Powerful Editor Vim Usage on Linux - Stupid Cat Blog
It seems that there are some very long-lasting things in the software world, and vi is one of them. This article focuses on some of the most commonly used features in online development. As for installing plugins and writing scripts, that's usually done on the development machine, and there are no conditions or time in the production environment to tolerate you doing these enhancements. I hope that after reading this article, you will have a general impression of this tool. Of course, proficient use also requires conscious cultivation in daily life.
Vim is an enhanced version of vi. Generally, modern Linux distributions do not lack a few megabytes of space, so the pre-installed version is the enhanced version. This article assumes the use of vim by default.
Develop Habits#
The biggest contribution of Vim is its key system. This is why editors like Chrome, IntelliJ IDEA, Atom, etc. provide a Vim mode. I have seen many experienced programmers, including architects, who are used to using the arrow keys to move the cursor. This is not to say that it is wrong, but it also abandons the essence of Vim, which greatly reduces efficiency. Stick to using h, j, k, l, and you will thank yourself for the correction today. The brain and fingers do have memory. When you use them enough, they become your established settings.
Another feature of Vim is that it has modes. There are a total of four modes, and we don't need to memorize them. Just use examples to understand.
Don't Mess Around#
Don't use Vim to open large files, as Vim reads all the content into memory at once, which can easily cause memory overflow on the host machine. Before opening a file, you can use the du -h
command to check the file size. Generally, it is advisable to keep it below 100MB.
Common Operations#
The following operations are performed in normal mode by pressing keys consecutively.
Roaming#
j
Down30j
Move down 30 linesk
Uph
Leftl
Right0
To the beginning of the line^
To the first character of the line, if there are spaces before it$
To the end of the linegg
Quickly go to the beginning of the fileG
Quickly go to the end of the file100G
Jump to line 100
It is not recommended to move the cursor in insert mode, as it is inefficient.
Copy: y#
yy
Copy one line10yy
Copy 10 lines downwardyw
Copy a word starting from the cursory$
Copy from the cursor to the end of the lineyfB
Copy the content between the cursor and the first uppercase By2fB
Copy the content between the cursor and the second uppercase B
Cut: x#
x
Cut one character at a time, if it is at the end of the line, it cuts backward3x
Cut three charactersxp
Swap two characters that are not at the end of the line, such as changing "bs" to "sb"
Delete: d#
The deleted content will be placed in the clipboard, and you can paste it elsewhere by pressing p.
dd
Delete one line200dd
Delete 200 linesdw
Delete a word (my favorite)df"
Delete to the first double quotation mark encountered
Paste: p#
p
Paste the copied or cut content3p
Paste the copied or cut content three times
Visual Mode#
v
Line mode, select some content
Visual mode is a very useful mode. Press v in normal mode to enter it. Use h, j, k, l to roam and select the corresponding content.
For example, select a part of the content you want and delete it.
Ctrl+V Block Mode#
Demonstration: Add each line in the file to an ArrayList:
-
- In command mode, execute
%s/$/");/g
to append data to the end of each line
- In command mode, execute
-
- Press
ESC
to enter normal mode, and usegg
to go back to the beginning of the file
- Press
-
- Press
ctrl+v
to enter visual mode, then pressG
to go to the end of the file
- Press
-
- Ignore the editor's response and press
I
to enter insert mode, then enterlist.add("
- Ignore the editor's response and press
-
- Press
ESC
to return to normal mode, and you will find that the above input has taken effect on each line
- Press
Block mode can also be used to call columns, which seems to be a magical technique in UE.
Command Mode#
The above examples have already shown how to enter command mode. In normal mode, enter : to enter it
%s/$/sth/
Appendsth
to the end of each line%s/^M//g
Replace thedos
line break character,\^M
can be entered usingctrl+v
+Enter
:g/^s*$/d
Delete empty lines and lines that only contain spaces%s/#.*//g
Delete characters after #
Yes, command mode uses regular expressions, and these experiences are universal.
You have already noticed that this is probably the sed command for the editor window.
Search for Strings#
Similarly, knowledge of regular expressions can be applied.
In normal mode, press /
to directly enter search mode, enter the corresponding string, and press Enter to confirm.
n
Find the next match
N
Find the previous match
2n
Find the second match below
If you feel dizzy from jumping back and forth, you can enter set nu
in command mode to enable line numbers.
Macro Recording#
This can be said to be a killer feature of Vim. Take the example above as an example.
Add each line in the file to an ArrayList
-
- Press
gg
to go to the beginning of the line
- Press
-
- Press
qa
to start recording the macro, a is the label name we chose
- Press
-
- Press
I
to enter insert mode, then enterlist.add("
- Press
-
- Press
ESC
to enter normal mode, then press$
to jump to the end of the line
- Press
-
- Press
j
to go to the next line, then press^
to go back to the beginning of the line
- Press
-
- Press
q
again to end the macro recording
- Press
-
- Enter
@a
to trigger the macro and test the recording effect
- Enter
-
- Enter
100@a
to repeat the macro 100 times, affecting the following 100 lines
- Enter
You can record multiple macros for batch operations.
Others#
Some other less commonly used main functions are
r
Replace charactersggVG
Select allu
Undo changesJ
Merge the next linegU
Convert to uppercase at the cursorggguG
Convert the entire article from uppercase to lowercase%
Jump to the next match. For example, pressing%
on<div>
will jump to the corresponding</div>
:e /tmp/a
Open the/tmp/a
file in the same editor. The buffer and clipboard of the same editor are shared, making it easy to copy between multiple filesbp
Jump to the previous bufferbn
Jump to the next buffer
Exit the Editor#
wq
Save the current file and exitwqa
Save all files and exitq!
Exit without savingqa!
If multiple files are open, exit all at once
This article only focuses on common functions to help readers quickly process online text. As for more, there is no room for them, you have to explore them yourself.