Common vi/vim commands

forward from:

https://blog.csdn.net/wang907553141/article/details/78846784

Common vi/vim commands:

vi command:
    yy: copy the line where the cursor is located
    nyy: copy n lines down from the line where the cursor is located

    p: paste

    dd: cut the line where the cursor is located
    ndd: cut the line where the cursor is located down n lines
    D: from the current The cursor starts to cut until the end of the line
    d: Cut from the current cursor to the beginning of the line
    x: delete the character of the current cursor, only one character can be deleted at a time
    X: delete the character in front of the current cursor, only one character can be deleted at a time
    nx: delete n characters backward from the current cursor (including the current cursor)
    nX: delete n characters forward from the current cursor (excluding the current cursor)

    h left l right k up j down
    H: the current screen The top line
    M: the middle of the
    current screen L: the bottom line of the current screen

    ctrl+f: turn down one page
    ctrl+b: turn back one page
    ctrl+d: turn down half a page
    ctrl+u: turn up Half page

    nG: Quickly locate to the position of the nth line
    G: Quickly locate the position of the last line
    gg: Quickly locate the position of the first line

    w: Skip to the beginning of the next word
    b: Skip to the previous one At the beginning of the word
    
    u: undo the operation just now
    ctrl+r: undo the

    selection of a piece of text
    v:
    V:

    >>: move code to the right
    <<: move code to the left

    .: repeat the last operation
    {: move by segment, move up
    }: move by segment, move down
    
    r: replace the current character
    R: replace The current line cursor and the characters after the cursor

    /: str find
    n: find the previous content
    N: find the next content


Here are all the commands:

vi has 3 modes: insert mode, command mode, low-line mode.

Insert Mode: In this mode, you can enter characters, press ESC to return to command mode.

Command mode: You can move the cursor, delete characters, etc.

Low-line mode: You can save files, exit vi, set vi, search and other functions (low-line mode can also be regarded as command mode).

1. Open, save, and close files (used in vi command mode)

vi filename //Open the filename file 
:w //Save the file 
:w vpser.net //Save to the vpser.net file 
:q //Exit the editor, if the file has been modified, please use the following command 
: q! //Quit editing editor without saving 
: wq //Exit the editor and save the file 

2. Insert text or line (used in vi command mode, after executing the following command, it will enter insert mode, press ESC key to exit insert mode)

a      //在当前光标位置的右边添加文本 
i       //在当前光标位置的左边添加文本 
A     //在当前行的末尾位置添加文本 
I      //在当前行的开始处添加文本(非空字符的行首) 
O     //在当前行的上面新建一行 
o     //在当前行的下面新建一行 
R    //替换(覆盖)当前光标位置及后面的若干文本 
J    //合并光标所在行及下一行为一行(依然在命令模式)

三、移动光标(vi命令模式下使用)

1、使用上下左右方向键

2、命令模式下:h   向左、j   向下 、k   向上、l  向右。

空格键 向右、Backspace  向左、Enter  移动到下一行首、-  移动到上一行首。

四、删除、恢复字符或行(vi命令模式下使用)

x         //删除当前字符 
nx         //删除从光标开始的n个字符 
dd      //删除当前行 
ndd   //向下删除当前行在内的n行 
u       //撤销上一步操作 
U      //撤销对当前行的所有操作 

五、搜索(vi命令模式下使用)

/vpser     //向光标下搜索vpser字符串 
?vpser     //向光标上搜索vpser字符串 
n           //向下搜索前一个搜素动作 
N         //向上搜索前一个搜索动作 

六、跳至指定行(vi命令模式下使用)

n+        //向下跳n行 
n-         //向上跳n行 
nG        //跳到行号为n的行 
G           //跳至文件的底部

七、设置行号(vi命令模式下使用)

:set  nu     //显示行号 
:set nonu    //取消显示行号

八、复制、粘贴(vi命令模式下使用)

yy    //将当前行复制到缓存区,也可以用 "ayy 复制,"a 为缓冲区,a也可以替换为a到z的任意字母,可以完成多个复制任务。

nyy   //将当前行向下n行复制到缓冲区,也可以用 "anyy 复制,"a 为缓冲区,a也可以替换为a到z的任意字母,可以完成多个复制任务。

yw    //复制从光标开始到词尾的字符。

nyw   //复制从光标开始的n个单词。

y^      //复制从光标到行首的内容。  VPS侦探

y$      //复制从光标到行尾的内容。

p        //粘贴剪切板里的内容在光标后,如果使用了前面的自定义缓冲区,建议使用"ap 进行粘贴。

P        //粘贴剪切板里的内容在光标前,如果使用了前面的自定义缓冲区,建议使用"aP 进行粘贴。

九、替换(vi命令模式下使用)

:s/old/new      //用new替换行中首次出现的old 
:s/old/new/g         //用new替换行中所有的old 
:n,m s/old/new/g     //用new替换从n到m行里所有的old 
:%s/old/new/g      //用new替换当前文件里所有的old

十、编辑其他文件

:e otherfilename    //编辑文件名为otherfilename的文件。

十一、修改文件格式

:set fileformat=unix   //将文件修改为unix格式,如win下面的文本文件在linux下会出现^M


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324400033&siteId=291194637