Common operations of Linux vi
How to jump to a specified number of lines in vi
method 1:
Enter: n, which means jump to the nth line, such as: 79, it will jump to the 79th line.
Method 2:
Enter nG to jump to the nth line, such as 79G, to jump to the 79th line.
) : move the cursor to the end of the sentence
( : move the cursor to the beginning of the sentence
}: Move the cursor to the last letter of the paragraph
{: move the cursor to the first letter of the paragraph
nG: move the cursor to the beginning of the nth line
n+: move the cursor down n lines
n-: move the cursor up n lines
n$: move the cursor to the end of the nth line
H : Move the cursor to the top line of the screen
M : Move the cursor to the middle line of the screen
L : Move the cursor to the last line of the screen
0: (note the number zero) move the cursor to the beginning of the current line
$: Transition tail
vi operation
1. Jump to the last line of text: press "G", that is, "shift+g"
2. Jump to the last character of the last line: First repeat the operation of 1, that is, press "G", then press the "$" key, that is, "shift+4".
3. Jump to the first character of the first line: first press "g" twice,
4. Jump to the first character of the current line: Press "0" in the current line.
5.vi encryption. Enter vi, enter ":" + "X" and you will be prompted to enter the password twice. After: wq save and exit. When you log in again, you will be prompted to enter your password. If you don't want the password anymore, just press enter twice in a row when :X prompts you to enter the password, and you're done. Ha ha.
basic replacement
:s/vivian/sky/ Replace the first vivian of the current line with sky
:s/vivian/sky/g replace all vivian in the current line with sky
:n,$s/vivian/sky/ Replace the first vivian of each line from the nth line to the last line with sky
:n,$s/vivian/sky/g Replace all vivians with sky from the beginning of the nth line to the last line
(n is a number, if n is ., it means from the current line to the last line)
:%s/vivian/sky/ (equivalent to :g/vivian/s//sky/) replace the first vivian of each line with sky
:%s/vivian/sky/g (equivalent to :g/vivian/s//sky/g) replace all vivian in each line with sky
如果替换的文本中的"/"等特殊字符,则需要转义:"\/"
参考:http://www.cnblogs.com/tovep/articles/2715803.html
Linux vi 删除多行
dd 删除一行
d$ 删除以当前字符开始的一行字符
ndd 删除以当前行开始的n行
dw 删除以当前字符开始的一个字
ndw 删除以当前字符开始的n个字
D 与d$同义
d) 删除到下一句的开始
d} 删除到下一段的开始
d回车 删除2行
vim编辑器的命令模式中常用的快捷键
命令 | 作用 |
dd | 删除(剪切)光标所在整行。 |
5dd | 删除(剪切)从光标处开始的5行。 |
yy | 复制光标所在整行。 |
5yy | 复制从光标处开始的5行。 |
p | 将之前删除(dd)或复制(yy)过的数据粘贴到光标后。 |
/字符串 | 在文本中从上至下搜索该字符串。 |
?字符串 | 在文本中从下至上搜索该字符串。 |
n | 显示搜索命令定位到的下一个字符串。 |
N | 显示搜索命令定位到的上一个字符串。 |
u | 撤销上一步的操作 |
vim编辑器的末行模式中的常用命令
命令 | 作用 |
:w | 保存 |
:q | 退出 |
:q! | 强制退出(放弃对文本的修改内容) |
:wq! | 强制保存退出 |
:set nu | 显示行号 |
:set nonu | 不显示行号 |
:命令 | 执行该命令 |
:整数 | 跳转到该行 |