1 vim repeat command
1.1 repeat the last command
In the normal mode .
(decimal) represents repeat the last command operation
A copy of the test file to a local directory
$ cd /home/shiyanlou
$ cp /etc/protocols .
Open the file for editing
$ vim protocols
Enter the normal mode x
, delete the first character input .
(decimal point) deletes a character again, and in addition can also duplicate dd
deletion
1.2 execute the same command specified number
Into normal mode inputs N<command>
, N represents the number of repetitions of the back, to practice the following:
Open the file for editing
$ vim protocols
Below you can do the following exercises in order:
- Enter
10x
, Delete 10 consecutive characters - Input
3dd
, 3 lines of text will be removed
In normal mode, you can also use dw
or daw
(delete a word) delete a word, so you can easily think of dnw
( n replaced with the corresponding digital representation delete n words)
Quick Jump 2 cursors
In normal mode, the following command allows the cursor to quickly adjust to the specified location, we discuss the rapid realization of inter-row jump and inline jump
Jump between 2.1 line
command | Explanation |
---|---|
nG (n Shift+g) |
Cursor to the n-th row (if not the default line number, please enter command mode, the input :set nu to line numbers) |
gg |
To move the cursor to the first row |
G (Shift+g) |
To the last line |
Or to practice it:
Use vim to open the document practice
$ vim protocols
In order to do the following exercises:
- Quick jump to line 9, and then delete the row
- Back to the first line, delete 8 rows down
- Go to the end of the document, and then delete the row
Tip: You jump after completing the turn, can be used Ctrl+o
to quickly return to (before the jump) last cursor position , this technique is very useful, such as when you write code, I suddenly remembered there is a bug, you need to modify this when you jump over changing for the better, just press Ctrl+o
you can return to your previous position. vim will be waiting for you to discover with many similar tips.
2.2 Line Jump
Use the following command line in the normal mode to jump to the unit in accordance with the word
command | Explanation |
---|---|
w |
To the beginning of the next word |
e |
To the end of the current word |
b |
To the beginning of the previous word |
ge |
To the end of the previous word |
0 or^ |
To outfit |
$ |
To the end of the line |
f<字母> |
Search backwards <letter> and jumps to the first matching position (useful) |
F<字母> |
Search forward <letter> and jumps to the first matching position |
t<字母> |
Search backwards <letters> letter and jumps to a position before the first match (not used) |
T<字母> |
Search forward <letter> and jump to the letter after the first match position (not used) |
In order to do the following exercises:
- In normal mode, jump to any line, use
w
jumps to the start of a word, thendw
delete the word - In normal mode,
e
jumps to the end of a word, and use~
the cursor letters become uppercase or lowercase
3 and Cut Copy Paste
3.1 Copy and paste text
-
Normal mode is used
y
to copy- In the normal mode,
yy
copying a full-line cursor (3yy
represented by row 3 copy) - Normal mode,
y^
copied to the beginning of the line, ory0
. Excluding the character at the cursor. - In the normal mode,
y$
copied to the end of the line. With the character at the cursor. - Normal mode,
yw
copy a word. - Normal mode,
y2w
copy the two words. - 普通模式中,
yG
复制至文本末。 - 普通模式中,
y1G
复制至文本开头。
- In the normal mode,
-
普通模式中使用
p
粘贴- 普通模式中,
p
(小写)代表粘贴至光标后(下) - 普通模式中,
P
(大写)代表粘贴至光标前(上)
- 普通模式中,
打开文件进入普通模式练习上述命令,这会儿你就可以随意 yy
了。
$ vim protocols
3.2 剪切及粘贴
其实前面讲得 dd
删除命令就是剪切,你每次 dd
删除文档内容后,便可以使用 p
来粘贴,也这一点可以让我们实现一个很爽快的功能——交换上下行:ddp
,就这么简单,即实现了快速交换光标所在行与它下面的行
4 字符的替换及撤销(Undo操作)
替换和Undo命令都是针对普通模式下的操作
命令 | 说明 |
---|---|
r +<待替换字母> |
将游标所在字母替换为指定字母 |
R |
连续替换,直到按下Esc |
cc |
替换整行,即删除游标所在行,并进入插入模式 |
cw |
替换一个单词,即删除一个单词,并进入插入模式 |
C (大写) |
替换游标以后至行末 |
~ |
反转游标所在字母大小写 |
u {n} |
撤销一次或n次操作 |
U (大写) |
撤销当前行的所有修改 |
Ctrl+r |
redo,即撤销undo的操作 |
打开文件进行编辑:
$ vim practice_2
# 输入以下文本
www.shiyanlou.com
然后依次进行如下操作
- 输入
fa
跳转到第一个a
字符 - 输入
r
,并且输入b,a字符被b字符替换(实用) - 输入
R
替换字符,输入新字符串,输入完按ESC回到普通模式(实用) - 输入
cc
替换整行字符,输入新字符串,输入完按ESC回到普通模式 - 输入
cw
替换一个英文字(word),输入完按ESC回到普通模式(实用) - 输入
~
,翻转游标所在字符的大小写 - 输入
C
替换至行尾,即游标所在处以后的字都会被替换,输入完按ESC回到普通模式 - 输入
u
撤销上一次的操作
最后可以尝试下输入更多行的文本内容,并进行跳转到指定行的操作:
- 输入
2G
,跳转到 2 行
5 快速缩进
5.1 使用命令进行快速调整缩进操作
这一小节学习如何在vim中进行快速缩进,缩进操作均在普通模式下有效
打开文件进行编辑
$ vim protocols
- 普通模式下输入
15G
,跳转到15行 - 普通模式下输入
>>
整行将向右缩进(使用,用于格式化代码超爽) - 普通模式下输入
<<
整行向左回退 - 普通模式下输入
:
进入命令行模式下对shiftwidth
值进行设置可以控制缩进和回退的字符数
5.2 shiftwidth命令
shiftwidth
命令是指上一节>>
命令产生的缩进(可以简写成sw
) 普通模式下输入:
进入命令行模式下对shiftwidth
值进行设置可以控制缩进和回退的字符数 获取目前的设定值
:set shiftwidth?
设置缩进为10个字符
:set shiftwidth=10
输入 ESC
回到普通模式,再次尝试 >>
看缩进量是否变化
5.3 调整文本位置
命令行模式下输入:ce
(center)命令使本行内容居中
:ce
命令行模式下输入:ri
(right)命令使本行文本靠右
:ri
命令行模式下输入:le
(left)命令使本行内容靠左
:le
6 查找
6.1 快速查找
普通模式下输入 /
然后键入需要查找的字符串 按回车后就会进行查找。 ?
与/
功能相同,只不过 ?
是向上而 /
是向下查找。 进入查找之后,输入n
和 N
可以继续查找。 n
是查找下一个内容,N
查找上一个内容。
6.2 快速查找练习
使用 vim 打开文件进行编辑(搜索高亮需要在配置文件 .vimrc
中设置 set hls
,实验环境中已经设置好了)
$ vim protocols
- 普通模式下输入
/icmp
然后回车即可查找字符串icmp
- 普通模式下输入
n
查找下一个icmp
- 普通模式下输入
?tcp
向上查找字符串tcp
- 普通模式下输入
N
查找上一个出现的tcp
- 命令行模式下输入
noh
然后回车即可取消搜索
6.3 高级查找
- 普通模式下输入
\*
寻找游标所在处的单词 - 普通模式下输入
\#
同上,但\#
是向前(上)找,\*
则是向后(下)找 - 普通模式下输入
g\*
同\*
,但部分符合该单词即可 - 普通模式下输入
g\#
同\#
,但部分符合该单词即可
以上查找n
,N
的继续查找命令依然可以用