Linux file editor of Vim

1.VIM basic overview

1. What is VIM?

vi and vim is a text editor under Linux. (It can be understood as windows notepad or word document)

2. Why use VIM?

Since everything is Linux file system, and most of our work is to modify the configuration of a service (in fact, is to modify the contents of the file)
that is, if there is no vi / vim, we have a lot of work can not be completed. PS: vim learning is one of the most important command linux

3.VI and VIM What is the difference?

  • vi and vim is a text editor, vi is just an enhanced version of vim, syntax highlighting more than vi, other editing functions almost no difference, so use vi or vim depends on personal habits. (Equivalent to "Notepad" distinguished "notepad ++" text editing software under windows system)
    PS:因为前期最小化安装CentOS系统,所以默认情况下没有vim命令,但可以使用yum install vim -y安装

    4. How to use VIM editor?

  • Before using VIM, we need to introduce three models under the VIM: normal mode, edit mode, command modes, each respectively support a wide variety of shortcut keys, in order to efficiently manipulate text, you must first find out the method of switching between the three modes of operation modes and the difference
    image.png
    Summary: vim open file edit overall process is as follows:
    1. open the file in the normal mode by default
    2. switching from the normal mode to the edit mode requires the use of a, I, O the AIO
    . 3 edit mode modified after the ECS need to use a return to normal mode
    4. enter the normal mode ":" or "/" command mode, and may be implemented to save the file back
    out.
    PS: In vim, you can not switch from command mode to edit mode

    Normal mode vim

1.命令光标跳转
G       光标跳转至文件末端
gg      光标跳转至文件顶端
Ngg     光标跳转至当前文件内的N行
$       光标跳转至当前光标所在行的尾部
^|0     光标跳转至当前光标所在行的首部
-------------------------------------------
2.文件内容较多
ctrl+f   往下翻页(行比较多)
ctrl+b   往上翻页
-------------------------------------------
3.复制与粘贴 yy p
yy       复制当前光标所在的行
5yy      复制当前光标以及光标向下4行
p(小写)   粘贴至当前光标下一行 
P(大写)   粘贴至当前光标上一行
-------------------------------------------
4.删除、剪贴、撤销 
dd     删除当前光标所在的行
4dd    删除当前光标所在的行以及往下的3行
dG     删除当前光标以后的所有行
D      删除当前光标及光标以后的内容 
x      删除当前光标标记往后的字符
       删行,删除行中的一部分,删除单个字符
u      撤销上一次的操作
dd & p 剪贴、先删除dd(number dd),后粘贴p
-------------------------------------------
5.替换
r      替换当前光标标记的单个字符

vim editing mode

2.编辑模式(从普通模式进入到编辑模式)*
i  进入编辑模式,光标不做任何操作
a  进入编辑模式,将当前光标往后一位
o  进入编辑模式,并在当前光标下添加一行空白内容
-------------------------------------------
I  进入编辑模式,并且光标会跳转至本行的头部
A  进入编辑模式,将光标移动至本行的尾部
O  进入编辑模式,并在当前光标上添加一行空白内容

vim command mode

3.命令模式,主要用于搜索, 保存, 退出文件
i  进入编辑模式,光标不做任何操作
a  进入编辑模式,将当前光标往后一位
o  进入编辑模式,并在当前光标下添加一行空白内容
-------------------------------------------
I  进入编辑模式,并且光标会跳转至本行的头部
A  进入编辑模式,将光标移动至本行的尾部
O  进入编辑模式,并在当前光标上添加一行空白内容
1.文件保存与退出
:w    保存当前状态
:q    退出当前文档(文档必须保存才能退出)
:wq   先保存,在退出
:w!   强制保存当前状态
:q!   强制退出文档不会修改当前内容
:wq!  强制保存并退出
:x    先保存,在退出
ZZ    保存退出, shfit+zz
:number 跳转至对应的行号
文件保存---->>> :wq 保存并退出 :wq! 强制保存并退出
-------------------------------------------
2.文件内容查找
/string    需要搜索的内容(查找)
n          按搜索到的内容依次往下进行查找
N          按搜索到的内容依次往上进行查找
-------------------------------------------
3.文件内容替换
:1,5s#sbin#test#g  替换1-5行中包含sbin的内容为test
:%s#sbin#test#g    替换整个文本文件中包含sbin的替换为test
-------------------------------------------
4.文件内容另存
:w /root/test.txt  将所有内容另存为/root/test.txt文件中
-------------------------------------------

vim view mode

4.视图模式(从普通模式进入视图模式),主要进行批量操作
ctrl+v 进入可视块模式,选中需要注释的行
 1.插入:按shift+i进入编辑模式,输入#,结束按ESC键
 2.删除:选中内容后,按d键删除
 3.替换:选中需要替换的内容, 按下r键,然后输入替换后的内容
-------------------------------------------
shift+v 进入可视行模式,选中整行内容
 1.复制:选中行内容后按y键及可复制。
 2.删除:选中行内容后按d键删除。

vim expansion of knowledge

1.环境变量临时生效
:set nu       显示行号
:set ic       忽略大小写, 在搜索的时候有用
:set ai       自动缩进
:set list     显示制表符(空行、tab键)
:set no[nu|ic|ai…]  取消临时设定的变量
2.环境变量永久生效。~/.vimrc 个人环境变量(优先级高) /etc/vimrc 全局环境变量
vim ~/.vimrc               当下次再打开文件自动显示行号并忽略大小写
set nu
set ic
如果个人vim环境没有配置, 则使用全局vim环境变量配置。
如果个人vim环境和全局环境变量产生冲突, 优先使用个人vim环境变量。
3.如何同时编辑多个文件
vim -o file1 file2  水平分割
vim -O file1 file2  垂直分割
ctrl+ww 文件间切换
4.相同文件之间差异对比,通常用于对比修改前后差异
 diff     文件对比 
 vimdiff  以vim方式打开两个文件对比,高亮显示不同的内容
5.如果VIM非正常退出 (ctrl+z)挂起或强制退出终端没关闭VIM后
假设打开filename文件被以外关闭,需要删除同文件名的.swp文件即可
解决
# rm -f .filename.swp

Guess you like

Origin www.cnblogs.com/yinwu/p/11412015.html