Chapter 3: Modifying and Saving Files
Modify the document
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.
Insert
iinsert before the current characterainsert after the current characterIinsert at the beginning of the lineAinsert at the end of the lineoinsert in the next lineOinsert 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.
Delete (and save to Vim clipboard)
sdelete the current character and enterINSERTmodeSdelete the current line and save it to the Vim clipboard, and enterINSERTmode (equivalent tocc)xdelete the current character, equivalent toDeletein insert modeXdelete the previous character, equivalent toBackspacein insert modedddelete the current line and save it to the Vim clipboardd<X>delete the specified content and save it to the Vim clipboardccdelete the current line and save it to the Vim clipboard, and enterINSERTmodec<X>delete the specified content and save it to the Vim clipboard, and enter
Vim's command grammar
This is the most useful pattern in Vim: many edits can be read as [count] + operator + motion.
- The operator says what to do:
ddeletes,cchanges, andycopies. - The motion says how far:
wto the next word,$to line end,}to the next paragraph. - A count repeats the action:
2dwdeletes two words;d2wdoes the same.
Thus dw means “delete to the next word,” c$ means “change to line end,” and y} means “copy to the next paragraph.” Each new motion you learn immediately becomes a new way to delete, change, or copy; there is no separate shortcut list.
Note: Repeating an operator makes it linewise: dd, cc, and yy. After deleting, c enters Insert mode while d stays in Normal mode.
Copy
yycopy the current line to the Vim clipboardy<X>copy the specified content to the Vim clipboard
Paste
ppaste after the current positionPpaste before the current position
Merge
Jmerge 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.On an ordinary line, try
dw,u,c$, type a few characters,Esc, thenu. Before each command, say its operator and range. Finish by undoing any remaining practice edits.
Replace
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>/greplace every match on every line in the file
:s works on the current line and replaces only its first match by default. % expands the range to the whole file, g replaces every match per line, and adding c (gc) asks for confirmation at each match.
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.
Undo, Redo
uundoCtrl-rredo
Save file
:wsave the current file:wasave all files:wqorZZsave and exit:q!orZQforce 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.