Detailed usage of vim editor


Vim (Vi IMproved) is a powerful text editor and an enhanced version of Vi (Visual Editor). It is known for its high customizability, powerful editing features and extensive plugin support. Here are some tips on how to use the Vim editor. Skilled use of these skills can greatly improve the efficiency of text editing under Linux.

Install Vim on Linux platform

Install under Linux

# 安装Vim
sudo apt install vim-gtk3

# 查看Vim的安装版本
apt list --installed | grep vim

Download address of the installation package under Windows:
https://www.vim.org/download.php#pc

Vim configuration file

The address of the configuration file of Vim under Ubuntu is: /etc/vim/vimrc, and the name of the configuration file under Windows is _vimrc. The contents of the file are as follows:

# 指定编码方式为utf-8  
set enc-utf-8
# Vim 不兼容vi
set nocompatible
# 导入Vim的示例配置
source $VIMRUNTIME/vimrc_example.vim    

Vim four modes

There are four commonly used working modes of Vim:
1. Normal mode (normal): the default mode after Vim starts, used to move the cursor, delete text, overwrite input text, restore operations, paste text, etc. 2.
Insert mode (insert) : Enter i or a in normal mode to enter insert mode, which is equivalent to the edit mode of a normal editor, press ESC to exit
3. Visual mode (visual): press v to enter visual mode (character selection), press V to enter visual mode (line selection), press Ctrl + V to enter visual mode (block selection)
4. Command mode (command-line): used to execute internal and external commands, through ":" "/" "?" ":!" Enter command mode, ":" execute internal command "/", "?" search up or down, execute external command

basic operation

file storage

#   文件退出/存储
:w  # 保存文件
:q  # 退出编辑
:wq # 保存退出
:q! # 强制退出

:help          # 显示帮助菜单
set nu / nonu  # 显示/隐藏行号

cursor jump

# 普通模式下
v   # 进入可视化模式 w 移动光标到下一个单词的开头  b键来移动光标到前一个单词的开头
gg                 # 跳转到开头
G                  # 跳转到结尾
nG                 # 移动到该文件的第n行
xxgg               # 跳转到xx行
0  $ 或 home end   # 跳转到行首和行尾
h  j  k  l         # 对应着光标左移、下移、上移、右移  
b / w              # 跳转到上一个单词(words Backward)/下一个单词(words forwrd)
()            # 跳转到上一句/下一句
{
    
    }            # 跳转到上一段/下一段  
PageUp 和 PageDown # 翻页
zt zz zb           # 把当前行滚动到顶部(top) 滚动到中部 滚动到底部(bottom)

Edit content
Move the cursor to the keyword you want to search, press the * key, Vim will automatically extract the keyword under the cursor, and automatically add \< and \> to search.

# 删除
dd       # 删除整行(delete)
D        # 删除到行尾

# 插入行
o   O    # 在下方/上方插入一个新行 

# 撤销
u        # 撤销上一次操作
Ctrl + r # 重做上一次撤销的操作

# 查找
:nohlsearch                  # 设置不要高亮搜索
/word                        # 查找字符 n查找下一个 N查找上一个

# 替换
:n1,n2 s/word1/word2/       # 在第 n1 与 n2 行之间寻找 word1 这个字符串,并将该字符串取代为 word2
:%s/word1/word2/g           # 从第一行到最后一行寻找word1字符串,并将该字符串取代为word2 
:%s/word1/word2/gc          # 从第一行到最后一行寻找word1字符串,并将该字符串取代为word2,且在取代前显示提示字符给用户确认(confirm)是否需要取代

# 复制
y       # 复制选中的内容(搭配v模式来用)  
yy 或 Y # 复制当前行
nyy	    # n为数字, 复制光标所在的向下n行 
yw      # 用来复制往后面的一个词  
y3w     # 复制往后的三个词  
yiw     # 复制当前词  
y$      # 复制到行尾  
y^      # 复制到行首  
yf.     # 复制直到下一个 . 字符
ggyG 或 :%y  # 复制整个文件

# 粘贴(paste)
p,P # p粘贴到光标下一行,P粘贴到光标上一行(Paste)

multi-object operation

Multiple file editing

vim *.cpp       # 编辑多个文件  

# 命令模式操作  
:set autowrite     # 告诉Vim 切换文件的时候自动存盘 
:args              # 显示正在编辑的文件列表
:args filename     # 切换某个文件
:next              # 打开下一个文件
:prev              # 打开上一个文件
:frist 或 :rewind  # 回到列表中的第一个文件
:last              # 打开列表中的最后一个文件

cache management

:buffers 或 ls      # 显示缓冲区列表
:buffer  编号       # 跳转到固定编号的缓冲区(可以简化成:b)   
:bdelete 编号       # 删除对应的缓冲区(可缩写为:db)
:bnext   编号       # 跳转到下一个缓冲区(缩写为:bn)
:bNext / bprevious  # 跳转到上一个缓冲区(缩写成:bN 或者:bp) 
:blast              # 跳转到最后一个缓冲区
Ctrl+O  Ctrl+I      # 跳转到后一个/前一个编辑的缓冲区

Common operating procedures

copy cut/paste

1.normal模式下,按v来进入字符选择模式,按V进入行选择模式,Ctrl+v进入块选择模式
2.移动光标来选择对应的区域  
3.按y来进行复制, 按p/P 来进行粘贴
4.移动光标到对应位置,p粘贴到光标之后,P粘贴到光标之前(Paste 辅助记忆)

剪切命令(把上面操作中的y改成d就是剪切操作)

Paste in insert mode (p shortcut key is not available in insert mode)

Ctrl+r " 插入最近一次复制/剪切/删除的内容。" 是 Vim 的匿名寄存器
Ctrl+r 0 插入最近一次复制的内容。其中 0 属于 Vim 的编号寄存器,保存最近一次拷贝的内容  

Paste in command line mode
Press in normal mode: You can enter command line mode

Ctrl+r " 插入最近一次复制/剪切/删除的内容
Ctrl+r 0 插入最近一次复制的内容
Ctrl+f   选择一条历史命令(包括当前正在键入的命令)来编辑

Multi-window operation

window split

:split (缩写为:sp) + 文件名 分割窗口并打开指定的文件  
:split (缩写为:sp)         不加文件名 表示把当前窗口分开
:vsplit (缩写为:vs)        表示垂直分割

Ctrl+w  + (h、j、k、l) 可以在窗口之间跳转
Ctrl+w n      打开一个新窗口
Ctrl+w c      关闭当前窗口(最后一个则无效) 
Ctrl+w q      退出当前窗口
Ctrl+w o      只保留当前窗口 关闭其它窗口  
Ctrl+w s      和:split作用相同 横向一分为二
Ctrl+w v      和:vsplit作用相同 纵向一分为二  
Ctrl+w =      使得所有窗口大小相等  
Ctrl+w _      设置窗口的高度, 命令前的数字数表示高度行数,默认为纵向占满
Ctrl+w |      设置窗口宽度, 命令前的数字表示要增加的列数 默认为横向占满
Ctrl+w +      增加窗口的高度
Ctrl+w -      减少窗口的高度
Ctrl+w >      增加窗口的宽度
Ctrl+w <      减少窗口的宽度
Ctrl+w T      将多窗口中的某个窗口切换成独立页签(是大写的T==shift+t)

compare multiple files

vimdiff 或 gvimdiff filename1  filename2  # 对比两个文件

在一个文件编辑窗口中输入下面的命令  
:vert diffsplit filename2                 # 分割窗口和第二个文件做对比

Multi-tab operation

在已有的命令前加 tab 可以在新的标签页中展示命令的结果(例如  :tab help 在新的标签页中打开帮助)  

:tab split                在新的标签页中打开当前缓冲区
:tabs                     展示所有的标签页列表   
:tabnew 或 :tabedit       打开一个空白的新标签页,后面有文件名称则打开该文件  
:tabclose                 可以关闭当前页签
:tabnext 或 Ctrl+PageDown 切换到下一个标签页  
:tabNext 或 Ctrl+PageUp   切换到上一个标签页  
:tabfirst                 切换到第一个标签
:tablast                  切换到最后一个标签

Guess you like

Origin blog.csdn.net/yang1fei2/article/details/132416245