【Vim】Vim common editing operations

Table of contents

regular expression

vim command

working mode of vim

Undo changes, redo and save

cursor movement command

text insertion

Text delete operation

Text copy, cut and paste

Text modification and replacement

Multi-window operation


regular expression

Simply put, a regular expression is a symbolic notation for recognizing patterns in text. In a way, they are similar to shell wildcards used when matching file and pathnames, but are much more versatile. Many command-line tools and most programming languages ​​support regular expressions as a way to solve text manipulation problems. However, regular expressions can vary slightly between different tools, and between different programming languages, which further complicates matters. For convenience, we restrict our discussion of regular expressions to the POSIX standard (which covers most command-line tools), unlike many programming languages ​​(most notably Perl), which use a somewhat larger set of symbols.

vim command

The vim editor has three modes:

command mode, edit mode, last line mode

How to switch between modes:

(1) In the command mode, after entering:, enter the last line mode

(2) In the last line mode, press esc to rewind slowly, press esc twice to rewind quickly, or delete all commands to return to command mode

(3) In command mode, press i, a and other keys to enter edit mode

(4) In edit mode, press esc to return to command mode

working mode of vim

Vim generally has 6 working modes.

  • Normal mode: The default mode when using vim to open a file, also called command mode, allows users to browse code, scroll and other operations through various commands.
  • Insert mode: It can also be called edit mode. Press i, a or o in normal mode to enter insert mode, allowing users to input and edit via keyboard.
  • Command line mode: In normal mode, first enter a colon:, and then enter a command to configure vim through configuration commands, such as changing the color theme, displaying line numbers, etc. These configuration commands can also be saved to /etc/vim In the /vimrc configuration file, the default configuration is executed every time it is opened.
  • Visualization mode: In the normal mode, press the v key on the front panel to enter the visualization mode, and then move the cursor to select a piece of text, which is often used to complete operations such as copying, pasting, and deleting text.
  • Replacement mode: If we want to modify a certain character, we don’t need to enter the insert mode first, delete it, and then enter a new character. In the normal mode, we can directly replace it by pressing the R key.
  • EX mode: similar to the command line mode, you can run multiple commands at a time

The various working modes of vim can be switched by different keys, and the user uniformly uses the ESC key to return to the normal mode.

Configure the gvim interface

Enter in terminal: gvim ~/.vimrc

Undo changes, redo and save

u:  撤销上一步的操作。
Ctrl+r:  将原来的插销重做一遍
:U  恢复一整行原来的面貌(文件打开时的文本状态)
:q  若文件没有修改,直接退出
:q!  文件已经被修改,放弃修改退出
:wq  文件已经被修改,保存修改并退出
:e!  放弃修改,重新回到文件打开时的状态

cursor movement command

单个字符移动:
h:  向左移动
l:  向右移动
j:  向下移动
k:  向上移动
xh:  向左移动x个字符距离

单词移动:
w:  将光标移动到下一个单词的开头
b:  将光标移动到前一个单词的开头
e:  将光标移动到下一个单词的词末
E:  移动到单词的结尾(忽略标点符号)
ge:  将光标移动到上一个单词的词末
2w:  指定移动的次数

行移动:
$:  将光标移动到当前行的行尾
0:  将光标移动到当前行的行首
^:  将光标移动到当前行的第一个非空字符(行首和当前行非空字符不是一个位置)
2|:  移到当前行的第2列
fx:  将光标移动到当前行的第一个字符x上
3fx: 将光标移动到航前行的第3个字符x上
tx:   将光标移动到目标字符x的前一个字符上
fx和tx可以通过;和,进行重复移动,一个是正向重复,一个是反向重复
%:  用于符号间的移动,它会在一对()、[]、{}之间跳跃

文本块移动:
(:  移到当前句子的开头
):  移到下一个句子的开头
{:  移到当前一段的开头
}:  移到下一段的开头
[[:  移到当前这一节的开头
]]:  移到下一节的开头

在屏幕中移动
xG:  跳转到指定的第x行,G移动到文件按末尾,``(2次单引号)返回到跳转前的位置
gg:  移动到文件开头
x%:  移动到文件中间,就使用50%
H:  移动到home
M:  移动到屏幕中间
L:  移动到一屏末尾
ctrl+G:  查看当前的位置状态

text insertion

i:  在当前光标的前面插入字符
a:  在当前光标的后面追加字符
o:  在当前光标的下一行行首插入字符
I:  在一行的开头添加文本
A:  在一行的结尾处添加文本
O:  在光标当前行的上一行插入文本
s:  删除当前光标处的字符并进入到插入模式
S:  删除光标所在处的行,并进入到插入模式
u:  撤销修改

Text delete operation

字符删除
x:  删除当前光标所在处的字符
X:  删除当前光标左边的字符

单词删除
dw:  删除一个单词(从光标处到空格)
daw:  无论光标在什么位置,删除光标所在的整个单词(包括空白字符)
diw:  删除整个单词文本,但是保留空格字符不删除
d2w:  删除从当前光标开始处的2个单词
d$:  删除从光标到一行末尾的整个文本
d0:  删除从光标到一行开头的所有单词
dl:  删除当前光标处的字符=x
dh:  删除当前光标左边的字符=X

行删除
dd:  删除当前光标处的一整行=D
5dd:  删除从光标开始处的5行代码
dgg:  删除从光标到文本开头
dG:  删除从光标到文本结尾

行合并
J:  删除一个分行符,将当前行与下一行合并

Text copy, cut and paste

y:  复制,p:粘贴
yw:  复制一个单词
y2w:  复制2个单词
y$:  复制从当前光标到行结尾的所有单词
y0:  复制从当前光标到行首的所有单词
yy:  复制一整行
2yy:  复制从当前光标所在行开始的2行

复制文本块
    1.首先进入visual模式:v
    2.移动光标选择文本
    3.复制与粘贴的操作

Text modification and replacement

cw:  删除从光标处到单词结尾的文本并进入到插入模式
cb:  删除从光标处到单词开头的文本并进入到插入模式
cc:  删除一整行并进入到插入模式
~: 修改光标下字符的大小写
r:  替换当前光标下的字符
R:  进入到替换模式
xp:  交换光标和下一个字符

Multi-window operation

分割窗口
    split/vsplit filename
窗口间跳转
    ctrl+w hjkl
    ctrl+w w
移动窗口
    ctrl+w HJKL
调整窗口尺寸
    ctrl+w +/-  调整窗口的高度
    ctrl+w </>  调整窗口的宽度
    ctrl+w = 所有的窗口设置相同的尺寸
    :resize n将当前窗口尺寸调整为N行
关闭窗口
    close: 关闭一个窗口
    qall: 退出所有窗口
    qall!: 放弃修改,退出所有窗口
    wqall: 保存并退出所有窗口
    wall: 保存所有窗口

 The operation of vim is complicated, and it needs to be used frequently to be proficient. It is also a preliminary contact at present, so that the records can be easily viewed in the future.

Guess you like

Origin blog.csdn.net/m0_61298445/article/details/128524483
Recommended