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:
From a line to a line comment, what is actually done is the previous replacement command, replacing the beginning of the line with a # sign.
:n1,n2s/^/#/g
Last year's note: replace the # at the beginning of the line with nothing, pay attention to the expression symbol at the beginning of the line ^
:n1,n2s/^#//g
Auto replace:
:ab myblog wlcacc.iteye.com
When you enter myblog and then a space or a carriage return, the blog address will be automatically replaced
The above shortcut keys are temporary, and they will be lost when the server restarts. How to persist?
The shortcut keys need to be saved to the configuration file of his home directory.
root:/root/.vimrc
Ordinary user test : /home/test/.vimrc
Only commands in edit mode can be placed
set no