primers

The most used editor on the r&d line is VI. Whether it’s the fastest way to see the contents of a file, or the fastest way to edit a file, VI can help.

There seems to be some very long life in the software world, and VI is one of them. This article focuses on some of the most commonly used features on the r&d line. As for installing plug-ins, write scripts that are typically played on a development machine and the production environment doesn’t have the time or the conditions for you to do these enhancements. I hope after reading this article, I can have a general impression of this artifact. Of course, skilled use also needs daily conscious training.

Vim is an enhanced version of VI, and most modern Linux doesn’t lack a few megabytes of space, so all pre-installed versions are enhanced, and this article uses Vim by default.

  


Get into the habit of

Vim’s biggest contribution is its button system. This is why chrome, Idea, Atom and other editors all provide a Vim mode. I’ve seen many experienced programmers, including architects, use arrow keys to control cursor movement. This is not wrong, but it also throws away the best part of Vim, which is a huge loss of efficiency. Stick with H, J, K, L, and you’ll thank you for your corrections today. Your brain and your fingers really do have memories, and when you use them enough, it becomes your norm.

Another feature of VIm is its mode. There are four modes, and we don’t need to memorize them, just use examples to understand them.

Don’t be messed up

Do not use Vim to open large files, vim will read all contents into memory at once, easy to cause the host memory overflow.

Before opening the file, run the du -h command to check the file size. Generally, less than 100MB is appropriate.

Common operations

The following operations are performed in common mode by pressing continuously

roaming

J down

30j goes down 30 rows

K up

H to the left

L right

0 to the beginning of a line

^ To the first character of the line, if preceded by a space

$to the end of each line

Gg fast to file header

G Quickly to the end of the file

100G jumps to line 100

Cursor movement in insert mode is not recommended as it is inefficient

Copy: y

Yy Copy one line

Copy down 10 lines

Yw copies the word at the beginning of the cursor

Y $copies the cursor to the end of the line

YfB copies the cursor to the content in the middle of the first capital B

Y2fB copies the cursor to the content in the middle of the second capital B

Shear: x

The x is clipped one character at a time, or forward if it is the end of the line

3X cut three

Xp swaps two characters at the end of a non-line, such as from BS to sb

Delete: d

The deleted content is placed on the clipboard and pasted elsewhere by pressing P

Dd delete a row

200dd Deletes 200 lines

Dw delete a word (favorite)

Df “is deleted to the first double quotation mark that appears

Paste: p

P Paste copied or cut content

3p pastes the copied or cut content three times

Visual mode

V line mode, select some content

Visual mode is a very useful mode to enter by pressing V in normal mode.

Roaming using H, J, K, and L, select the corresponding content.

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

  


CTRL + v block model

Example: Add each line of a file to an ArrayList:

  


1) In command mode, execute %s/$/); /g, append data to 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 G to the end of the file

4) Ignore the editor response, press I to enter insert mode and type list.add(

5) Press ESC to return to normal mode, you can see that the above input has taken effect on each line

Block mode can also be used to swap columns, which seems to have been seen in UE.

  


Command mode

The command mode entry mode has been shown in the example above. In common mode, enter:.

%s/$/ STH/append STH to the end of the line

%s/\^M//g replaces the DOS newline, \^M can be entered by CTRL + V + Enter

:g/\^\s*$/d Deletes empty lines and whitespace only lines

%s/#.*//g Deletes characters after #

Yes, command mode uses re, and these lessons are universal

As you can see, this is probably the sed command for the editor window.

Find string

Similarly, knowledge of regularization can be applied

In normal mode, press/to enter the search, enter the corresponding string, and press OK.

N Finds the next match

N Finds the last match

2n looks for the second match below

If you feel confused and jumpy, enter set nu in command mode to turn on the line number.

Macro recording

This is a killer app for Vim. Take the example above.

Adds each line in the file to the ArrayList.

  


1) Press GG to go to the beginning of the line

2) Press QA for macro recording, a is the name of a mark we created

3) Press I to enter insert mode and type list.add(

4) Press ESC to enter normal mode, then $to jump to the end of the line

5) Press j to go to the next line, then press ^ to return to the beginning of the line

6) Press Q again to end macro recording

7) Input @a trigger macro to test the recording effect

8) Type 100@a and repeat the macro 100 times to affect the following 100 lines

Multiple macros can be recorded to facilitate batch operation

other

Other major features that are used less often are

R substitution character

GgVG selection

U Restore changes

J merges the next row

Move to uppercase at the cursor gU

GgguG converts the entire article from uppercase to lowercase

% jumps to the next match, as in

Press % to jump to the corresponding


:e/TMP /a Opens the/TMP /a file in the same editor. The buffer for the same editor is shared with the clipboard, making it easy to copy across multiple files

Bp jumps to the previous buffer

Bn jumps to the next buffer

Exit the editor

Wq Saves the current file and exits

Wqa saves all files and exits

q! If no, exit directly

qa! Multiple files were opened and exited simultaneously

This article focuses on common features that help readers quickly process online text. As for more, there is no room, only you to explore.

Vim has a high barrier to entry, but fortunately, you can’t get rid of it if you use it enough.