One picture flow: three modes and switching methods of vim
1. Command line
- delete
「x」 //每按一次,删除光标所在位置的“后面”一个字符
「*x」 //删除光标所在位置后*个字符
「X」 //大写的X,每按一次,删除光标所在位置的“前面”一个字符
「*X」 //删除光标所在位置的前*个字符
「dd」 //删除光标所在行
「*dd」 //从光标所在行开始删除*行
- copy
「yw」 //将光标所在之处到字尾的字符复制到缓冲区中
「*yw」 //复制*个字到缓冲区
「yy」 //复制光标所在行到缓冲区
「*yy」 //复制光标所在行在内的后*行到缓冲区
「p」 //将缓冲区内的字符贴到光标所在位置
**注意:所有与“y”有关的复制命令都必须与“p”配合才能完成复制与粘贴功能
- Reply to the last operation.
「u」 //如果你误执行一个命令,可以使用「u」,回到上一个操作。按多次“u”可以执行多次回复
- Exit vim and save the file: In the "command line mode", click the ":" colon key to enter the "bottom line mode"
「: w "filename"」 //将文章以指定的文件名filename保存
「: wq」 //存盘并退出vim
「: q!」 //不存盘强制退出vim
- replace.
「r」 //替换光标所在处的字符
「R」 //替换光标所到之处的字符,直到按下「ESC」键为止
- Jump to the specified line.
「Ctrl+g」 //列出光标所在行的行号。
「*G」 //移动到第*行
2. Insert row:
Edit text, the operation is not much different from other compilers
3. Bottom line:
Before using the bottom line mode, please remember to press the "ESC" key to confirm that you are already in the command line mode, and then press the ":" colon to enter the bottom line mode.
- List the line numbers.
「set nu」 //在文件中的每一行前面列出行号。
- Jump to a line in the file.
「:*」 //跳到第*行
- Find characters.
「/关键字」 //先按「/」键,再输入你想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往后寻找到您要的关键字为止。
「?关键字」 //先按「?」键,再输入你想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往前寻找到你要的关键字为止。
Fourth, advanced operations
- One of the ways to correct errors (search + replace)
「:s/old/new」 //替换该行第一个old为new
「:s/old/new/g」 //替换全行中所有的old为new
「:*,*s/old/new/g」 //替换两行之间出现的old为new,*,*为两行的行号
「:%s/old/new/g」 //替换全文的old为new
「:%s/old/new/gc」 //全文替换前需确认
- Search for matching brackets (useful in program trial adjustment)
Usage: move the cursor to a bracket, press %, the cursor jumps to its matching bracket - Invoke external commands and external files
- The method of executing external commands inside vim.
「:!ls」 //显示当前文件下所有文件名
- Save and delete files.
「:w filename」 //保存文件
「:!dir」
「:!rm filename」 //删除文件
- Selective save command.
「:*,* w filename」 //保存两行之间的文本
- Extract and merge files.
「:r anotherfile」 //将anotherfile文件中的内容提取到当前vim中
- Multiple file editing
「:n filename」 //编辑另一个文件
「:N filename」 //编辑上一个文件
「:files」 //列举vim目前打开的所有文件
- Multi-window operation
:sp [filename]
「ctrl+w+j」 //移到下一个窗口
「ctrl+w+k」 //移到上一个窗口
「ctrl+w+q」 //退出当前窗口