Vim text editing tool

Introduction to Vim

vim is an upgraded version of vi. For example, when vim edits text, it will display colors, while vi does not display colors.

yum install vim

[root@centos-01 ~]# vim
-bash: vim: 未找到命令
[root@centos-01 ~]# yum install -y vim-enhanced

Three modes of vim

There are three modes of vim: general mode, editing mode, and command mode.

  1. Normal mode : When vim filenameediting a file, it is normal mode as soon as you enter the file. In this mode, you can do the following operations: move the cursor up and down; delete a character; delete a line; copy, paste one or more lines.
  2. Edit mode : In normal mode, it is not possible to modify a character. From normal mode to edit mode, you just need to press a key (i, I, a, A, o, O, r, R). When entering edit mode, the words "INSERT" or "REPLACE" will appear on the bottom line of the screen. From edit mode back to normal mode, just press the ESC key at the top left of the keyboard.
  3. Command mode : In normal mode, enter ":" or "/" to enter command mode. In this mode, you can search for a character or string, save, replace, exit, display line numbers, etc.

Copy the file for practice

[root@centos-01 ~]# yum install -y dnsmasq
[root@centos-01 ~]# cp /etc/dnsmasq.conf /tmp/1.txt
[root@centos-01 ~]# vim /tmp/1.txt

Move the cursor in normal mode

button behavioral manipulation
h / left arrow key Move the cursor one character to the left
j / down arrow key Move the cursor down one character
k / up arrow key Move the cursor up one character
l (lowercase L) / right arrow key / space bar Move the cursor one character to the right
Ctrl+f / PageUp键 Move the screen forward one page
Ctrl+b / PageDown键 Move the screen back one page
Number 0 / Shift+6 Move to the beginning of the line
Shift+4 move to end of line
gg move to the first line of this text
G Move the cursor to the end of the text
nG (n is a number) move to the nth line of this text
Ctrl+d Move the screen forward by half a page
Ctrl+u Move the screen back half a page
n spaces/nl (n is a number) Press the number and then press space/l, the cursor moves to the right by n characters; if the line of characters is less than n, the cursor continues to move to the right from the bottom line until n

Copy, cut and paste in normal mode

button behavioral manipulation
dd delete/cut the line where the cursor is
ndd (n is a number) Delete/cut n lines starting from the line where the cursor is located
yy Copy the line where the cursor is located
nyy Start at the line where the cursor is and copy down n lines
p From the line where the cursor is located, paste the copied content down
P Paste the copied content upwards, starting from the line where the cursor is located
u Redo the previous operation
Ctrl+r Cancel the restore of the previous step
x Delete/cut one character backward
X Delete/cut one character forward
v After pressing v, moving the cursor will select the specified character, and then you can copy, cut and other operations of the specified character

enter edit mode

button behavioral manipulation
i Insert character before current character
I Insert character at the beginning of the current line
O Insert a new line below the current line
O Insert a new row on the current row
a Insert character after current character
A Insert character at the end of the current line

command mode

button behavioral manipulation
/word Look for a string named word behind the cursor, when the first word is found, press "n" to continue searching for the next one, and "Shift+n" to search for the previous one
?word Look for a string named word before the cursor, when the first word is found, press "n" to continue searching for the previous one
:n1,n2s/word1/word2/g Find the string word1 between lines n1 and n2 and replace it with word2
:1,$s/word1/word2/g From the first line to the last line, find word1 and replace with word2
:1,$s/word1/word2/gc The effect of adding c is to require user confirmation before replacing

If the string to be searched/replaced contains a "\" symbol, you can add a "/" symbol before the "\" symbol, for example, to find "/etc/hosts", you can do this with "/\/etc\/hosts". When searching and replacing a string, the string contains the "\" symbol. You can also replace "/" with "#" or "@", such as ":1,$s#word1#word2#g".

button behavioral manipulation
: set no Display line number at the beginning of each line
:set nonu cancel line number
:nohl unhighlight
:w save edited text
:w! If the text property is read-only, force saving
:q quit
:q! Exit without saving, whether edited or unedited
:wq save and exit
:x Execute ":x" after editing the file, the effect is the same as ":wq". If you open the file for viewing, and nothing has changed, ":wq" will change mtime, and ":x" will not change mtime.

Practice with Vim

[root@centos-01 ~]# cp /etc/dnsmasq.conf /tmp/1.txt
[root@centos-01 ~]# vim /tmp/1.txt
  1. Move down, right, left, and up by 6 characters ( 6j, 6l, 6h, 6k)
  2. 分别向下、向上翻两页(分别按两次Ctrl+fCtrl+b
  3. 把光标移动到第49行(49G
  4. 把光标移动到行尾,再移动到行首(Shift+4Shift+6
  5. 移动到文件的最后一行(G
  6. 移动到文件的首行(gg
  7. 搜索文件中出现的“dnsmasq”(/dnsmasq
  8. 把从第1行到第10行出现的dnsmasq替换成dns(:1,10s/dnsmasq/dns/g
  9. 还原上一步操作(u
  10. 把整个文件中所有的etc替换成cte(:1,$s/etc/cte/g
  11. 还原上一步操作(u
  12. 把光标移动到第37行,删除字符“ly”(37G然后按l向右移动光标找到“ly”,按v选中指定字符再删除)
  13. 还原上一步操作(u
  14. 删除第50行(50G dd
  15. 还原上一步操作(u
  16. 删除第37-42行的所有内容(37G 6dd
  17. 还原上一步操作(u
  18. 复制第48行的内容并粘贴到第52行下面(48G yy 52G p
  19. 还原上一步操作(u
  20. 复制第37-42行的内容并粘贴到第45行上面(37G 6yy 45G P
  21. 还原上一步操作(u
  22. 把第37-42行的内容移动到第19行下面(37G 6dd 19G p
  23. 还原上一步操作(需要按两次u,因为先是剪切再粘贴)
  24. 把光标移动到文本首行,把第1行内容改为“#!/bin/bash”(先按gg,把光标定位到第1行,然后按字母A,进入编辑模式,Ctrl+u删除当前行内容,进行修改操作,完成后按ESC
  25. 在第1行下面插入新的一行,并输入“# Hello!”(按o进入编辑模式,同时光标向下另起一行,输入“# Hello!”)
  26. 保存文档并退出(按ESC键,输入“:wq”)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325729553&siteId=291194637