d, followed by a cursor movement | Delete from the cursor position to the indicated position. | dj deletes the current line and the line below; dw deletes one word. |
dd | Deletes a line. | dd deletes the current line; 15dd deletes 15 lines. |
y, followed by a cursor movement | Yank from the cursor position to the indicated position. | yj yanks the current line and the line below; yw yanks one word. |
yy | Yanks a line. | yy yanks the current line; 15yy yanks 15 lines. |
p | Puts yanked or deleted text after the cursor. If the text contains any partial lines, it is inserted directly after the cursor; otherwise, it is inserted starting on the next line. | p puts one copy of the yanked text into the document after the cursor; 20p puts 20 copies of the yanked text after the cursor. |
P | Puts yanked or deleted text before the cursor. If the text contains any partial lines, it is inserted directly before the cursor; otherwise, it is inserted on the previous line. | P puts one copy of the yanked text into the document before the cursor; 20P puts 20 copies of the yanked text before the cursor. |
4.4.1.5. Searching
Typing / followed by some text (actually, a regular expression) and pressing Enter will search forward in the document. Typing n will repeat the previous search in the forward direction. Typing ? instead of / will search backward instead of forward; N will repeat a search backward.
Searching can be combined with deleting and yanking; for example, d/hello will delete from the cursor position up to the start of the word hello .
4.4.1.6. Undoing, redoing, and repeating
Pressing u will undo the last operation performed; pressing Ctrl-R will redo it. Typing a dot ( . ) will repeat the last operation.
4.4.1.7. Saving and exiting
There are a number of commands available for saving the document and exiting
Table 4-10. Saving text and exiting vi
Command | Description |
---|---|
:w | Write (save) using the current filename. |
:w | Write to the file |
:w! | Force-write (write even if in read-only mode). |
:q | Quit (succeeds only if the document is saved). |
:q! | Force quit even if the document isn't saved (abort!). |
:wq or :x or ZZ | Write and quit (exit with save). |
4.4.2. How Does It Work?