Vim Editor
Vi:visual Interface, Text editor
Text: Ascii,unicode
Type of text editing:
Line Editor: SED
Full Screen Editor: Nano,vi
Vim-vi improved
Using Vim
Vim: A patterned editor
Basic mode:
Edit mode, Command mode:
Input mode:
Last-line mode:
Built-in command-line interface
Open File:
#vim [option] ... FILE ...
+#: After opening the file, just leave the cursor at the beginning of line #
+/pattern: Immediately after opening the file, leave the cursor at the beginning of the first line that is matched to the PATTERN
Mode conversion:
Edit mode--Input mode
I:insert, enter at the cursor position
I: Enter at the beginning of the line where the cursor is currently located
A:append, enter after cursor location
A: At the end of the line where the current cursor is entered
O: Opens a new line below the line where the current cursor is located
O: Open a new row above the line where the current cursor is located
C:
C:
Input mode--edit mode
Esc
Edit Mode--last-line mode
:
Last-line mode--edit mode
CTRL + C or two times ESC
To close a file:
: Q exit
: q! Force exit, discard the modifications made
: Wq Save Exit
: X Save exit
: W/path/to/somewhere
ZZ: Save exit (edit mode)
Cursor Jump:
Jump between characters:
H,j,k,l
H: Left
J: Next
K: Up
L: Right
#COMMAND: Jumps the number of characters specified by #
Jump between words:
W: The first word of the next word
E: The ending of the current or next word
B: The first word of the current or previous word
#COMMAND: Jumps the number of words specified by #
Beginning line End Jump:
^: jumps to the first non-whitespace character at the beginning of a line
0: Jump to the beginning of the line
$: Jump to end of line
Move between rows:
#G: Jumps to the line specified by #
G: Last line
1g,gg: Jump to First line
Move between sentences:
): Next sentence
(: Previous sentence
Move between segments:
}: Next Paragraph
{: Last paragraph
Vim's edit command:
Character editing:
X: Delete the character at the cursor location
#x: Remove the back # characters at the cursor location
XP: Swap the position of the character and the following character at the cursor location
Replace command:
R: the character at which the cursor is replaced
Delete command:
D: Delete command, can be combined with the cursor jump character, to achieve range deletion
d$: Cursor to end of line content
d^: Cursor to the beginning of the content
D0: Cursor to the beginning of the content
Dw:
De
Db:
#COMMAND
#dw:
#de:
#db:
DD: Delete the entire row where the cursor is located
#COMMAND: Delete # lines
Paste command (p,put,paste):
P (lowercase): If the buffer is stored as an entire row, it is pasted below the line where the current cursor is located. Otherwise, paste to the back of the current cursor
P (UPPERCASE): If the buffer is stored as an entire row, it is pasted above the line where the current cursor is located. Otherwise, paste at the front of the current cursor
Copy command (Y,yank):
Y: Copy, work behaves similar to D command:
y$
Y0
y^
Ye
yw
Yb
#COMMAND
YY: Copying rows
#yy: Copy # lines
Change command (C,change)
C: Modify
Edit mode--Input mode
C $
C0
c^
Ce
cw
Cb
#COMMAND
CC: Delete and enter new content
#cc: Delete # lines
Visualization mode:
V: Selected by character
V: Selected by row
Note: often combine edit commands
D,c,y
To undo previous edits:
U (undo): Undo Previous action
#u: Undo Previous # Operations
To revoke a previous revocation:
Ctrl+r
Repeat the previous edit operation:
.
Turn screen operation:
CTRL+F: Flip a screen to the end of the file
CTRL+B: Flip a screen to the file header
Ctrl+d: Turn half screen at the end of the file
Ctrl+u: Turn half screen to file header
Vim comes with a tutorial
Vimtutor
Last-line mode in VIM:
Built-in command-line interface
(1) Address delimitation
: Start_pos,end_pos
#: The specific # line, for example, 2 means line 2nd
#,#: From the left # indicates the line start, to the right # indicates the end of the line
#,+#: From the left # indicates the start of the line, plus the number of rows on the right #
.: When moving forward
$: Last line
For example:., $-1: From the current line to the second penultimate line
%: Full text, equivalent to 1,$
/pattern1/,/pattern2/: Starting from the first line that is matched to the pattern1 pattern, until the end of the line that was first matched to the PATTERN2
Common usage:
#,/pattern/
/pattern/,$
How to use:
followed by an edit command
D
Y
W/path/to/somewhere: Save a range of rows to a specified file
R/path/from/somefile: Inserts all content from the specified file at the specified location
(2) Find
/pattern: Looks at the end of the file from the current cursor location
? PATTERN: Finds the file header from the current cursor location
N: Find pattern-matching content in the same direction as the command
N: Find pattern-compliant content in the opposite direction of the command
(3) Find and replace
S: Complete the Find and replace operation in the last line mode
s/what to look for/replace with content/modifiers
What to look for: Available modes
Replace with: cannot use mode, but can use \1,\2, ... You can also use "&" to refer to the entire content found in the previous lookup.
Modifier:
I: Ignore case
G: Global substitution: By default, each row replaces only the first occurrence of the
Find separators in substitutions/can be replaced with other characters, such as
:% s/\/var\/log\/message/\/etc\/passwd/g
Can be written as:% [email protected]/var/log/[email protected]/etc/[email protected]
Practice:
1. Copy the/etc/grub.conf to/tmp directory and use the Find replacement command to delete the white space character at the beginning of the/tmp/grub.conf file
2. Copy the/etc/rc.d/init.d/functions file to the/tmp directory and add a # number to the beginning of the line beginning with each line of/tmp/functions with the Find replace command
2.9-vim Editor