VIM常用快捷键(VIM common shortcut keys)

一、移动光标

  • h j k l 上 下 左 右
  • ctrl-y 上移一行
  • ctrl-e 下移一行
  • ctrl-u 上翻半页(up)
  • ctrl-d 下翻半页(down)
  • ctrl-f 上翻一页(forward)
  • ctrl-b 下翻一页(backward)
  • w 跳到下一个字首,按标点或单词分割
  • W 跳到下一个字首,长跳,如end-of-line被认为是一个字
  • e 跳到下一个字尾
  • E 跳到下一个字尾,长跳
  • b 跳到上一个字
  • B 跳到上一个字,长跳
  • 0 跳至行首,不管有无缩进,就是跳到第0个字符
  • ^ 跳至行首的第一个字符
  • $ 跳至行尾
  • gg 跳至文首
  • G 调至文尾
  • 5gg/5G 调至第5行
  • gd 跳至当前光标所在的变量的声明处
  • fx 在当前行中找x字符,找到了就跳转至
  • ; 重复上一个f命令,而不用重复的输入fx
  • * 查找光标所在处的单词,向下查找
  • # 查找光标所在处的单词,向上查找

二、删除复制

  • dd 删除光标所在行
  • dw 删除一个字(word)
  • d/D 删除到行末
  • x 删除当前字符
  • X 删除前一个字符
  • yy 复制一行
  • yw 复制一个字
  • y/Y 复制到行末
  • p 粘贴粘贴板的内容到当前行的下面
  • P 粘贴粘贴板的内容到当前行的上面

三、插入模式

  • i 从当前光标处进入插入模式
  • I 进入插入模式,并置光标于行首
  • a 追加模式,置光标于当前光标之后
  • A 追加模式,置光标于行末
  • o 在当前行之下新加一行,并进入插入模式
  • O 在当前行之上新加一行,并进入插入模式
  • Esc 退出插入模式

四、编辑

  • J 将下一行和当前行连接为一行
  • cc 删除当前行并进入编辑模式
  • cw 删除当前字,并进入编辑模式
  • c$ 擦除从当前位置至行末的内容,并进入编辑模式
  • s 删除当前字符并进入编辑模式
  • S 删除光标所在行并进入编辑模式
  • xp 交换当前字符和下一个字符
  • u 撤销
  • ctrl+r 重做
  • ~ 切换大小写,当前字符
  • >> 将当前行右移一个单位
  • << 将当前行左移一个单位(一个tab符)
  • == 自动缩进当前行

五、查找替换

  • /pattern 向后搜索字符串pattern
  • ?pattern 向前搜索字符串pattern
  • “\c” 忽略大小写
  • “\C” 大小写敏感
  • n 下一个匹配(如果是/搜索,则是向下的下一个,?搜索则是向上的下一个)
  • N 上一个匹配(同上)
  • :%s/old/new/g 搜索整个文件,将所有的old替换为new
  • :%s/old/new/gc 搜索整个文件,将所有的old替换为new,每次都要你确认是否替换

六、退出编辑器

  • :w 将缓冲区写入文件,即保存修改
  • :wq 保存修改并退出
  • 😡 保存修改并退出
  • :q 退出,如果对缓冲区进行过修改,则会提示
  • :q! 强制退出,放弃修改

七、多文件编辑

  • vim file1.. 同时打开多个文件
  • :args 显示当前编辑文件
  • :next 切换到下个文件
  • :prev 切换到前个文件
  • **:next!**不保存当前编辑文件并切换到下个文件
  • **:prev!**不保存当前编辑文件并切换到上个文件
  • :wnext 保存当前编辑文件并切换到下个文件
  • :wprev 保存当前编辑文件并切换到上个文件
  • :first 定位首文件
  • :last 定位尾文件
  • ctrl+^ 快速在最近打开的两个文件间切换
  • :split[sp] 把当前文件水平分割
  • :split file 把当前窗口水平分割, file
  • :vsplit[vsp] file 把当前窗口垂直分割, file
  • :new file 同split file
  • :close 关闭当前窗口
  • :only 只显示当前窗口, 关闭所有其他的窗口
  • :all 打开所有的窗口
  • :vertical all 打开所有的窗口, 垂直打开
  • :qall 对所有窗口执行:q操作
  • :qall! 对所有窗口执行:q!操作
  • :wall 对所有窗口执行:w操作
  • :wqall 对所有窗口执行:wq操作
  • ctrl-w h 跳转到左边的窗口
  • ctrl-w j 跳转到下面的窗口
  • ctrl-w k 跳转到上面的窗口
  • ctrl-w l 跳转到右边的窗口
  • ctrl-w t 跳转到最顶上的窗口
  • ctrl-w b 跳转到最底下的窗口

八、多标签编辑

  • :tabedit file 在新标签中打开文件file
  • :tab split file 在新标签中打开文件file
  • :tabp 切换到前一个标签
  • :tabn 切换到后一个标签
  • :tabc 关闭当前标签
  • :tabo 关闭其他标签
  • gt 到下一个tab
  • gT 到上一个tab
  • 0gt 跳到第一个tab
  • 5gt 跳到第五个tab

