Home  >  Article  >  System Tutorial  >  The use of vim under Linux and efficient techniques

The use of vim under Linux and efficient techniques

王林
王林forward
2024-02-10 19:57:17947browse

Today we will introduce the following usage of vim under Linux and efficient techniques

The use of vim under Linux and efficient techniques

Table of contents

1. About vim editor

2. Vim editor mode

3. Basic operations in normal mode

4.Basic operations of V mode (column mode)

5. Basic operations in command mode

6. Customize vim environment

Seven.vim opens multiple files at the same time

8. Compare the contents of two files

1. About vim editor

Vim is a well-known powerful and highly customizable text editor similar to Vi. It has improved and added many features based on Vi. VIM is free software. vim can be used as

An upgraded version of vi, it can display some special information in a variety of colors.

[root@node5 ~]# rpm -qf `which vim`
vim-enhanced-7.4.160-5.el7.x86_64
[root@node5 ~]# rpm -qf `which vi`
vim-minimal-7.4.160-2.el7.x86_64

#As you can see from the output, vim is an enhanced version of vi. The most obvious difference is that vim can highlight syntax and is fully compatible with vi

2. Vim editor mode

1. The vim editor has three modes. The first time you enter is the normal mode. "Insert" that appears in the lower left corner is the editing mode. Input: is the command line mode.

How to switch from edit mode to command line mode? Edit mode->esc->General mode->: ->Command mode
How to enter edit mode from normal mode? Press a or i or o or A or I or O

image-20201010173449952
image-20201010173559526

2. Summary: How to enter other modes of vim?

 a A o O i I 都是可以进行插入,编辑模式
 : 进入命令行模式
 v 进入可视模式
 ctrl+v 进入可视块模式
 V 进入可视行模式
 R 擦除、改写,进入替换模式
 你进入以上模式后,想要退出 ,按esc

3. Basic operations in normal mode

i Insert before the current character (before the cursor)

I 行首插入 (行首)
a 当前字符之后插入 (光标后)
A 行尾插入(行尾)
o下一行插入 (另起一行)
O上一行插入(上一行插入)
x 向后删除一个字符 等同于delete
X 向前删除一个字符
u 撤销一步 每按一次就撤销一次
r 替换, "r"命令不是一个操作符命令。它等待你键入下一个字符用以替换当前光标下的那个字符。"r"命令前辍以一个命令记数是将多个字符都替换为即将输入的那个字符。要把一个字符替换为一个换行符使用"r"。它会删除一个字符并插入一个换行符。在此处使用命令记数只会删除指定个数的字符:"4r"将把4个字符替换为一个换行符。

#Cursor positioning

hjkl 左下上右
0 和 home键表示切换到行首, $和end键表示切换到行尾
gg 快速定位到文档的首行 , G定位到未行
3gg 或者 3G 快速定位到第3行
/string(字符串) -----找到或定位你要找的单词或内容,如果相符内容比较多,我们可以通过N、n来进行向上向下查找,并且vi会对查找到的内容进行高亮显示,取消用 :noh
/^d ----^意思表示以什么开头 ,,查找以字母d开头的内容
/t$ -----$意思表示以什么结尾,,查找以字母t结尾的内容
vim + a.txt 打开文件后,光标会自动位于文件的最后一行

#Edit text

#删除、复制、粘贴、撤销
y 复制(以字符为单位):表示对单个字符进行复制,如果要复制整行,用yy(以行为单位),"y"操作符命令会把文本复制到一个寄存器3中。然后可以用"p"命令把它取回。因为"y"是一个操作符命令,所以你可以用"yw"来复制一个word. 同样可以使用命令记数。如下例中用"y2w"命令复制两个word,"yy"命令复制一整行,"Y"也是复制整行的内容,复制当前光标至行尾的命令是"y$"。

