[Liunx] Linux vi / vim tutorial

All Unix Like systems will have a built-in vi document editor, and other document editors will not necessarily exist.

But currently we use vim editor more.

Vim has the ability to edit programs, and can actively identify the correctness of grammar by font color, which is convenient for program design.

Related article: The Most Complete Vim Shortcut Key Bitmap in History — Getting Started to Advanced


What is vim?

Vim is a text editor developed from vi. The functions of convenient programming such as code completion, compilation and error jump are particularly rich, and are widely used by programmers.

In simple terms, vi is an old word processor, but the function is very complete, but there is still room for improvement. Vim can be said to be a very useful tool for program developers.

Even vim's official website ( http://www.vim.org ) also said that vim is a program development tool rather than word processing software.

vim keyboard diagram:


Use of vi / vim

Basically vi / vim is divided into three modes, namely command mode (Command mode) , input mode (Insert mode) and bottom line command mode (Last line mode) . The functions of these three modes are:

Command mode:

The user has just started vi / vim and entered the command mode.

In this state, the keyboard tapping action will be recognized as a command by Vim instead of entering characters. For example, if we press i at this time, we will not enter a character, i is regarded as a command.

The following are the commonly used commands:

  • i Switch to input mode to enter characters.
  • x Delete the character at the current cursor position.
  • : Switch to the bottom line command mode to enter commands at the bottom line.

If you want to edit the text: start Vim, enter the command mode, press i to switch to input mode.

The command mode only has some basic commands, so we still have to rely on the bottom line command mode to input more commands.

Input mode

Press i in the command mode to enter the input mode.

In the input mode, you can use the following keys:

  • Character keys and Shift combination to enter characters
  • ENTER , Enter key, line feed
  • BACK SPACE , backspace key, delete the character before the cursor
  • DEL , delete key, delete the character after the cursor
  • Arrow keys to move the cursor in the text
  • HOME / END , move the cursor to the beginning / end of the line
  • Page Up / Page Down , page up / down
  • Insert , switch the cursor to input / replace mode, the cursor will become vertical / underline
  • ESC , exit input mode, switch to command mode

Bottom line command mode

Press in command mode: (English colon) to enter the underline command mode.

The bottom line command mode can input single or multiple character commands, and there are many commands available.

In the underline command mode, the basic commands are (the colon has been omitted):

  • q Exit the program
  • w Save the file

Press the ESC key to exit the bottom line command mode at any time.

Simply put, we can think of these three patterns as icons below:


vi / vim usage examples

Use vi / vim to enter the general mode

If you want to use vi to create a file named runoob.txt, you can do this:

$ vim runoob.txt

Direct input vi filename will be able to enter the general mode of vi. Please note that the file name must be added after vi, no matter whether the file exists or not!

Press i to enter input mode (also called edit mode) and start editing text

In the general mode, just press i, o, a and other characters to enter the input mode!

In the edit mode, you can find that the word –INSERT- appears in the status bar in the lower left corner, that is, you can enter any character prompt.

At this time, except the Esc key on the keyboard , other keys can be regarded as general input buttons, so you can make any edits.

Press ESC button to return to normal mode

Well, assuming that I have finished editing him according to the above style, how should I exit? Yes! That's right! Just press the Esc button for him ! Immediately you will find-INSERT-in the lower left corner of the screen is gone!

Press in general mode : wq save and leave vi

OK, we are going to archive, the command to save and leave is very simple, enter : wq to save and leave!

OK! So we successfully created a runoob.txt file.


vi / vim key description

In addition to i, Esc,: wq in the simple example above, in fact, vim has a lot of buttons to use.

Part 1: Cursor movement, copy and paste, search and replace, etc. available in general mode