九、执行shell命令

  • 在命令模式下输入”:sh”,可以运行相当于在字符模式下,到输入结束想回到VIM编辑器中用exit,ctrl+D返回VIM编辑器
  • 可以”!command”,运行结束后自动回到VIM编辑器中
  • 用“Ctrl+Z“回到shell,用fg返回编辑
  • :!make -> 直接在当前目录下运行make指令

十、VIM启动项

  • -o[n] 以水平分屏的方式打开多个文件
  • -O[n] 以垂直分屏的方式打开多个文件

十一、自动排版

在粘贴了一些代码之后,vim变得比较乱,只要执行就能搞定

gg=G

十二、如何在vim中编译程序

在vim中可以完成make,而且可以将编译的结果也显示在vim里,先执行  命令,将结果输出的窗口打开,然后执行 

:copen
:make

编译后的结果就显示在了copen打开的小窗口里了,而且用鼠标双击错误信息,就会跳转到发生错误的行。

十三、buffer操作

buffer状态

  • – (非活动的缓冲区)
  • a (当前被激活缓冲区)
  • h (隐藏的缓冲区)
  • % (当前的缓冲区)
  • # (交换缓冲区)
  • = (只读缓冲区)
  • + (已经更改的缓冲区)

十四、 VIM 操作目录

1.打开目录

  • vim .
  • vim a-path/

2.以下操作在操作目录时生效

p,P,t,u,U,x,v,o,r,s

  • c 使当前打开的目录成为当前目录
  • d 创建目录
  • % 创建文件
  • D 删除文件/目录
  • – 转到上层目录
  • gb 转到上一个 bookmarked directory
  • i 改变目录文件列表方式
  • ^l 刷新当前打开的目录
  • mf – 标记文件
  • mu – unmark all marked files
  • mz – Compress/decompress marked files
  • gh 显示/不显示隐藏文件( dot-files)
  • ^h 编辑隐藏文件列表
  • a 转换显示模式, all – hide – unhide
  • qf diplay infomation about file
  • qb list the bookmarked directories and directory traversal history
  • gi Display information on file
  • mb
  • mc
  • md – 将标记的文件(mf标记文件)使用 diff 模式
  • me – 编辑标记的文件,只显示一个,其余放入 buffer 中
  • mh
  • mm – move marked files to marked-file target directory
  • mc – copy
  • mp
  • mr
  • mt

vim 中复制,移动文件

  • mt – 移动到的目录
  • mf – 标记要移动的文件
  • mc – 移动/复制

R 移动文件

打开当前编辑文件的目录

  • :Explore
  • :Hexplore
  • :Nexplore
  • :Pexplore
  • :Sexplore
  • :Texplore
  • :Vexplore
————————

1、 Move cursor

  • H J K L # up, down, left and right
  • Ctrl-y # move up one line
  • CTRL-E # move down one line
  • Ctrl-u# up
  • ctrl-d 下翻半页(down)
  • ctrl-f 上翻一页(forward)
  • ctrl-b 下翻一页(backward)
  • W , skip to the next prefix and split by punctuation or word
  • W , jump to the beginning of the next word, long jump, such as end of line is considered as a word
  • E) skip to the next suffix
  • E , jump to the next suffix, long jump
  • B) skip to the previous word
  • B. jump to the previous word, long jump
  • 0 # jump to the beginning of the line, whether indented or not, is to jump to the 0th character
  • ^Skip to the first character at the beginning of the line
  • $jump to end of line
  • GG jump to the beginning of the text
  • G. adjust to the end of the text
  • 5GG / 5G # adjust to line 5
  • Gd# jumps to the declaration of the variable where the current cursor is located
  • FX , find the X character in the current line, and jump to
  • ; Repeat the previous f command without entering FX repeatedly
  • *Find the word at the cursor and look down
  • #Find the word at the cursor and look up

2、 Delete replication

  • DD delete the line where the cursor is located
  • DW delete a word
  • D / D # delete to end of line
  • X) delete the current character
  • X # delete the previous character
  • YY # copy one line
  • Yw# copy a word
  • Y / Y # copy to end of line
  • P) paste the contents of the pasteboard below the current line
  • Paste the contents of the pasteboard onto the top of the current line

3、 Insert mode

  • I) enter the insertion mode from the current cursor
  • I) enter the insertion mode and place the cursor at the beginning of the line
  • A) append mode, set the cursor after the current cursor
  • A) add mode, set the cursor at the end of the line
  • O) add a new line under the current line and enter the insertion mode
  • O) add a new line above the current line and enter the insert mode
  • ESC # exit insert mode

4、 Edit

  • J) connect the next row and the current row into one row
  • CC delete the current line and enter edit mode
  • Cw# delete the current word and enter the editing mode
  • C $erase the content from the current position to the end of the line and enter the editing mode
  • S) delete the current character and enter editing mode
  • S) delete the line where the cursor is located and enter the editing mode
  • XP exchange current character and next character
  • U. revocation
  • ctrl+r 重做
  • ~Toggle case, current character
  • >>Moves the current line one unit to the right
  • < < move the current line left by one unit (a tab)
  • ==Automatically indent the current line

