picture
 focus on
"The Script House
, with millions of developers

picture

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, viit can help.

The software world seems to have some very long-lived things, vibe 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.

vimYes vi, the enhanced version is generally linuxnot lacking in modern times, so the pre-installed version is the enhanced version, which is used by default in this article vim.

picture

to form a habit

vimThe 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 vimbiggest essence, and the efficiency is a lot lower. Stick with h, j, k, land 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.

vimAnother 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 vimto open large files, vimit 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 -hcommand to view the file size. Generally, 100MBthe 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 from bsbecomingsb

delete :d

Deleted content will be placed on the clipboard, press pto 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

vline mode, select something

Visual mode is a very useful mode, which can be accessed by pressing v in normal mode .
Use h, j, k, lto roam, and select the corresponding content.

For example, select a part of the desired content and delete it.

picture

ctrl+vblock mode

Demo: add each line in the file to ArrayList:

picture

1) In command mode, execute %s/$/");/g, append data at the end of the line
2) Press ESCto enter normal mode, and use ggto return to the beginning of the line
3) Press ctrl+vto enter visual mode, then press Gto the end of the file
4) Ignore the editor's response, press Ito enter insert mode, enter list.add("
5) Press ESCback 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 UEhave been seen in this magic trick.

picture

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, \^Muse it ctrl+v  + Enterto 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

sedAs 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 nuIf you feel dizzy from jumping around, you can enter the opening line number in command mode .

Macro recording

This can be said to be vima killer. Take the example above.
Add each line in the file to ArrayList.

picture

1) Press ggto the beginning of the line
2) Press qato record a macro, which ais a tag name we made
3) Press Ito enter insert mode, enter list.add("
4) Press ESCto enter normal mode, then press $to skip to the end of the line
5) Press jto enter the next line, Then press ^back to the beginning of the line
6) Press again to qend the macro recording
7) Enter the @atrigger macro to test the recording effect
8) Enter the 100@arepeat 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.

vimThe barrier to entry is relatively high, and luckily, the more you use it, the more you can't let it go.

<END>

Programmer exclusive T-shirt

Product direct purchase link👇 

Script house carefully selected , transaction guarantee , buy with confidence , programmer geek short-sleeved T-shirt

picture

  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!

The Mysteries Behind Linux Memory
picture