Linux basic study notes-VIM program editor

VIM program editor

1. Use of vi

Basically, vi is divided into three modes, namely: general command mode, edit mode and command line command mode .

  • General command mode

    Open a file with vi to directly enter the general command mode (this is the default mode, also referred to as general mode for short). In this mode, you can move the cursor “up, down, left , and right”, use “delete characters” and “delete entire lines” to process files, or use “copy and paste” to process your file data .

  • Edit mode

    In the normal mode, you can delete, copy, paste, etc., but you cannot edit the file content! You have to wait until you press any of the keys "i, I, o, O, a, A, r, R" to enter the editing mode.

  • Command line command mode

    In the normal mode, input any one of the three keys ": / ?" to move the cursor to the bottom column . In this mode, the action of "searching for data" can be provided, and tasks such as reading, saving, replacing characters, leaving vi, and displaying line numbers are all completed in this mode .

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-JLtID3xx-1596852243831) (E:\study notes\Linux\Linux learning\vi mode switch.png)]

1.1. Description of keys available in general command mode, cursor movement, copy and paste, search and replace, etc.

How to move the cursor
Arrow keys up down left right
[Page Down/Up] Page down/up
n<space> n stands for number. Press the number and then the space, the cursor will move to the right for n characters in this line
0 or [Home] Move to the top of this line
$ Or [End] Move to the end of this line
G Move to the last line of this file
nG n is a number. Move to the nth line of this file
gg Move to the first line of this file
n<Enter> n is a number. Cursor down n lines
Search and replace
/word Look for a string named word under the cursor
?word Look for a string named word above the cursor
n Repeat the previous action
N Repeat the previous action in reverse
:n1,n2s/word1/word2/g n1 and n2 are numbers. Find the character string word1 between n1 and n2, and replace this character with word2!
:1,$s/word1/word2/g Same as above from the first line to the last line
:1,$s/word1/word2/gc Prompt the user to confirm before replacing (confirm)
Delete, copy and paste
dd Delete the line where the cursor is
ndd Delete the n rows down where the cursor is
yy Copy the line where the cursor is
nyy Copy the n lines down from the line where the cursor is
p,P Paste the copied data in the next line of the cursor, and P is the previous line
u Undo the previous action
[Ctrl]+r Repeat the previous action

1.2. Description of available keys for switching from general command mode to edit mode

Enter insert or replace edit mode
i,I Enter insert mode
i means " insert from the current cursor position"; I means "insert at the first non-space character in the current line"
a,A Enter the insert mode
a is " Insert from the next character where the cursor is currently", A is "Insert from the last character of the line where the cursor is"
o, O Enter insert mode
o means "insert a new line at the current cursor position", O means "insert a new line at the current cursor position"
r,R Entering the replace mode
r will only replace the character where the cursor is located once, and R will always replace the file where the cursor is, until the ESC key is pressed

1.3. Description of available buttons for switching from general command mode to command line mode

Instructions for storing and leaving the command line mode
:w Write the edited file to the hard disk
:w! If the file attribute is "read only", it is mandatory to write. But ultimately it has to do with file permissions
:q Leave vi
:q! Do not save, force leave
:wq Save away
ZZ If the file is changed, save and leave; if there is no change, leave without saving
:w [文件名] 将编辑的文件保存到另一个文件中
:r [文件名] 在编辑的数据中,读入另一个文件的数据。即将文件的内容加到光标所在的行之后
vi 环境的变更
:set nu 显示行号
:set nonu 取消行号

1.4、VIM 的暂存文件、救援回复与开启时的警告信息

[root@li ~]# cd /tmp/testpw/
[root@li testpw]# vim man_db.conf
						#按下 [Ctrl]-z
[1]+  已停止               vim man_db.conf

[root@li testpw]# ls -al
总用量 2024
drwxr-xr-x.  2 root root     140 8月   8 09:35 .
drwxrwxrwt. 19 root root    4096 8月   8 09:35 ..
-rw-------.  1 root root    4096 8月   8 09:35 .man_db.conf.swp	#暂存文件
...

[root@li testpw]# kill -9 %1
						#中断 vim 的工作
[1]+  已停止               vim man_db.conf
[root@li testpw]# ls -al .man_db.conf.swp	
-rw-------. 1 root root 4096 8月   8 09:35 .man_db.conf.swp	#暂存文件还是会存在
[1]+  已杀死               vim man_db.conf

那么此时编辑 man_db.conf 会出现什么情况呢?

[root@li testpw]# vim man_db.conf
E325: 注意
发现交换文件 ".man_db.conf.swp"
            所有者: root    日期: Sat Aug  8 09:35:51 2020
            文件名: /tmp/testpw/man_db.conf
            修改过: 否
            用户名: root      主机名: li.erver
           进程 ID: 15396
正在打开文件 "man_db.conf"

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    如果是这样,请用 ":recover""vim -r man_db.conf"
    恢复修改的内容 (请见 ":help recovery")。
    如果你已经进行了恢复,请删除交换文件 ".man_db.conf.swp"
    以避免再看到此消息。

交换文件 ".man_db.conf.swp" 已存在!
以只读方式打开([O]), 直接编辑((E)), 恢复((R)), 删除交换文件((D)), 退出((Q)), 中止((A)):

上面 vim 提示两点主要的问题与解决方案:

  • 问题 1 :可能有其他人或程序正在同时编辑这个文件,解决方法是:
    1. 找到另一个程序或人员,请他结束 vim 的工作;
    2. 如果你只是要看该文件的内容并不会修改的话,那么你可以选择开启称为只读文件,即上述最后一行的 O。
  • 问题 2:可能不知名的原因导致 vim 中断:
    1. 如果你之前的 vim 动作没有保存,此时按下 R 按键,此时会加载 .man_db.conf.swp 的内容,然后自行决定是否保存。但是这个暂存文件不会自动删除,必须手动删除,不然每次编辑这个文件,都会出现警告信息。
    2. 如果你确定那个暂存文件是没用的,直接按下 D 按键,删除暂存文件。

2、VIM 的额外功能

2.1、区块选择

区块选择的按键说明
v 字符选择
V 行选择
[Ctrl]+v 区块选择,可以用长方形的方式选择资料
y 复制
d 删除
p 粘贴

2.2、多文件编辑

多文件编辑按键
:n 编辑下一个文件
:N 编辑上一个文件
:files 列出目前这个 vim 的开启的所有文件

2.3、多窗口功能

多窗口功能按键
:sp [文件名] 开启一个新窗口,如果加上文件名,表示在新窗口开启一个新文件,否则表示两个窗口为同一个文件(同步显示)
[Ctrl]+w+j
[Ctrl]+w+↓
先按下 [Ctrl] 不放,再按下 w 后放开所有的按键,然后按下 j 或 ↓
[Ctrl]+w+k
[Ctrl]+w+↑
同上
[Ctrl]+w+q 结束当前窗口

[Ctrl]+w+↑ | 同上 |
| [Ctrl]+w+q | 结束当前窗口 |

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/107875449