5、 Find replace

  • /pattern 向后搜索字符串pattern
  • ?pattern 向前搜索字符串pattern
  • “\ C” ignores case
  • “\ C” case sensitive
  • N  next match (if yes / search, it is the next down, and? Search is the next up)
  • N = previous match (ditto)
  • :% s / old / new / G – search the entire file and replace all old with new
  • :% s / old / new / GC: search the whole file, replace all old with new, and ask you to confirm whether to replace each time

6、 Exit editor

  • : w# write the buffer to the file, that is, save the changes
  • : wq# save changes and exit
  • : x# save changes and exit
  • : q# exit. If the buffer has been modified, you will be prompted
  • :q! Forced exit, discard modification

7、 Multi file editing

  • vim file1.. Open multiple files at the same time
  • : args # displays the currently edited file
  • : next # switch to the next file
  • : prev# switch to previous file
  • **:next!** Do not save the currently edited file and switch to the next file
  • **:prev!** Do not save the currently edited file and switch to the previous file
  • : wnext save the currently edited file and switch to the next file
  • : wprev # saves the currently edited file and switches to the previous file
  • : first # locate the first file
  • : last # locate the tail file
  • CTRL + ^ quickly switch between two recently opened files
  • : split [SP] split the current file horizontally
  • :split file 把当前窗口水平分割, file
  • :vsplit[vsp] file 把当前窗口垂直分割, file
  • :new file 同split file
  • : close closes the current window
  • : only displays the current window and closes all other windows
  • : all open all windows
  • : vertical all , open all windows vertically
  • : qall: q for all windows
  • :qall! Execute on all windows: q! operation
  • : Wall}: W for all windows
  • : wqall# perform: WQ actions on all windows
  • Ctrl-w h# jump to the left window
  • Ctrl-w j# jump to the following window
  • Ctrl-w k# jump to the upper window
  • Ctrl-w l# jump to the right window
  • Ctrl-w t# jump to the top window
  • Ctrl-w b# jump to the bottom window

8、 Multi label editing

  • :tabedit file 在新标签中打开文件file
  • :tab split file 在新标签中打开文件file
  • : TABP # switch to previous tag
  • : tabn # switch to the next tab
  • : tabc turn off the current tag
  • : Tabo # turn off other tags
  • gt 到下一个tab
  • gT 到上一个tab
  • 0gt jump to the first tab
  • 5gt jump to the fifth tab

9、 Execute shell command

  • Enter “: SH” in the command mode, which is equivalent to running in the character mode. If you want to return to the VIM editor at the end of input, use exit and Ctrl + D to return to the VIM editor
  • You can “! Command” and automatically return to VIM editor after running
  • Use “Ctrl + Z” to return to the shell and FG to return to editing
  • :! Make – > run the make instruction directly in the current directory

10、 VIM startup item

  • -O [n] open multiple files in horizontal split screen mode
  • -O [n] open multiple files in vertical split screen mode

11、 Automatic typesetting

After pasting some code, VIM becomes messy and can be done as long as it is executed

gg=G

12、 How to compile programs in VIM

Make can be completed in VIM, and the compiled results can also be displayed in vim. First execute the command , open the output window of the results, and then execute

:copen
:make

The compiled results are displayed in the small window opened by Copen, and double clicking the error message with the mouse will jump to the wrong line.

十三、buffer操作

buffer状态

  • -(inactive buffer)
  • A (currently activated buffer)
  • H (hidden buffer)
  • %(current buffer)
  • #(swap buffer)
  • =(read only buffer)
  • +(changed buffer)

14、 VIM operation directory

1. Open Directory

  • vim .
  • vim a-path/

2. The following operations take effect when operating the directory

p,P,t,u,U,x,v,o,r,s

  • C) make the currently open directory the current directory
  • D) create directory
  • %Create file
  • D) delete files / directories
  • -Go to the upper directory
  • gb 转到上一个 bookmarked directory
  • I) change the list mode of directory files
  • ^L) refresh the currently open directory
  • MF – tag file
  • mu – unmark all marked files
  • mz – Compress/decompress marked files
  • GH) show / not show hidden files (dot files)
  • ^H) edit hidden file list
  • a 转换显示模式, all – hide – unhide
  • qf diplay infomation about file
  • qb list the bookmarked directories and directory traversal history
  • gi Display information on file
  • mb
  • mc
  • MD – use diff mode for tagged files (MF tagged files)
  • Me – edit the marked file, display only one, and put the rest into the buffer
  • mh
  • mm – move marked files to marked-file target directory
  • mc – copy
  • mp
  • mr
  • mt

Copy and move files in VIM

  • MT – Directory moved to
  • MF – mark the file to move
  • MC – move / copy

< strong > R < / strong > move files

Open the directory of the currently edited file

  • :Explore
  • :Hexplore
  • :Nexplore
  • :Pexplore
  • :Sexplore
  • :Texplore
  • :Vexplore