Linux Vim/Vi

Linux vi/vim

All Unix Like systems will have a built-in vi text editor, other text editors may not exist.

But currently we use more vim editor.

vim has the ability of program editing, and can actively identify the correctness of grammar by font color, which is convenient for program design.

what is vim?

Vim is a text editor developed from vi. The functions of convenient programming such as code completion, compilation and error jumping are particularly rich, and are widely used by programmers. vim is a program development tool not a word processor

To put it simply, vi is an old-fashioned word processor, but the functions are already very complete, but there is still room for improvement. vim can be said to be a very useful tool for program developers.

vim keyboard map:


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 command mode.

Keystrokes in this state are recognized by Vim as commands, not characters. For example, if we press i at this time, a character will not be entered, and i is regarded as a command.

The following are some commonly used commands:

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

To edit text: Start Vim, enter command mode, press i to switch to input mode.

Command mode only has some basic commands, so you still have to rely on the bottom line command mode to enter more commands.

input mode

Press i in command mode to enter input mode.

In input mode, the following keys are available:

  • Character keys and Shift combinations to enter characters
  • ENTER , carriage return, line feed
  • Backspace , the backspace key, deletes the character before the cursor
  • DEL , delete key, delete the character after the cursor
  • Arrow keys , 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 from input -->> replace mode, the cursor will become an underline
  • ESC , exit input mode, switch to command mode

Bottom Line Command Mode

In the command mode, press : (English colon) to enter the bottom line command mode.

The bottom line command mode can enter single or multi-character commands, and there are many commands available.

In bottom-line command mode, the basic commands are (colons have been omitted):

  • q to quit the program
  • w save file

The bottom line command mode can be exited at any time by pressing the ESC key.

Simply put, we can think of these three modes as the icons at the bottom:


vi/vim usage example

Use vi/vim to enter normal mode

If you want to use vi to create a file called test.txt, you can do:

$ vi runoob.txt

Enter the vi file name directly to enter the general mode of vi. Please note, remember to add the file name after vi, whether the file exists or not!

Press i to enter input mode (also known as edit mode) to start editing text

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

In the editing mode, you can find the words –INSERT- in the status bar in the lower left corner, which is a prompt that you can enter any character.

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

Press the 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 notice that the bottom left corner of the screen – INSERT – is gone!

In normal mode press :wq to save and leave vi

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

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


vi/vim key description

In addition to i, Esc, :wq in the simple example above, there are actually a lot of keys in vim that can be used.

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

How to move the cursor
h or left arrow key (←) Move the cursor one character to the left
j or down arrow key (↓) Move the cursor down one character
k or up arrow key (↑) Move the 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 the hjkl are arranged so that you can use the four buttons to move the cursor. If you want to move multiple times, for example, move down 30 lines, you can use the "30j" or "30↓" key combination, that is, add the number of times you want to perform (number), and press the action!
[Ctrl] + [f] Move the screen "down" one page, which is equivalent to the [Page Down] button (commonly used)
[Ctrl] + [b] Move the screen "up" one page, which is equivalent to the [Page Up] button (commonly used)
[Ctrl] + [d] Move the screen "down" by half a page
[Ctrl] + [u] Move the screen "up" by half a page
+ Move the cursor to the next line with a non-space character
- Move the cursor to the previous line of a non-space character
n<space> That n means "number", eg 20. Press the number and then the space bar, the cursor will move to the right n characters of the line. For example, 20<space> will move the cursor back 20 characters.
0 or function key [Home] This is the number "0": move to the first character of the line (commonly used)
$ or function key [End] move to the last character of the line (common)
H Move the cursor to the first character of the top line of this screen h: move left
M Move the cursor to the first character of the line in the center of the screen
L Move the cursor to the first character of the bottom line of this screen l: move right
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, which is equivalent to 1G! (commonly used)
n<Enter> n is a number. Move the cursor down n lines (commonly used)
search and replace
/word Look under the cursor for a string named word. For example, to search for the string vbird in the file, just enter /vbird! (commonly used)
?word Look above the cursor for a string named word.
n The n is the English key. Represents the action of repeating the previous search. For example, if we just executed /vbird to search down the string vbird, after pressing n, it will continue to search for the next string named vbird. If ?vbird is executed, then pressing n will continue to search upwards for the string named vbird!
N The N is the English button. Contrary to n, the previous search action is performed for "reverse". For example, after /vbird, pressing N means "up" to search for vbird.
使用 /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 从第一行到最后一行寻找 word1 字符串,并将该字符串取代为word2 !(常用)
:1,$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↓』即可。 


 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895503&siteId=291194637