Original: Miss Sister Taste (WeChat Public Account ID: xjjdog)
Reprinted with the authorization of the original official account
Introduction
The editor that is most used in R&D online is vi
. Whether it's the quickest way to view the contents of a file, or to quickly edit a file, vi
it can help.
The software world seems to have some very long-lived things, vi
be it one. This article focuses on some of the most commonly used functions on the R&D line. As for installing plug-ins and writing some scripts, it is usually played on the development machine, and the production environment has no conditions and no time to bear you to do these enhancements. I hope that after reading this article, I can have a general impression of this artifact. Of course, skilled use also requires daily conscious training.
vim
Yesvi
, the enhanced version is generallylinux
not lacking in modern times, so the pre-installed version is the enhanced version, which is used by default in this articlevim
.
to form a habit
vim
The biggest contribution is its key system . This is why chrome , idea , atom and other editors provide one vim mode
. The author has seen many senior programmers, including architects, who are used to using the arrow keys to control the movement of the cursor . This cannot be said to be wrong, but it also abandons the vim
biggest essence, and the efficiency is a lot lower. Stick with h
, j
, k
, l
and you'll be thankful for your correction today. The brain and fingers really have memories, and when you use them enough, this becomes your conventional setting.
vim
Another feature is the mode . There are four modes in total, we don't need to memorize, just use examples to understand.
don't make a mess
Do not use vim
to open large files, vim
it will read all the content to the memory at one time, which may easily cause the host memory to overflow.
Before opening the file, you can use the du -h
command to view the file size. Generally, 100MB
the following is appropriate.
Common operations
The following operations are performed in normal mode, pressing the key continuously
roaming
j down
30j move down 30 lines
k up
h left
l right
0 to the beginning of the line
^ to the first character at the beginning of the line, if there is a space before
$ to the end of the line
gg fast to the beginning of the file
G fast to the end of the file
100G jump to line 100
Cursor movement in insert mode is not recommended, it is inefficient
copy: y
yy copy one line
10yy copy 10 lines down
yw copy a word at the beginning of the cursor
y$ copy the cursor to the end of the line
yfB copy the cursor to the content in the middle of the first capital B
y2fB copy the cursor to the content in the middle of the second capital B
Cut: x
Cut one character in the x direction, if it is at the end of the line, it will cut
3x forward to cut three
xp non-end of line and exchange two characters, such as frombs
becomingsb
delete :d
Deleted content will be placed on the clipboard, press p
to paste elsewhere
dd delete a line
200dd delete 200 lines
dw delete a word (favorite)
df " delete to the first double quotation mark that appears
Paste: p
p paste the copied or cut content 3
p paste the copied or cut content three times
visualization mode
v
line mode, select something
Visual mode is a very useful mode, which can be accessed by pressing v in normal mode .
Use h
, j
, k
, l
to roam, and select the corresponding content.
For example, select a part of the desired content and delete it.
ctrl+v
block mode
Demo: add each line in the file to
ArrayList
:
1) In command mode, execute %s/$/");/g
, append data at the end of the line
2) Press ESC
to enter normal mode, and use gg
to return to the beginning of the line
3) Press ctrl+v
to enter visual mode, then press G
to the end of the file
4) Ignore the editor's response, press I
to enter insert mode, enter list.add("
5) Press ESC
back to normal mode, you can find that the above input has taken effect in each line
Block mode can also complete column swapping, which seems to UE
have been seen in this magic trick.
command mode
The above example has shown the entry mode of the command mode. In normal mode, enter :
to enter .
%s/$/sth/ Append sth at the end of the line
%s/\^M//g replace the dos newline,\^M
use itctrl+v + Enter
to enter
: g/\^\s*$/d to delete empty lines and lines with only spaces
%s/#.*//g delete#
characters after
Yes, the command mode uses regularity, these experiences are general
sed
As you've figured out, this is presumably the command for the editor window .
find string
Similarly, regular knowledge can also be applied*
In normal mode, press /
to directly enter the search, enter the corresponding string and press OK.
nFind next
matchNFind previous
match2nFind next match next
set nu
If you feel dizzy from jumping around, you can enter the opening line number in command mode .
Macro recording
This can be said to be vim
a killer. Take the example above.
Add each line in the file to ArrayList
.
1) Press gg
to the beginning of the line
2) Press qa
to record a macro, which a
is a tag name we made
3) Press I
to enter insert mode, enter list.add("
4) Press ESC
to enter normal mode, then press $
to skip to the end of the line
5) Press j
to enter the next line, Then press ^
back to the beginning of the line
6) Press again to q
end the macro recording
7) Enter the @a
trigger macro to test the recording effect
8) Enter the 100@a
repeat macro 100 times, which affects the following 100 lines
Multiple macros can be recorded for convenient batch operations
other
In addition, some of the main functions that are less used are:
r Replace character
ggVG Select all
u Restore changes
J Merge the next line
gU Convert uppercase at the cursor
ggguG Convert the entire article to lowercase
% Jump to the next match, if<div>
you press on%
it, jump to the corresponding</div>
: e /tmp/ aOpen/tmp/a
the file within the same editor . The buffer of the same editor is shared by the clipboard, which is convenient for copying
bp in multiple files and jumping to the previous buffer
bn jumping to the next buffer
Exit the editor
wq saves the current file and exits
wqa saves all files and exits
q! Do not save, exit
qa directly! Multiple files are opened, exit at the same time
This article only focuses on commonly used functions to help readers quickly process online texts. As for more, you can't hold it, only you can explore it yourself.
vim
The barrier to entry is relatively high, and luckily, the more you use it, the more you can't let it go.
Programmer exclusive T-shirt
Recommended reading:
finally! I found the reason why programmers love to wear sweaters
All online services are down. After investigation, it is actually vim's pot?
Learn these 21 rules, and you are not far from the Vim god!