next up previous contents
Next: Adding and Moving Text Up: Guide to VI and Previous: Sentence - Paragraph -

Recovering Mistakes or Deleted Text


u undo last text change not regarding cursor position
if the last change was an undo, undo the preceding undo
U undo all changes made in current line (can be used only once)
not allowed if cursor is moved from current line
"1p recover last deleted text from buffer 1 after current line
"1P recover last deleted text from buffer 1 before current line


When change, delete or yank commands are executed, the text-object is copied into a vi buffer for a possible recover:

p put buffer contents in text after cursor pos
P put buffer contents in text before cursor pos

We can use p command to swap characters, words or lines:

xp swap current char with following char
dwwP swap current word with following word
ddp swap current line with following line

Lines are duplicated by yank as follows:

YP duplicate current line before (same as yyP)
Yp duplicate cuurent line after (same as yyp)

As vi maintains a history of deletion in the internal numbered buffers 1:9 in LIFO order, deleted lines are stored in such buffers and we can recover the last 9 delete operations by repeating with the dot command up to 8 times the "1p recover command, as shown in the following example.


Restore deleted lines from unnamed buffers
5dd delete 5 lines stored in buffer 1
i insert new text
<esc> end insert mode
6dd delete 6 more lines stored in buffer 2
G go to file end
"1P insert buffer 1 before cursor position
. repeat insert for buffer 1
n.b. - lines are retored in the same order of delete command


In the Restore example we first delete 5 lines, then insert some text and after we delete other 6 lines. Finally we restore the deleted lines in the same order. Note the difference between \framebox{\texttt{3dd}} that stores 3 lines in the current buffer and \framebox{\textbf{. . .}} that stores the same 3 lines in 3 sequential buffers. In the same way \framebox{\texttt{''1p}\textbf{ . .}} restores buffers 1-2-3 in reverse order, while \framebox{\texttt{''1p3}\textbf{.}}restores buffer 1 and one or three times buffer 2.
N.B. Behaviour is platform dependent, as some versions of vi do not support repeat count for the dot command.
Remember that buffer 1 contains the result of the last dd, buffer 2 of the last but one, and so on for a maximum of 9 dd commands.


Reverse deleted lines from unnamed buffers
dd delete one line
. delete next line
. delete next line; 3 lines deleted
i insert text
<escape> stop insert
  position cursor
"1p restore 3-rd line
. restore 2-nd line
. restore 1-rst line; restore lines in reverse order


The Reverse example shows the usage of vi internal buffers to reverse text order. Note that the dot command is mandatory to keep a correct buffer ordering. Care must also be exercised to avoid clobbering with buffer sequence when using in this way vi buffer internal facility.

When typing text with LATEX, a common mistake is the ordering of font defining commands, so that a font command like \textsf must be moved elsewhere. In our example we want to move \textsf{ after \verb in the simple sentence \textsf{\verb!123! any}. The same result is achieved either using words or chars as shown in the Exchange example.
The character movement is cleaner and simpler. First 8 characters are deleted, then the cursor is moved to the wanted position, finally characters are inserted before the cursor position (command P).
Using words a full word must be created first by inserting a <space> after {. Then we delete the full word, move to the wanted position and insert the word before the current cursor position (command P).


Exchange Deleted Words
\textsf{\verb!123! any} original line positioned at BOL
8<space>i<space><esc>0 insert a space after {; go to BOL
\textsf{ \verb!123! any} line with full word created at BOL
dW11<space>P exchange deleted word \textsf{
\verb!123! \textsf{ any} final result exchanging words
Exchange Deeleted Characters
8x11<space>P exchange deleted characters
123 \textsf{any} final result exchanging chars



next up previous contents
Next: Adding and Moving Text Up: Guide to VI and Previous: Sentence - Paragraph -
Marisa Luvisetto
2000-12-20