Outline
vi 与 I came
vi editor is a standard editor for Unix and Linux systems, the equivalent of Notepad on Windows systems. We use the Linux system is an indispensable tool.
vim ability to program editing, font color can discern the correctness of grammar, easy programming; compared to the vi editor, vim editor has more powerful features, if the vi editor likened to the Windows system Notepad , then the vim editor is Windows in the Word. Because the program is simple, editor at a fairly fast.
vim syntax will automatically determines the execution of the program according to the file type extension, or at the beginning of the information in the file, determining contents of the file, and then to display the color information of the program code in general.
vim it added a lot of extra features, such as support for regular expression search, multi-file editor, block copy and so on.
Vim editor
Vim editor function in accordance with its shape divided into three modes, i.e. insert mode, command mode, command mode expansion.
mode | Features |
---|---|
Insert mode | Text can be entered, in the normal mode, according to i, a, o and so can enter the insert mode, in this mode, insertion and deletion of text can be modified and the like; press the ESC key to return to command mode |
Command Mode | Vim command mode is the initial mode of the editor, in this operation mode, the command can be Linux |
Expand command mode | Press "Shift +:" to enter the extended command mode, in this mode can be command-line operations, such as save, exit, etc. |
Vim commands and their applications
Vim commands (start Vim editor)
Format: vim + Option + file name (middle separated by a space)
format | Features |
---|---|
-c cmd | Before opening the file, runs the specified command |
-r | Abnormal exit of the last file recovery |
-R | Open the file in read-only mode, but can be forced to save |
-M | Open the file in read-only mode, can not be forced to save |
whether -y | The edit window size to num lines |
+ | From the end of the file start |
+ a | Starting at line num |
+/string | Open the file, and the cursor on the first string found |
Vim editor mode switch
Command mode -> Insert Mode
button | Features |
---|---|
i | insert, the input at cursor |
I | Enter the first line of the current cursor line |
a | append, in the rear of the cursor input |
A | In the end of the current cursor line input |
O | Open a new line below the current cursor line |
O | Open a new line above the current cursor line |
Insert Mode -> Command Mode
button | Features |
---|---|
ESC | Switch to command mode |
Command Mode -> Extended Command Mode
button | Features |
---|---|
Shift+: | Switching to command mode expansion |
Extended Command Mode -> Command Mode
button | Features |
---|---|
ESC then press Enter | Switch to command mode |
Vim command mode
Common commands
operating | Features |
---|---|
ZZ | Save and exit |
ZQ | Exit without saving |
h | The cursor is moved to the left |
l | Move the cursor to the right |
j | Move the cursor down |
k | Cursor up |
#COMMAND | The cursor jumps by the character designation number # |
w | The cursor jumps to the next word of the first word |
e | The cursor jumps to the ending of the current or next word |
b | The cursor jumps to the current or previous word of the first word |
H | The cursor jumps to the top |
M | The cursor jumps to the page in the middle row |
L | The cursor jumps to the bottom of the page |
Delete command
operating | Features |
---|---|
d | Delete command, the cursor jumps characters can be combined to achieve Clear |
d$ | Delete to end of line |
d^ | Delete the first non-empty row |
d0 | Delete the beginning of the line |
dd | Delete line cursor |
#dd | Delete multiple lines |
D | Has been deleted from the current cursor position to the end of the line, the line blank, equivalent to d $ |
Copy command
operating | Features |
---|---|
Y | Copy, behavior similar to the d command |
and $ | Copy to end of line |
y0 | Copied to the beginning of the line |
and ^ | Copy the first non-empty row |
is | Copied to the current or next word endings |
is | Copy the first word to the next word |
yy | Copy Rows |
#yy | Copy multiple lines |
Y | Copy the entire row |
Other commands
operating | Features |
---|---|
p | If the buffer becomes full row, the bottom row of the pasted current cursor; otherwise, attached to the back at the current cursor |
in | Undo recent changes |
#u | Before you undo multiple changes |
Vim extended command mode
Common operations
operating | Features |
---|---|
:q | drop out |
:q! | Force Quit, discarding modifications made |
:wq | Save and exit |
:x | Save and exit |
:r filename | Read the contents of the file to the current file |
:w filename | The current contents of the file written to another file |
:!command | Excuting an order |
:r!command | Reads the output of the command |
Address defined
Delimiter | Features |
---|---|
# | DETAILED # of rows, for example 2, line 2 represents |
#,# | # Represents the starting row from the left, to the right end of the line represents # |
#,+# | It represents the starting row from the left #, # plus the number represented by the right side of the line |
:2,+3 | Line represents 2-5 |
. | Current line |
$ | the last line |
.,$-1 | The current line to the penultimate line |
% | Paper, equivalent to 1, $ |
Find and Replace
Find
operating | Features |
---|---|
/PATTERN | Find at the current cursor to the end of the file |
?PATTERN | Find from the current cursor location to the header file |
n | And command the same direction |
N | 与命令反方向 |
替换
操作 | 功能 |
---|---|
i: | 忽略大小写 |
g | 全局替换;默认情况下,每一行只替换第一次出现 |
gc | 全局替换,每次替换前询问 |
示例1 | %s@目标内容@替换内容@g |
示例2 | %s#目标内容#替换内容#i |
设置文件格式
操作 | 功能 |
---|---|
启用windows格式 | set fileformat=dos |
启用unix格式 | set fileformat=unix |
设置文本宽度
:set textwidth=65
:set wrapmargin=15
案例
案例一:在vim中设置tab缩进为4个字符
1.通过cd命令到达home目录下,利用vim命令打开.vimrc文件
liuyan@liuyan-virtual-machine:~$ vim .vimrc
- 在.vimrc文件里输入set tabstop=4 保存退出后运行.vimrc文件即可
案例二:删除文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号
- 利用vim命令打开目标文件,目标文件如下:
1 #!/bin/bash
2 #+--+--+--+--+--+--+--+--+--+
3 #author:liuyan
4 #date:2020-03-07
5 #tel:188332489740
6 #Email:[email protected]
7 #+--+--+--+--+--+--+--+--+--+
8 # hsdgjdfhglkjs
9 # hgfjkhdsksd
10 # aaaaaaaaaaaaaaaaaaaaaaaas
11 # aaaaaaaaaaaaad
12 # aaaaaaaaac
13 # aaaaaaaaaaaaacc
14 adsssssssssssfdgsf
15 adgshjkfd
- 转到扩展命令模式,输入修改命令:%s@# +@@g修改后如下:
1 #!/bin/bash
2 #+--+--+--+--+--+--+--+--+--+
3 #author:liuyan
4 #date:2020-03-07
5 #tel:188332489740
6 #Email:[email protected]
7 #+--+--+--+--+--+--+--+--+--+
8 hsdgjdfhglkjs
9 hgfjkhdsksd
10 aaaaaaaaaaaaaaaaaaaaaaaas
11 aaaaaaaaaaaaad
12 aaaaaaaaac
13 aaaaaaaaaaaaacc
14 adsssssssssssfdgsf
15 adgshjkfd
16
~
~