vi and vim are an editor that comes with centos, but it is not easy to use it proficiently. vim is the same as vi, except that when vim supports color
vi editing, there are command mode and editing mode. When entering a file, it automatically switches to command mode
/ Search content Search for keywords in the file (press 'n' for next)
:set ic ignore case : set noic cancel case
: %s /(old character)/(new character)/g new character replaces old character % Under the full text of s
: n1,n2s /(old character)/(new character)/g new character replaces old character from n1 to n2 to replace
ps. The last 'g' is not asking the user whether to replace and can be replaced with 'c' for asking Whether the user replaces
: w save
: wq save and exit
ZZ shortcut, save and exit
: q! Force do not save and exit
: wq! Force save and exit (file owner, root user)
: w + new name Save as specified file
: set nu set line
:set nonu cancel line
Six commands to enter the edit mode :
i insert before the word where the cursor is located I insert before the line where the cursor is located
a insert the word where the cursor is located after insert A insert after the line where the cursor is located
o insert a new line under the line where the cursor is located O insert a new line on the line where the cursor is located
Common editing commands (in command mode):
gg jump to the first line
G jump to the last line
nG to which line n is the number of lines
: n to which line n is the number of lines
$ end of line
0 line start
x delete the word at the cursor
nx delete n words after the cursor
dd delete the cursor line (also cut)
ndd delete n lines (also cut)
dG delete the cursor to the end of the file
n1, n2d delete n1 to n2 lines
yy copy line
nyy copy line next n Line
p is pasted under the cursor line
P is pasted on the cursor line
r replaces the cursor text
R Always replace from the cursor, Esc to end u
to cancel the previous operation a
Import command (where the cursor is):
:r file name (command/path)
:!which command View the command location
: !date to see the time ps. :r !date can import the time
Continuous line comment :
:n1,n2s /^/#/gContinuous comment#
:n1,n2s /^#//gCancel continuous#
:n1,n2s /^/\/\//gSet //sign
Map definition:
: map (ctrl+v) + shortcut key Combination commands
such as: :map [ctrl+v]P
After I# is defined, enter P in command mode, there will be multiple # signs before the line, and the ctrl+v combination key is in A symbol similar to ^ will be generated in the vi editor
vi /root/.vimrc Permanently change the command file
The command configured in this file will automatically take effect in vi by default, and the above edited command will be invalid after exiting the editor,
such as adding: :set nu
Then enter later The editor will automatically set the line number