banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

【Reprint】Powerful Editor Vim Usage on Linux

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.image

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#

  • j Down
  • 30j Move down 30 lines
  • k Up
  • h Left
  • l Right
  • 0 To the beginning of the line
  • ^ To the first character of the line, if there are spaces before it
  • $ To the end of the line
  • gg Quickly go to the beginning of the file
  • G Quickly go to the end of the file
  • 100G Jump to line 100

It is not recommended to move the cursor in insert mode, as it is inefficient.

Copy: y#

  • yy Copy one line
  • 10yy Copy 10 lines downward
  • yw Copy a word starting from the cursor
  • y$ Copy from the cursor to the end of the line
  • yfB Copy the content between the cursor and the first uppercase B
  • y2fB Copy the content between the cursor and the second uppercase B

Cut: x#

  • x Cut one character at a time, if it is at the end of the line, it cuts backward
  • 3x Cut three characters
  • xp Swap 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.

  • dd Delete one line
  • 200dd Delete 200 lines
  • dw Delete a word (my favorite)
  • df" Delete to the first double quotation mark encountered

Paste: p#

  • p Paste the copied or cut content
  • 3p Paste 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.

image

Ctrl+V Block Mode#

image

Demonstration: Add each line in the file to an ArrayList:

    1. In command mode, execute %s/$/");/g to append data to the end of each line
    1. Press ESC to enter normal mode, and use gg to go back to the beginning of the file
    1. Press ctrl+v to enter visual mode, then press G to go to the end of the file
    1. Ignore the editor's response and press I to enter insert mode, then enter list.add("
    1. Press ESC to return to normal mode, and you will find that the above input has taken effect on each line

Block mode can also be used to call columns, which seems to be a magical technique in UE.

image

Command Mode#

The above examples have already shown how to enter command mode. In normal mode, enter : to enter it

  • %s/$/sth/ Append sth to the end of each line
  • %s/^M//g Replace the dos line break character, \^M can be entered using ctrl+v + Enter
  • :g/^s*$/d Delete empty lines and lines that only contain spaces
  • %s/#.*//g Delete 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

image

    1. Press gg to go to the beginning of the line
    1. Press qa to start recording the macro, a is the label name we chose
    1. Press I to enter insert mode, then enter list.add("
    1. Press ESC to enter normal mode, then press $ to jump to the end of the line
    1. Press j to go to the next line, then press ^ to go back to the beginning of the line
    1. Press q again to end the macro recording
    1. Enter @a to trigger the macro and test the recording effect
    1. Enter 100@a to repeat the macro 100 times, affecting the following 100 lines

You can record multiple macros for batch operations.

Others#

Some other less commonly used main functions are

  • r Replace characters
  • ggVG Select all
  • u Undo changes
  • J Merge the next line
  • gU Convert to uppercase at the cursor
  • ggguG Convert 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/a Open the /tmp/a file in the same editor. The buffer and clipboard of the same editor are shared, making it easy to copy between multiple files
  • bp Jump to the previous buffer
  • bn Jump to the next buffer

Exit the Editor#

  • wq Save the current file and exit
  • wqa Save all files and exit
  • q! Exit without saving
  • qa! 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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.