You have learned how to control the cursor, open files, switch files, and find content in files. These operations are all performed in Vim's normal mode. Now, it's time to enter another mode of Vim - insert mode, and learn how to modify the file.
i
insert before the current charactera
insert after the current characterI
insert at the beginning of the lineA
insert at the end of the lineo
insert in the next lineO
insert in the previous line
Note: Any of the above commands will cause Vim to enter insert mode. After
entering this mode, the cursor will change, and the text you enter will appear
directly in the document. Press Esc
or Ctrl-[
or Ctrl-C
to exit insert
mode.
s
delete the current character and enterINSERT
modeS
delete the current line and save it to the Vim clipboard, and enterINSERT
mode (equivalent tocc
)x
delete the current character, equivalent toDelete
in insert modeX
delete the previous character, equivalent toBackspace
in insert modedd
delete the current line and save it to the Vim clipboardd<X>
delete the specified content and save it to the Vim clipboardcc
delete the current line and save it to the Vim clipboard, and enterINSERT
modec<X>
delete the specified content and save it to the Vim clipboard, and enter
Note: The <X>
part is a description of the operation content. If you want to
delete a word, enter dw
or de
. If you want to copy the content from the
current position to the end of the line, enter y$
. If you want to delete the
next 3 characters and insert them, enter c3l
, and so on.
yy
copy the current line to the Vim clipboardy<X>
copy the specified content to the Vim clipboard
p
paste after the current positionP
paste before the current position
J
merge the current line with the next line
Try copying and pasting in the text below
Delete this line
Paste below this line
Cut ABC and paste it in front of XYZ to make this part look like
Cut and paste it in front of ABC XYZ.
r<X>
replace the current character with Xgu<X>
convert the specified text to lowercasegU<X>
convert the specified text to uppercase:%s/<search>/<replace>/
find the search content and replace it with the replace content
Try changing the case of the following text
Change this line to UPPERCASE, THEN TO lowercase.
There is a more interesting command
g~<X>
, first locate the cursor to the line of text above, execute0g~$
to see what happened.
u
undoCtrl-r
redo
:w
save the current file:wa
save all files:wq
orZZ
save and exit:q!
orZQ
force exit without saving:saveas <new filename>
save the file as<new filename>
:w <new filename>
save a copy of the file as<new filename>
and continue editing the original file
You can try to save the current (perhaps already changed beyond recognition) file as a copy, and then continue learning the next chapter.