复制N行: Nyy ,比如: 2yy ,表示复制2行
dd(删除,以行为单位,删除当前光标所在行)
删除N行: Ndd ,比如: 2dd ,表示删除2行
p : P粘贴
剪切: dd
x 删除光标所在位置的字符
D 从光标处删除到行尾
u 撤销操作
ctrl+r 还原撤销过的操作,将做过的撤销操作再还原回去,也就是说撤销前是什么样,再还原成什么样
r 替换,或者说用来修改一个字符, "r"命令不是一个操作符命令。它等待你键入下一个字符用以替换当前光标下的那个字符。"r"命令前辍以一个命令记数是将多个字符都替换为即将输入的那个字符。要把一个字符替换为一个换行符使用"r"。它会删除一个字符并插入一个换行符。在此处使用命令记数只会删除指定个数的字符:"4r"将把4个字符替换为一个换行符。

4. Basic operations of V mode (column mode)

1. Enter v mode, move the cursor to select the area, and perform multi-line comments during programming:

 ctrl+v 进入列编辑模式
 向下或向上移动光标,把需要注释、编辑的行的开头选中起来
 如果需要删除的话,就直接输入x键进行删除
 如果要添加内容,就按大写的I
 再插入注释符或者你需要插入的符号,比如"#"
 再按Esc,就会全部注释或添加了

2. Delete: Press ctrl v again to enter column editing mode; move the cursor down or up; select the comment part, and then press d, the comment symbol will be deleted.

5. Basic operations in command mode

:w 保存 save
:w! 强制保存
:q 没有进行任何修改,退出 quit
:q! 修改了,不保存,强制退出
:wq 保存并退出
:wq! 强制保存并退出
:x 保存退出

#Call external files or commands

Assumption: I want to write the MAC address of my network card. I want to check it. I am currently editing the document in vim and follow the instructions. This is so troublesome.
Operating in command line mode:

:!ifconfig 调用系统命令
!+命令
读取其他文件。(把其他文件中的内容追加到当前文档中)
:r /etc/hosts

#Text replacement

#格式:范围(其中%所有内容) s分隔符 旧的内容 分隔符 新的内容(分隔符可以自定义)
#默认是每一行的第一个符合要求的词(/g全部)
:1,3 s/bin/xuegod #替换第1到3行中出现的第一个bin进行替换为xuegod
:1,3 s/bin/xuegod/g #替换第1到3行中查找到所有的bin进行替换为xuegod
:3 s/xue/aaaaa #只把第3行中内容替换了
:% s/do/xuegod/g #将文本中所有的do替换成xuegod
:% s/do/xuegod/gi #将文本中所有的do替换成xuegod, 并且忽略do的大小写
:% s@a@b@g #将文本中所有的a替换成b

6. Customize vim environment

You can customize the vim environment according to your own preferences.

#Temporarily set vim environment

:set nu #设置行号
:set nonu #取消设置行号
:noh #取消高亮显示

#Permanently set vim environment

[root@node5 ~]#vim /etc/vimrc #设置后会影响到系统所有的用户
[root@node5 ~]#vim ~/.vimrc #在用户的家目录下,创建一个.vimrc。这样只影响到某一个用户,没有自己建一个
例:
[root@node5 ~]# cat /root/.vimrc
set nu

Seven.vim opens multiple files at the same time

1. Open two documents in the upper and lower forms, and use ctrl ww to switch between the two documents for editing

#Lowercase o split screen up and down

[root@node5 ~]#vim -o /etc/passwd /etc/hosts

2. Open two documents in left and right mode, and use ctrl ww to switch between the two documents for editing

#Capital O split screen left and right

[root@node5 ~]#vim -o /etc/passwd /etc/hosts

8. Compare the contents of two files

There are two methods for comparing the contents of two files: diff and vimdiff.

[root@node5 ~]#cp /etc/passwd mima.txt
[root@node5 ~]#echo aaa >> mima.txt
[root@node5 ~]#diff /etc/passwd mima.txt
40a41
> aaa
[root@node5 ~]#vimdiff /etc/passwd mima.txt

Committed to solving your problems in a one-stop manner

The above is the detailed content of The use of vim under Linux and efficient techniques. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete