High-efficiency shortcut keys commonly used in vim

Preface

There are many shortcut keys for operation in vim, and the functions are very powerful. When you are proficient and practical with some shortcut keys, you can get twice the result with half the effort and improve the efficiency a lot. This article records some efficient shortcut keys related to document modification for use.

vim case conversion

format:

[开始位置]    ———— 可以定位开始的位置,默认为光标所在位置
gu           ———— 选择范围内转小写
gU           ———— 选择范围内转大写 
[结束位置]    ———— 可以定位结束位置,可跟着类似w,6G,G,gg等定位操作

Note: The following commands do not need to enter the command line mode , vim can open the file and type the command

1. Case conversion of " letter " level
If you want to convert the case of the letter at the cursor position, directly shift + ~

2. " Word "-level case conversion
Perform case conversion of the entire word at the cursor position

guw 或 gue     # 光标所在位置的单词转小写
gUw 或 gUe     # 光标所在位置的单词转大写
 
gu3w 或 gu3e    # 光标后面的3个单词转小写
gU3w 或 gU3e    # 光标后面的3个单词转大写

3. Case conversion at " line " level

guu    # 光标所在的行转小写
gUU    # 光标所在的行转大写
 
gu0    # 光标位置到行首转小写,不包含光标所在字母
gU0    # 光标位置到行首转大写,不包含光标所在字母
 
gu$    # 光标位置到行尾转小写,包含光标所在字母
gU$    # 光标位置到行尾转大写,包含光标所在字母
 
gu1G   # 光标位置所在行到文章第一行转小写
gU1G   # 光标位置所在行到文章第一行转大写
 
guG    # 光标位置所在行到文章末尾转小写
gUG    # 光标位置所在行到文章末尾转大写

4. Case conversion at " file " level

1GguG 或 ggguG    # 整个文章全部转小写
1GgUG 或 gggUG    # 整个文章全部转大写
注:[1G或gg]:定位开始位置,[gu或gU]:定义转小写或大写  [G]:定位结束位置

vim replacement shortcuts

In vi/vim, you can use the: s command to replace strings. Previously, only one format was used to replace the full text. Today, I found that there are many ways to write this command (vim is really powerful!!!)

:s/vivian/sky/ 替换当前行第一个 vivian 为 sky
:s/vivian/sky/g 替换当前行所有 vivian 为 sky
:n,$s/vivian/sky/ 替换第 n 行开始到最后一行中每一行的第一个 vivian 为 sky
:n,$s/vivian/sky/g 替换第 n 行开始到最后一行中每一行所有 vivian 为 sky
n 为数字,若 n 为 .,表示从当前行开始到最后一行
:%s/vivian/sky/(等同于:g/vivian/s//sky/)替换每一行的第一个 vivian 为 sky
:%s/vivian/sky/g(等同于:g/vivian/s//sky/g)替换每一行中所有 vivian 为 sky

: %S/vivian/sky/g realizes the replacement of the file and, replace the vivian in the entire file with sky.

You can use # as a separator, and the / in the middle will not be used as a separator

:s#vivian/#sky/# 替换当前行第一个 vivian/ 为 sky/
:%s+/oradata/apras/+/user01/apras1+ (使用+ 来 替换 /):/oradata/apras/替换成/user01/apras1/ 

vim edit multiple files at the same time in the same window

1. If you do not open vim in the terminal, you can:

Horizontal split display:

$ vim -o filename1 filename2 

Vertical split display:

$ vim -O filename1 filename2

2. If you have opened a file with vim and want to open another file at the same time in the window:

Horizontal split display:

:vs filename

Vertical split display:

:sp filename

Among them, vs can be replaced with vsplit, and sp can be replaced with split.

If the finename does not exist, the file will be created and opened.

Three, close the window

Close the window where the cursor is:

:q or :close

Close other windows except the window where the cursor is located:

:only

Close all windows:

:qa

Four, switch windows

When multiple windows are open and need to switch between windows:

ctrl + w w

That is, hold down the ctrl key and press the w key twice.

Or ctrl + w <h|j|k|l>

That is, hold down the ctrl key, press the w key once, and press the h or j or k or l indicating the direction again, and the cursor will switch to the left|down|up|right window of the current window.

Use Vim to compare the contents of two files

  1. Use vim's compare mode to open two files:
vim -d file1 file2
或
vimdiff file1 file2
  1. If the file file1 has already been opened, open another file file2 for comparison:
:vert diffsplit file2

If the vert command is not used, diffsplit will be divided into upper and lower windows.

  1. If you have opened two files file1 and file2 in split mode, you want to compare the differences between the two files.
    Enter commands in the two windows respectively:
    :diffthis

  2. If you change the content of a window and vim does not automatically update the diff check, you can use the following command to update:
    :diffupdate

  3. Locate to a different point:
    [c jump to the previous different point
    ] c jump to the next different point

  4. Jump between windows:
    ctrl-w w jump to the next window
    ctrl-w h jump to the left window
    ctrl-w l jump to the right window
    ctrl-w j jump to the lower window
    ctrl-w k jump to the upper window window

  5. Merging documents:
    dp applies the content of the current document at the point of difference to another document (diff put)
    do copies the content of another document at the point of difference to the current document (diff get)

  6. Expanding and viewing the context When
    comparing and merging files, it is often necessary to combine the context to determine the final action to be taken. By default, Vimdiff will display the 6 lines of text above and below the difference for reference. Other identical text lines are automatically folded. If you want to modify the default number of context lines to 3, you can set it like this:
    :set diffopt=context:3

You can use a simple folding command to temporarily expand the same text line that has been folded:
zo (folding open, z This letter looks more like folded paper)

Then you can use the following command to refold:
zc (folding close)

vim view and modify binary files

Vim will be garbled when opening binary files, we can use it in command mode: %! xxd to convert to hexadecimal for viewing, you can use %xxd -r to restore it.

:%!xxd

xxd is a command of linux, vim can use "!" to call external commands, its function is to carry out hexadecimal dump or vice versa.

Reference
Reference: https://www.cnblogs.com/zhanglanyun/archive/2011/12/19/2293695.html
https://www.cnblogs.com/taskiller/archive/2012/07/26/2610583.html

Guess you like

Origin blog.csdn.net/u014470361/article/details/102082291