How to move the cursor
h or left arrow key (←) Cursor moves one character to the left
j or down arrow key (↓) Cursor down one character
k or up arrow key (↑) Cursor up one character
l or right arrow key (→) Move the cursor one character to the right
If you put your right hand on the keyboard, you will find that hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, to move down 30 lines, you can use the "30j" or "30 ↓" key combination, that is, add the number of times (number) you want to perform, and press the action!
[Ctrl] + [f] The screen moves down one page, which is equivalent to the [Page Down] button (commonly used)
[Ctrl] + [b] The screen moves up one page, which is equivalent to the [Page Up] button (commonly used)
[Ctrl] + [d] The screen 『down』 move half a page
[Ctrl] + [u] The screen "up" moves half a page
+ The cursor moves to the next line other than space character
- The cursor moves to the line above the non-space character
n<space> The n means "number", for example 20. Press the number and then press the space bar, the cursor will move to the right n characters of this line. For example, 20 <space>, the cursor will move back 20 characters.
0 or function key [Home] This is the number "0": Move to the first character of this line (commonly used)
$ Or function key [End] Move to the last character of this line (commonly used)
H The cursor moves to the first character of the top line of this screen
M The cursor moves to the first character in the center line of this screen
L The cursor moves to the first character in the bottom line of this screen
G Move to the last line of this file (commonly used)
nG n is a number. Move to line n of this file. For example, 20G will move to the 20th line of this file (can be used with: set nu)
gg Move to the first line of this file, equivalent to 1G! (Common)
n<Enter> n is a number. The cursor moves down n lines (commonly used)
Search and replace
/word Look under the cursor for a string with the name word. For example, to search for the vbird string in the file, type / vbird! (Common)
?word Look for a character string with the word name above the cursor.
n This n is the English button. Represents the action of repeating the previous search. For example, if we just executed / vbird to search downward for the vbird string, after pressing n, it will continue to search for the next string named vbird. If you execute? Vbird, then press n to continue to search for the string named vbird!
N This N is the English button. Contrary to n, the previous search action is performed for "reverse". For example, after / vbird, press N to search for vbird "up".
使用 /word 配合 n 及 N 是非常有帮助的!可以让你重复的找到一些你搜寻的关键词!
:n1,n2s/word1/word2/g n1 与 n2 为数字。在第 n1 与 n2 行之间寻找 word1 这个字符串,并将该字符串取代为 word2 !举例来说,在 100 到 200 行之间搜寻 vbird 并取代为 VBIRD 则:
『:100,200s/vbird/VBIRD/g』。(常用)
:1,$s/word1/word2/g 或 :%s/word1/word2/g 从第一行到最后一行寻找 word1 字符串,并将该字符串取代为 word2 !(常用)
:1,$s/word1/word2/gc 或 :%s/word1/word2/gc 从第一行到最后一行寻找 word1 字符串,并将该字符串取代为 word2 !且在取代前显示提示字符给用户确认 (confirm) 是否需要取代!(常用)
删除、复制与贴上
x, X 在一行字当中,x 为向后删除一个字符 (相当于 [del] 按键), X 为向前删除一个字符(相当于 [backspace] 亦即是退格键) (常用)
nx n 为数字,连续向后删除 n 个字符。举例来说,我要连续删除 10 个字符, 『10x』。
dd 删除游标所在的那一整行(常用)
ndd n 为数字。删除光标所在的向下 n 行,例如 20dd 则是删除 20 行 (常用)
d1G 删除光标所在到第一行的所有数据
dG 删除光标所在到最后一行的所有数据
d$ 删除游标所在处,到该行的最后一个字符
d0 那个是数字的 0 ,删除游标所在处,到该行的最前面一个字符
yy 复制游标所在的那一行(常用)
nyy n 为数字。复制光标所在的向下 n 行,例如 20yy 则是复制 20 行(常用)
y1G 复制游标所在行到第一行的所有数据
yG 复制游标所在行到最后一行的所有数据
y0 复制光标所在的那个字符到该行行首的所有数据
y$ 复制光标所在的那个字符到该行行尾的所有数据
p, P p 为将已复制的数据在光标下一行贴上,P 则为贴在游标上一行! 举例来说,我目前光标在第 20 行,且已经复制了 10 行数据。则按下 p 后, 那 10 行数据会贴在原本的 20 行之后,亦即由 21 行开始贴。但如果是按下 P 呢? 那么原本的第 20 行会被推到变成 30 行。 (常用)
J 将光标所在行与下一行的数据结合成同一行
c 重复删除多个数据,例如向下删除 10 行,[ 10cj ]
u 复原前一个动作。(常用)
[Ctrl]+r 重做上一个动作。(常用)
这个 u 与 [Ctrl]+r 是很常用的指令!一个是复原,另一个则是重做一次~ 利用这两个功能按键,你的编辑,嘿嘿!很快乐的啦!
. 不要怀疑!这就是小数点!意思是重复前一个动作的意思。 如果你想要重复删除、重复贴上等等动作,按下小数点『.』就好了! (常用)

