Operation and maintenance 10 vim editor

1. Install

yum -y install vim

2. Basics

a. Normal mode

1.命令光标跳转
G       #光标跳转至末端(文件的尾部)
gg      #光标跳转至顶端
Ngg     #光标跳转至当前文件内的N行   (指定到光标跳转至多少行) 50gg

ctrl+f  #往下翻页(行比较多)
ctrl+b  #往上翻页

$       #光标跳转至当前光标所在行的尾部  (只是跳转,并不会进入编辑模式)
^|0     #光标跳转至当前光标所在行的首部


-------------------------------------------
2.复制与粘贴
yy      #复制当前光标所在的行
5yy     #复制当前光标以及光标向下4行
 
p(小写)   #粘贴至当前光标下一行   
P(大写)   #粘贴至当前光标上一行
-------------------------------------------
3.删除、剪贴、撤销  
dd      #删除当前光标所在的行   
4dd     #删除当前光标所在的行以及往下的3行
dG      #删除当前光标以后的所有行
D       #删除当前光标及光标以后的内容  
x       #删除当前光标标记往后的字符
X       #删除当前光标标记往前的字符
dd & p  #剪贴、先删除dd(number dd),后粘贴p
u       #撤销上一次的操作
-------------------------------------------
4.替换
r       #替换当前光标标记的单个字符
R       #进入REPLACE模式, 连续替换,ESC结束

b. Edit mode (entered from the normal mode to the edit mode)

i   #进入编辑模式,光标不做任何操作		
a   #进入编辑模式,将当前光标往后一位	
o   #进入编辑模式,并在当前光标下添加一行空白内容  

I   #进入编辑模式,并且光标会跳转至本行的头部
A   #进入编辑模式,将光标移动至本行的尾部		
O   #进入编辑模式,并在当前光标上添加一行空白内容

c. the last line mode, used to search, save and exit the file.

1. Save the file and exit

:w      保存当前状态
:q      退出当前文档(文档必须保存才能退出)
:w!     强制保存当前状态
:q!     强制退出文档不会修改当前内容
:wq     先保存,在退出		:x      先保存,在退出
:wq!    强制保存并退出
:number 跳转至对应的行号

We went to edit a file, and sometimes there will be a network outage, or himself pressed the ctrl + z, resulting in abnormal circumstances.
When you edit a file, you can select the r key. Restored to modify the state. Key can be selected e. Did not recover files saved state.
After the recall file recovery, remember to get rid of the .xxx swp files (or move away)

2. Find the file contents

/string #需要搜索的内容(查找)
n       #按搜索到的内容依次往下进行查找
N       #按搜索到的内容依次往上进行查找

3. Replace the contents of the file

:1,5s#sbin#test#g   #替换1-5行中包含sbin的内容为test
:%s#sbin#test#g     #替换整个文本文件中包含sbin的替换为test

4. Save the file contents

:w /root/test.txt  #将所有内容另存为/root/test.txt文件中

The contents of the file read

:r  /etc/hosts  #读入/etc/hosts文件至当前光标下面
:5r /etc/hosts  #指定插入/etc/hosts文件至当前文件的第五行下面

3. Advanced Features

View mode (to enter the view mode from the normal mode), the main bulk operations

shift+v 进入可视行模式,选中整行内容
    1.复制:选中行内容后按y键及可复制。
    2.删除:选中行内容后按d键删除。

ctrl+v  进入可视块模式,选中需要注释的行
    1.插入:按shift+i进入编辑模式,输入#,结束按ESC键
    2.删除:选中内容后,按x或者d键删除
    3.替换:选中需要替换的内容, 按下r键,然后输入替换后的内容

4. Environment Variables

Provisional entry into force

set number   	显示行号
set ic 			忽略大小
set ai			自动缩进
set list		显示制表符(空行.tab键)
set no			【nu|ic|ailist....】

Permanent
~ / .vimrc personal environment variable takes precedence over the / etc / vimrc global environment variables

5. Multiple Files

Edit multiple files
vim -O file1 file2 vertical dividing
vim -o file1 file2 horizontal division
Comparison of different files plurality
vim -d file1 file2 or vimdiff file1 file2

Published 22 original articles · won praise 0 · Views 294

Guess you like

Origin blog.csdn.net/weixin_44648034/article/details/104803645