Most useful series:

The Most Common Set of “Vim” Techniques for Linux Production

The most Common set of “Sed” Techniques for Linux Production

The Most Common Set of “AWK” Tips for Linux Production

“Sed” Advanced Function: My Little Brain is spinning.”

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
  • xpSwap two characters at the end of a non-line, as frombsbecomesb

Delete: d

Delete content to the clipboard, presspYou can paste it somewhere else

  • Dd delete a row
  • 200dd Deletes 200 lines
  • Dw delete a word (favorite)
  • Df “deletes 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

vLine 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+vBlock pattern

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

  • 1)In command mode, run%s/$/"); /gTo append data to the end of the line
  • 2)ESCEnter normal mode and useggBack to the beginning of a line
  • 3)ctrl+vEnter visual mode and pressGThe end-of-file
  • 4)Ignore the editor response and pressIEnter insert mode and typelist.add("
  • 5)ESCBack to normal mode, you can see that the above input is already in effect on each line

Block mode can also complete column calls, 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//gReplace the DOS newline character,\^Musectrl+v + EnterYou can enter
  • :g/^\s$/d* Delete empty lines and whitespace only lines
  • %s/#.*//gdelete#Following character

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 theggTo the beginning of a line
  • 2)Press theqaMacro recording,aIt’s a tag name that we came up with
  • 3)IEnter insert mode and typelist.add("
  • 4)ESCEnter normal mode and press$Jump to the end of each line
  • 5)jGo to the next line and press^Back to the beginning of a line
  • 6)Once again, press theqEnd macro recording
  • 7)The input@aTrigger macro to test recording effect
  • 8)The input100@aRepeat the macro 100 times to affect the next 100 lines

Can record different macros, aspects of the 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
  • %Jump to the next match, as in<div>On the press%, jumps to the corresponding</div>
  • :e /tmp/aOpen in the same editor/tmp/aFile. 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.