第二部分:一般模式切换到编辑模式的可用的按钮说明

进入输入或取代的编辑模式
i, I 进入输入模式(Insert mode):
i 为『从目前光标所在处输入』, I 为『在目前所在行的第一个非空格符处开始输入』。 (常用)
a, A 进入输入模式(Insert mode):
a 为『从目前光标所在的下一个字符处开始输入』, A 为『从光标所在行的最后一个字符处开始输入』。(常用)
o, O 进入输入模式(Insert mode):
这是英文字母 o 的大小写。o 为『在目前光标所在的下一行处输入新的一行』; O 为在目前光标所在处的上一行输入新的一行!(常用)
r, R 进入取代模式(Replace mode):
r 只会取代光标所在的那一个字符一次;R会一直取代光标所在的文字,直到按下 ESC 为止;(常用)
上面这些按键中,在 vi 画面的左下角处会出现『--INSERT--』或『--REPLACE--』的字样。 由名称就知道该动作了吧!!特别注意的是,我们上面也提过了,你想要在档案里面输入字符时, 一定要在左下角处看到 INSERT 或 REPLACE 才能输入喔!
[Esc] 退出编辑模式,回到一般模式中(常用)

第三部分:一般模式切换到指令行模式的可用的按钮说明

指令行的储存、离开等指令
:w 将编辑的数据写入硬盘档案中(常用)
:w! 若文件属性为『只读』时,强制写入该档案。不过,到底能不能写入, 还是跟你对该档案的档案权限有关啊!
:q 离开 vi (常用)
:q! 若曾修改过档案,又不想储存,使用 ! 为强制离开不储存档案。
注意一下啊,那个惊叹号 (!) 在 vi 当中,常常具有『强制』的意思~
:wq 储存后离开,若为 :wq! 则为强制储存后离开 (常用)
ZZ 这是大写的 Z 喔!若档案没有更动,则不储存离开,若档案已经被更动过,则储存后离开!
:w [filename] 将编辑的数据储存成另一个档案(类似另存新档)
:r [filename] 在编辑的数据中,读入另一个档案的数据。亦即将 『filename』 这个档案内容加到游标所在行后面
:n1,n2 w [filename] 将 n1 到 n2 的内容储存成 filename 这个档案。
:! command 暂时离开 vi 到指令行模式下执行 command 的显示结果!例如
『:! ls /home』即可在 vi 当中察看 /home 底下以 ls 输出的档案信息!
vim 环境的变更
:set nu 显示行号,设定之后,会在每一行的前缀显示该行的行号
:set nonu 与 set nu 相反,为取消行号!

特别注意,在 vi/vim 中,数字是很有意义的!数字通常代表重复做几次的意思! 也有可能是代表去到第几个什么什么的意思。

举例来说,要删除 50 行,则是用 『50dd』 对吧! 数字加在动作之前,如我要向下移动 20 行呢?那就是『20j』或者是『20↓』即可。

vim 中批量添加注释

方法一 :块选择模式

批量注释:

Ctrl + v 进入块选择模式,然后移动光标选中你要注释的行,再按大写的 I 进入行首插入模式输入注释符号如 //#,输入完毕之后,按两下 ESCVim 会自动将你选中的所有行首都加上注释,保存退出完成注释。

取消注释:

Ctrl + v 进入块选择模式,选中你要删除的行首的注释符号,注意 // 要选中两个,选好之后按 d 即可删除注释,ESC 保存退出。

方法二: 替换命令

批量注释。

使用下面命令在指定的行首添加注释。

使用名命令格式: :起始行号,结束行号s/^/注释符/g(注意冒号)。

取消注释:

使用名命令格式: :起始行号,结束行号s/^注释符//g(注意冒号)。

例子:

1、在 10 - 20 行添加 // 注释

:10,20s#^#//#g

2、在 10 - 20 行删除 // 注释

:10,20s#^//##g

3、在 10 - 20 行添加 # 注释

:10,20s/^/#/g

4、在 10 - 20 行删除 # 注释

:10,20s/#//g

Guess you like

Origin www.cnblogs.com/HGNET/p/12724133.html