Vim editing instructions

Basic Commands

  • Esc or Ctrl + [enters the normal mode

  • i (insert) a (additional) insert mode

  • Enter wq to save and exit

  • Vim back with the file name already exists, or you want to open the file name (as the new file) does not exist. Open Xfce Terminal
$ vim practice_1.txt
  • You can also open the direct use vim vim editor vim

  • After input into the command line: e the file path can also open the file

  • Cursor movement

h left
l Right (lowercase L)
j under
k on
w Move to the next word
b Move to the previous word
  • Use the following key in the normal mode into insert mode, and can start from a position corresponding to the input
command Explanation
i Edited in the current cursor position
I First insert the line
A The insertion end of the line
a Insert editing cursor
O After the current line to insert a new row
O Before the current line to insert a new row
cw Replacement character to the end of a word from the cursor position

Try different ways to enter insert mode from the normal mode, in front of the last line shiyanlou add www., Note that each must first return to normal mode to switch to a different way to enter insert mode

Save the document (the document is saved in command line mode)

Normal mode from input: enter the command line mode, enter w Enter, save the document. Type: w document file name can be saved as a file name or other memory to the other path

Exit vim (command line mode exit vim)

  • Normal mode from input: enter the command line mode, enter wq enter, save and exit the editor
command Explanation
q! Forced to exit without saving
q drop out
wq! Force Save and Exit
w <file path> Save as
saveas file path Save as
x Save and Exit
wq Save and Exit
  • Exit vim normal mode

  • Enter the Normal mode Shift + zz to save and exit vim

Delete the text (deleted text messages vim normal mode)

  • Into normal mode, use the following command to delete text quickly
command Explanation
x Delete the character under the cursor
X To delete a character before the cursor
Delete With x
dd Delete the entire line
dw To delete a word (NA Chinese)
d $ or D Delete to end of line
d^ Delete to the beginning of the line
dG Delete To End Of Document
d1G To delete the first section of the document
in Undo
ndd N multi-line position of the cursor to delete from digital
yy Copy the line the cursor is currently located
nyy N digital multi-copy behavior
p Paste the copied content to the next line position of the cursor
Big P Paste the copied content to position the cursor on the line
for example, Paste to the digital multi-line cursor next behavior of n
cttl+r Repeat the last action
$ Jump to the tail line
0 跳到一行的头部
gg 移动到这个文件的第一行
G 跳到这个文件的最后一行
nG 跳到n行
set nu 显示行号
H 光标移动到屏幕的最上方那一行的第一个字符
M 光标移动到屏幕的中央那一行的第一个字符
L 光标移动到屏幕的最下面那一行的第一个字符

vim重复命令

  • 重复执行上次命令

  • 在普通模式下.(小数点)表示重复上一次的命令操作

  • 拷贝测试文件到本地目录
$ cd /home/shi
$ cp /etc/protocols .

游标的快速跳转

普通模式下,下列命令可以让光标快速调转到指定位置,我们分别讨论快速实现行间跳转和行内跳转

  • 行间跳转
命令 说明
nG(n shift+g) 游标移动到第 n 行(如果默认没有显示行号,请先进入命令模式,输入:set nu以显示行号)
gg 游标移动到第一行
G(Shift+g) 到最后一行

小技巧:你在完成依次跳转后,可以使用 Ctrl+o 快速回到上一次(跳转前)光标所在位置,这个技巧很实用,比如当你在写代码时,忽然想起有个 bug,需要修改,这时候你跳过去改好了,只需要按下 Ctrl+o 就可以回到你之前的位置。vim 中会用很多类似的小技巧就等着你去发掘。

  • 行内跳转
命令 说明
w 到下一个单词的开头
e 到当前单词的结尾
b 到前一个单词的开头
ge 到前一个单词的结尾
0或^ 到行头
$ 到行尾
f <字母> 向后搜索 <字母> 并跳转到第一个匹配的位置(非常实用)
F <字母> 向前搜索 <字母> 并跳转到第一个匹配的位置
t <字母> 向后搜索 <字母> 并跳转到第一个匹配位置之前的一个字母(不常用)
T <字母> 向前搜索 <字母> 并跳转到第一个匹配位置之后的一个字母(不常用)

复制粘贴和剪切

  • 复制及粘贴文本

  • 普通模式中使用y复制
    • 普通模式中,yy复制游标所在的整行(3yy表示复制3行)
    • 普通模式中,y^ 复制至行首,或y0。不含光标所在处字符。
    • 普通模式中,y$ 复制至行尾。含光标所在处字符。
    • 普通模式中,yw 复制一个单词。
    • 普通模式中,y2w 复制两个单词。
    • 普通模式中,yG 复制至文本末。
    • 普通模式中,y1G 复制至文本开头。
  • 普通模式中使用 p 粘贴
    • 普通模式中,p(小写)代表粘贴至光标后(下)
    • 普通模式中,P(大写)代表粘贴至光标前(上)

字符的替换及撤销(Undo操作)

  • 替换和撤销(Undo)命令
  • 替换和Undo命令都是针对普通模式下的操作
命令 说明
r+ <待替换字母> 将游标所在字母替换为指定字母
R 连续替换,直到按下Esc
cc 替换整行,即删除游标所在行,并进入插入模式
cw 替换一个单词,即删除一个单词,并进入插入模式
C(大写) 替换游标以后至行末
~ 反转游标所在字母大小写
u{n} 撤销一次或n次操作
U(大写) 撤销当前行的所有修改
Ctrl+r redo,即撤销undo的操作

如下操作

  • 输入fa 跳转到第一个a字符
  • 输入r,并且输入b,a字符被b字符替换(实用)
  • 输入R替换字符,输入新字符串,输入完按ESC回到普通模式(实用)
  • 输入cc替换整行字符,输入新字符串,输入完按ESC回到普通模式
  • 输入cw 替换一个英文字(word),输入完按ESC回到普通模式(实用)
  • 输入~,翻转游标所在字符的大小写
  • 输入C 替换至行尾,即游标所在处以后的字都会被替换,输入完按ESC回到普通模式
  • 输入u 撤销上一次的操作

快速缩进

  • 普通模式下输入15G,跳转到15行
  • 普通模式下输入>> 整行将向右缩进(使用,用于格式化代码超爽)
  • 普通模式下输入<< 整行向左回退
  • 普通模式下输入:进入命令行模式下对shiftwidth值进行设置可以控制缩进和回退的字符数

  • shiftwidth命令
  • shiftwidth命令是指上一节>>命令产生的缩进(可以简写成sw) 普通模式下输入:进入命令行模式下对shiftwidth值进行设置可以控制缩进和回退的字符数 获取目前的设定值
      :set shiftwidth?
  • 设置缩进为10个字符
      :set shiftwidth=10

输入 ESC 回到普通模式,再次尝试 >> 看缩进量是否变化

  • 调整文本位置
    命令行模式下输入:ce(center)命令使本行内容居中
      :ce

命令行模式下输入:ri(right)命令使本行文本靠右

      :ri

命令行模式下输入:le(left)命令使本行内容靠左

      :le

查找

  • 快速查找

普通模式下输入 / 然后键入需要查找的字符串 按回车后就会进行查找。 ? 与/ 功能相同,只不过 ? 是向上而 / 是向下查找。 进入查找之后,输入n 和 N 可以继续查找。 n是查找下一个内容,N查找

  • 普通模式下输入/icmp然后回车即可查找字符串 icmp
  • 普通模式下输入n查找下一个 icmp
  • 普通模式下输入?tcp向上查找字符串 tcp
  • 普通模式下输入N查找上一个出现的 tcp
  • 命令行模式下输入 noh 然后回车即可取消搜索

  • 高级查找

  • 普通模式下输入*寻找游标所在处的单词
  • 普通模式下输入#同上,但 # 是向前(上)找,*则是向后(下)找
  • 普通模式下输入g*同* ,但部分符合该单词即可
  • 普通模式下输入g#同# ,但部分符合该单词即可

可视模式

可视模式命令简介

  • 在普通模式下输入 v(小写),进入字符选择模式,就可以移动光标,光标走过的地方就会选取。再次按下v后就会取消选取。
  • 在普通模式下输入 Shift+v(小写),进入行选择模式,按下V之后就会把整行选取,您可以上下移动光标选更多的行,同样,再按一次 Shift+v 就可以取消选取。
  • 在普通模式下输入 Ctrl+v(小写),这是区域选择模式,可以进行矩形区域选择,再按一次 Ctrl+v 取消选取。
  • 在可视模式下输入 d 删除选取区域内容
  • 在可视模式下输入y复制选取区域内容

视窗操作

  • 视窗操作简介

vim 可以在一个界面里打开多个窗口进行编辑,这些编辑窗口称为 vim 的视窗。 打开方法有很多种,例如可以使用在命令行模式下输入 :new 打开一个新的 vim 视窗,并进入视窗编辑一个新文件(普通模式下输入 Ctrl+w也可以),除了 :new 命令,下述列举的多种方法也可以在命令模式或普通模式下打开新的视窗:

  • 命令行模式下输入:sp 1.txt 打开新的水平分屏视窗来编辑1.txt
  • 命令行模式下输入:vsp 2.txt 打开新的垂直分屏视窗来编辑2.txt
  • 普通模式下Ctrl+w s 将当前窗口分割成两个水平的窗口
  • 普通模式下Ctrl+w v 将当前窗口分割成两个垂直的窗口
  • 普通模式下Ctrl+w q 即 :q 结束分割出来的视窗。如果在新视窗中有输入需要使用强制符!即:q!
  • 普通模式下Ctrl+w o 打开一个视窗并且隐藏之前的所有视窗
  • 普通模式下Ctrl+w j 移至下面视窗
  • 普通模式下Ctrl+w k 移至上面视窗
  • 普通模式下Ctrl+w h 移至左边视窗
  • 普通模式下Ctrl+w l 移至右边视窗
  • 普通模式下Ctrl+w J 将当前视窗移至下面
  • 普通模式下Ctrl+w K 将当前视窗移至上面
  • 普通模式下Ctrl+w H 将当前视窗移至左边
  • 普通模式下Ctrl+w L 将当前视窗移至右边
  • 普通模式下Ctrl+w - 减小视窗的高度
  • 普通模式下Ctrl+w + 增加视窗的高度

Guess you like

Origin www.cnblogs.com/xinzaiyuan/p/12106242.html