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#
jDown30jMove down 30 lineskUphLeftlRight0To the beginning of the line^To the first character of the line, if there are spaces before it$To the end of the lineggQuickly go to the beginning of the fileGQuickly go to the end of the file100GJump to line 100
It is not recommended to move the cursor in insert mode, as it is inefficient.
Copy: y#
yyCopy one line10yyCopy 10 lines downwardywCopy a word starting from the cursory$Copy from the cursor to the end of the lineyfBCopy the content between the cursor and the first uppercase By2fBCopy the content between the cursor and the second uppercase B
Cut: x#
xCut one character at a time, if it is at the end of the line, it cuts backward3xCut three charactersxpSwap 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.
ddDelete one line200ddDelete 200 linesdwDelete a word (my favorite)df"Delete to the first double quotation mark encountered
Paste: p#
pPaste the copied or cut content3pPaste 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/$/");/gto append data to the end of each line
- In command mode, execute
-
- Press
ESCto enter normal mode, and useggto go back to the beginning of the file
- Press
-
- Press
ctrl+vto enter visual mode, then pressGto go to the end of the file
- Press
-
- Ignore the editor's response and press
Ito enter insert mode, then enterlist.add("
- Ignore the editor's response and press
-
- Press
ESCto 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/Appendsthto the end of each line%s/^M//gReplace thedosline break character,\^Mcan be entered usingctrl+v+Enter:g/^s*$/dDelete empty lines and lines that only contain spaces%s/#.*//gDelete 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
ggto go to the beginning of the line
- Press
-
- Press
qato start recording the macro, a is the label name we chose
- Press
-
- Press
Ito enter insert mode, then enterlist.add("
- Press
-
- Press
ESCto enter normal mode, then press$to jump to the end of the line
- Press
-
- Press
jto go to the next line, then press^to go back to the beginning of the line
- Press
-
- Press
qagain to end the macro recording
- Press
-
- Enter
@ato trigger the macro and test the recording effect
- Enter
-
- Enter
100@ato 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
rReplace charactersggVGSelect alluUndo changesJMerge the next linegUConvert to uppercase at the cursorggguGConvert 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/aOpen the/tmp/afile in the same editor. The buffer and clipboard of the same editor are shared, making it easy to copy between multiple filesbpJump to the previous bufferbnJump to the next buffer
Exit the Editor#
wqSave the current file and exitwqaSave 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.
