Skip to the content of the web site.

4. Advanced editing

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

We will now look at various ways of editing a document.


1. Deleting text: d

The following ways are used to delete text.

1.1 Deleting a line: dd

If you are on any line and in command mode press dd, that line will be deleted. If you press a number and then dd, that many lines will be deleted, although it will only delete up to the end of the file.

1.2 Deleting up to a search

If you press d and then perform any move or search, vi will perform an intelligent delete based on the type of move or search you made.

When text is deleted, it is copied to a clipboard.

For example, if you enter dw, it will delete everything up to the start of the next word. If you enter de, it will delete every character up to the last character of the current word. Similarly, db will delete all characters in the current word up to and including the first character in the current word.

To delete up to the end of the line, or up to the start of the line, use d$ and d0, respectively.

If you enter d/, you will now perform a search. If a match is found for the given search, vi will delete all characters up until the searched-for text.

If you press d and then press f or F followed by another character, vi will delete up to and including that searched-for character. If you are searching forward, it will also delete the character on which the cursor started, but if you are searching backward, it will leave the character on which the cursor started.

If you want to delete up to but not including the next instance of a character in a line, use dt or dT.

If you want to delete an entire block of code in C++, place the cursor on one delimiter, and then enter d$. vi will delete all characters including the delimiters.

If you want to delete up to the start of the next or previous paragraph separator, use d} and d{, respectively.

Always remember that you can undo a deletion by pressing u.

What is important here is that once you learn how to move and search within vi, the d command will perform a deletion in the direction of that move or search.


2. Pasting from the clipboard: pP

Once you delete text, you can paste that text anywhere you wish by first moving the cursor and then using either p or P.

If you have deleted a number of lines using ndd where n is a number, then p will paste those lines following the current line, while if you press P, it will paste those lines immediately before the current line.

If you have text by some form of search, pressing p will paste that text immediately after where the cursor is currently located; while pressing P will paste that text before where the cursor currently is.

If you enter a number before you press p or P, as you may suspect, it inserts that number of copies of what is on the clipboard to the text.

You will note that if you place the cursor on a character and then type xp, the x deletes the character, and then that deleted character is placed immediately after the following character. Thus, xp is a quick way to swap two characters.


3. Copying (yanking) to the clipboard: y

The letter y behaves exactly like d, only the text that would be deleted with d is only copied to the clipboard. That copied (or yanked) text can then be pasted elsewhere using either p or P.


4. Changing text: c

The letter c behaves exactly like d, only once the deletion is made, you are immediately placed into insert mode.


5. Visually deleting, yanking or changing: vV

If you are not clear exactly what text you want to delete, yank or change, you can press v and now use movement keys. You will see text highlighted between where the cursor started and where you have moved. If you now press either d, y or c, the highlighted text will be deleted, yanked, or changed (deleted and going into insert mode), respectively.

Starting with V highlights entire lines, starting with the line on which the cursor was when you pressed V. Like with v, you can now press d, y or c.

There are two other keys that are exceptionally useful after you have highlighted text in visual mode: < and > will either remove a Tab at the start of the lines or add a Tab at the start of the line.


6. Deleting text and replacing it: s

You will recall that x deletes a character, and if n is a number, then nx deletes that many characters. Using s in a similar manner deletes characters but also puts you into insert mode, so you can now start entering text.


7. Deleting an entire line and replacing it: S

If in command mode you press S, the entire line is deleted and you are put into insert mode.


8. Opening up a new line: oO

If in command mode you press o, it opens up a new empty line below the current line and puts you into insert mode.

If you press O, it opens up a new line empty line before the current line and puts you into insert mode.


9. Joining lines: J

If you want to remove the new line at the end of the current line and join the current line with the next, just press J in command mode.


10. Deleting, yanking, or changing entire words

If you are not at the start of a word, a command like dw will only delete up to the end of the next word.

If you want to delete, yank or change the entire current word, use diw, yiw or ciw, respectively.

There is no way to deduce these three edits from other edits.


11. Repeat the last edit: .

If you press ., the most recent edit you performed is done again. It is as if you typed the exact same sequence of characters, but now with the cursor where it currently is.


12. Switching case: ~

If you press ~ while on top of a letter of the alphabet, the case of that letter is switched and the cursor moves forward to the next character. Non-letters are unaffected by this command.

If n is a number, then n~ will change that number of characters starting where the cursor currently is, but only up to the end of the line.


13. Deleting or changing up to the end of the line: DC

If you press D, all characters including the where the cursor currently is and up to the end of the line are deleted. If instead you pressed C, you are also then put into insert mode.

If n is a number, then nD will delete up to the end of the current line but also delete the next n - 1 lines, as well; while nC has the same effect but putting you into insert mode.