Day 7 vim file editing with vim use

1. What is the vim?

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

2. Why use vim?

Because everything is linux file system, and most of our work is to modify a configuration of a service (in fact, file contents).
This means that if there is no vi / vim, we have a lot of work can not be completed.

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 the same.

4. How to use vim editor

Summary: vim editor to open the file overall process is as follows:
1. Open the file in the default normal mode
2. Switching from the normal mode to the edit mode requires the use of a, i, o AIO
3. After you are finished editing mode, you need to use ECS to 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. The cursor jumps command
G       光标跳转至文件末端
gg      光标跳转至文件顶端
Ngg     光标跳转至当前文件内的N行
$       光标跳转至当前光标所在行的尾部
^|0     光标跳转至当前光标所在行的首部

2. The content of the document more
ctrl+f   往下翻页(行比较多)
ctrl+b   往上翻页

3. Copy and paste yy p
yy       复制当前光标所在的行
5yy      复制当前光标以及光标向下4行
p(小写)   粘贴至当前光标下一行 
P(大写)   粘贴至当前光标上一行

4. Remove, cut and paste, undo
dd     删除当前光标所在的行
4dd    删除当前光标所在的行以及往下的3行
dG     删除当前光标以后的所有行
D      删除当前光标及光标以后的内容 
x      删除当前光标标记往后的字符
       删行,删除行中的一部分,删除单个字符
u      撤销上一次的操作

dd & p clip, delete dd (number dd), after pasting p

5. Replace
 r      替换当前光标标记的单个字符
vim editing mode
2.编辑模式(从普通模式进入到编辑模式)*
i  进入编辑模式,光标不做任何操作
a  进入编辑模式,将当前光标往后一位
o  进入编辑模式,并在当前光标下添加一行空白内容

I  进入编辑模式,并且光标会跳转至本行的头部
A  进入编辑模式,将光标移动至本行的尾部
O  进入编辑模式,并在当前光标上添加一行空白内容
vim command mode
3.命令模式,主要用于搜索, 保存, 退出文件
 i  进入编辑模式,光标不做任何操作
a  进入编辑模式,将当前光标往后一位
o  进入编辑模式,并在当前光标下添加一行空白内容

  I  进入编辑模式,并且光标会跳转至本行的头部
 A  进入编辑模式,将光标移动至本行的尾部
 O  进入编辑模式,并在当前光标上添加一行空白内容
1. Save the file and exit
 :w    保存当前状态
 :q    退出当前文档(文档必须保存才能退出)
 :wq   先保存,在退出
 :w!   强制保存当前状态
 :q!   强制退出文档不会修改当前内容
 :wq!  强制保存并退出
  :x    先保存,在退出
  ZZ    保存退出, shfit+zz
 :number 跳转至对应的行号

#### File Save ---- >>>: wq save and quit: wq forced to save and exit!

##### 2. Find the file content
/ string require content search (find)
the n-by searched contents in order to find down
N content by search to turn up to find
--------- ----------------------------------
. ##### 3 replaces file
: 1,5s # sbin # test # g row contains 1-5 replace the contents of sbin Test
: S #% # test # g sbin alternatively replace the entire text file is included sbin Test
-------------- -----------------------------

4. Save the file contents

: W /root/test.txt the entire contents of a file saved as /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. environment variables permanent. ~ / .Vimrc personal environment variables (high priority) / etc / vimrc global environment variables
vim ~/.vimrc               当下次再打开文件自动显示行号并忽略大小写
set nu
set ic
如果个人vim环境没有配置, 则使用全局vim环境变量配置。
如果个人vim环境和全局环境变量产生冲突, 优先使用个人vim环境变量。
3. How to edit multiple files simultaneously
vim -o file1 file2  水平分割
vim -O file1 file2  垂直分割
ctrl+ww 文件间切换
4. The contrast between the same file, typically used to modify the difference before and after comparison
 diff     文件对比 
 vimdiff  以vim方式打开两个文件对比,高亮显示不同的内容
5. If VIM exits abnormally (ctrl + z) is not suspended or forced off after withdrawal terminal VIM
  假设打开filename文件被以外关闭,需要删除同文件名的.swp文件即可
solve
 rm -f .filename.swp

Guess you like

Origin www.cnblogs.com/baozexu/p/11372807.html