Linux vi / vim command

一、什么是vim

Vim is developed from a vi text editor. Easy programming code-completion, and compilation errors jumps, etc. is particularly rich, it is widely used among programmers.
Simply put, vi is an old-fashioned word processor, but functionality has been very complete, but still have a place to progress. vim can be said to be a procedural tool developers good use.

二、vi/vim 的使用

Substantially vi / vim divided into three modes, respectively 命令模式(Command mode), 输入模式(Insert mode)and 底线命令模式(Last line mode).

2.1 Command Mode

Users just started vi / vim, then enter the command mode.
Keystrokes operation in this state is recognized as a command Vim, not input characters. For example, we then press the i, and not lose> into a character, i is treated as a command.
The following are a few commonly used commands:

  • i Switch to the input mode to input characters.
  • x Delete the character at the current cursor position.
  • : In the end line command mode is switched to input commands in the bottom row.

2.2 input mode

In the command mode by pressing i entered the input mode.
In input mode, the following keys:
character keys and Shift combinations, input character

  • ENTER, Enter key, line feed
  • BACK SPACE, Backspace, delete a character before the cursor
  • DEL, Delete key, delete the cursor one character
  • 方向键, Move the cursor in the text
  • HOME/END, Move the cursor to the beginning / end of the line
  • Page Up/Page Down, Upper / lower page
  • Insert, Move the cursor to an input / replace mode, the cursor will change to a vertical line / underline
  • ESC, Exit the input mode is switched to the command mode

2.3 The bottom line mode

Press :( colon) in the command mode to enter the bottom line command mode.
Bottom Line Command Mode can enter commands single or a plurality of characters, a lot of available commands.
In the bottom line command mode, commands are basic (colon have been omitted):

  • q exit the program
  • wSave the file
    , press the ESC key at any time to exit the bottom line command mode.

2.4 vim / vi mode of FIG.
Here Insert Picture Description

三、vi/vim 按键说明

3.1 Command Mode Key Description

3.1.1 The method of movement of the cursor

button description
h or left arrow key (←) The cursor one character to the left
j or down arrow key (↓) Cursor down one character
k or the up arrow key (↑) Moving the cursor up one character
l or the right arrow key (→) The cursor moves to the right one character
[Ctrl] + [f] Screen "down" to move one, the equivalent of [Page Down] keys (used)
[Ctrl] + [b] Screen "up" move one, the equivalent of [Page Up] button (common)
[Ctrl] + [d] Screen "down" to move half a page
[Ctrl] + [u] Screen "Up" to move half a page
+ Move the cursor to the next line of the non-space character
- Move the cursor to the line of non-space characters
n N represents the "digital", for example, 20. Press the number and then press the space key, the cursor will move to the right n characters of the line. E.g. 20 moves the cursor 20 to a character from behind.
0 or function key [Home] This is the number "0": the characters move to the front of the line at the (common)
$ Or function key [End] Moved to the rearmost character of the line (common)
H Move the cursor to the first character of this top of the line screen
M The first character cursor is moved to the line of the center of the screen
L Move the cursor to the first character in the bottom of the screen that row
G The move to the last line of the file (common)
ng n is a number. Move to the n-th row of this file. 20G, for example, the line will move to the file 20 (can be used with: set nu)
gg Move to the first line of the file, equivalent to 1G ah! (Common)
n n is a number. Move the cursor down n row (common)

If you put the right hand on the keyboard, you will find hjkl be arranged together, so you can use these four buttons to move the cursor. If you want to move a plurality of times, then, for example, downward movement of the line 30, may be used "30j" or "30 ↓" key combination, i.e. the number of times plus (digital) want to, the pressing action can be!

3.1.2 search and replace

button description
/word Looking for a string name for the word under the cursor. For example, to search for the string within the file vbird, enter / vbird can! (Common)
?word Looking for a string name for the cursor to the top of the word string.
n 这个 n 是英文按键。代表重复前一个搜寻的动作。举例来说, 如果刚刚我们执行 /vbird 去向下搜寻 vbird 这个字符串,则按下 n 后,会向下继续搜寻下一个名称为 vbird 的字符串。如果是执行 ?vbird 的话,那么按下 n 则会向上继续搜寻名称为 vbird 的字符串!
N 这个 N 是英文按键。与 n 刚好相反,为『反向』进行前一个搜寻动作。 例如 /vbird 后,按下 N 则表示『向上』搜寻 vbird 。
: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) 是否需要取代!(常用)

3.1.3 移动光标的方法

按键 描述
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 重做上一个动作。(常用)
. 不要怀疑!这就是小数点!意思是重复前一个动作的意思。 如果你想要重复删除、重复贴上等等动作,按下小数点『.』就好了! (常用)

3.2 命令模式切换到编辑模式的可用的按钮说明

按键 描述
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 为止;(常用)
[Esc] 退出编辑模式,回到一般模式中(常用)

3.3 命令模式切换到指令行模式可用的按钮说明

选项 描述
:w 将编辑的数据写入硬盘档案中(常用)
:w! If the file attribute is "read-only" forced to write to the file. However, in the end you can not write, or tell you about the file permissions files ah!
:q Leave vi (common)
:q! If the file has been modified, I do not want to store, use! Forced to leave without saving the file.
:wq After leaving the store, if it is: left (common) after wq was forced store!
ZZ This is a capital Z Oh! If the file is not modifier, not to leave the store, if the file has been modifier before, then after leaving the store!
:w [filename] Storing the edited data file to another (similar to the Save As)
:r [filename] In the data editing, the data read another file. That is the "filename" is added to the contents of this file line behind the cursor
:n1,n2 w [filename] N1 n2 to the content of this file is stored into the filename.
:! command Temporarily leave vi command execution result to the display of the command line mode! For example:! Ls / home "to look at them in vi / home under ls output to archival information!

Note ah, the exclamation mark (!) In which vi, often have a "mandatory" means!

Change 3.4 vim environment

button description
: Not set Display line numbers, after setting, the line number displayed in the row of each line prefix
:set nonu nu In contrast to the set, to cancel the line number!

四、vim 中批量添加注释

4.1 Batch Notes

Use the following command to add the first comment in the specified line.
Use the Name command format: :起始行号,结束行号s/^/注释符/g (note the colon).

4.2 uncommented

Use the Name command format: :起始行号,结束行号s/^注释符//g (note the colon).

4.3 shows the column

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 blog.csdn.net/qq_27870421/article/details/90897125