3. Text editor vim
The earlier version was called vi
Create, edit, display files
no menus, only commands
3.1. Common operations of vim
vim install
yum install vim
vim working mode:
Can not upload pictures, you can refer to here, quite detailed:
http://www.runoob.com/linux/linux-vim.html
Create a new file:
vim test.log
In edit mode:
set line number set nu
cancel line number set nonu
Insert command:
Order |
effect |
a |
Insert after the character under the cursor |
A |
Insert at the end of the line where the cursor is |
i |
Insert before the character under the cursor |
I |
Insert at the beginning of the line where the cursor is located |
O |
Insert a new line under the cursor |
O |
Insert a new line at the cursor |
Positioning command:
Order |
effect |
: set no |
set line number |
:set nonu |
cancel line number |
gg |
to the first place |
G |
to the last line |
nG |
to the nth line |
:n |
to the nth line |
$ |
move to end of line |
0 |
move to beginning of line |
delete command:
Order |
effect |
x |
delete the character at the cursor |
nx |
delete n characters after the cursor |
dd |
delete the line where the cursor is located |
ndd |
Delete the line where the cursor is located and delete n lines after it |
dG |
Delete the line where the cursor is located to the end of the file |
D |
Delete the content from the cursor to the end of the line |
:n1,d2d |
Delete the specified range of rows, for example: delete rows 10 to 23 : 10,12d |
Copy and Cut Commands:
Order |
effect |
yy |
copy current line |
nyy |
Copy n lines below the current line |
dd |
cut current line |
ndd |
Cut the current following n lines |
lowercase p |
Paste under the current cursor line |
capital P |
Paste on the current cursor line |
Replace and cancel commands:
Order |
effect |
r |
Replace the character where the cursor is, press ESC to end |
R |
Replace characters starting at the cursor position, press ESC to end |
u |
Cancel the previous operation |
Search and search replace commands:
Order |
effect |
/string |
Search for a specified string |
set ic |
Ignore case when searching |
set noic |
Case sensitivity when searching |
n |
Specifies the next occurrence of a string when searching is a search item, similar to the next of less |
:%s/old/new/g |
Replace the specified string in full text |
:n1,n2s/old/new/g |
Replace a specified string within a range |
Save and exit commands:
Order |
effect |
:w |
Save changes |
:w new_filename |
Do not save as specified file |
:wq |
save changes and exit |
ZZ |
Shortcut, save changes and exit |
:q! |
Exit without saving changes |
:wq! |
Save changes and exit ( only available to file owner and root ) |
3.2.Vim usage skills
Import command execution result: r ! command
For example import file content:
:r /tmp/test.txt
View the command execution result
:!which ls
Import command execution result:
:r !which ls
Define shortcut key syntax:
:map shortcut key trigger command
For example: the shortcut key plan is defined as Ctrl+p
It is as follows:
:map Ctrl+v+p I#<ESC>
I# means to start the first line, insert a # sign before the cursor
<ESC> means return to command mode
~
~
:map ^P I#<ESC>
Consecutive line comments:
从某行到某行注释,实际上做的是之前的替换命令,将行首替换为#号。
:n1,n2s/^/#/g
去年注释:将行首的#号替换为空,注意行首表达式符号^
:n1,n2s/^#//g
自动替换:
:ab myblog wlcacc.iteye.com
当你输入myblog然后空格或者回车时,会自动替换博客地址
以上快捷键都是临时的,服务器重启就丢失了,如何持久化呢?
需要把快捷键保存到他的家目录的配置文件中。
root:/root/.vimrc
普通用户test:/home/test/.vimrc
只能放编辑模式的命令
set nu