Vim Tips Wiki
Register
(Adjust previous/next navigation + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 14: Line 14:
 
You can accidentally lose text that you're typing in &ndash; text that you can't recover with undo. This tip allows you to recover your lost text, and to avoid future problems with a mapping.
 
You can accidentally lose text that you're typing in &ndash; text that you can't recover with undo. This tip allows you to recover your lost text, and to avoid future problems with a mapping.
   
In insert mode, pressing Ctrl-u deletes text you've typed in the current line, and Ctrl-w deletes the word before the cursor. You can't undo these deletions. However, what you've typed is still in the <tt>'''.'''</tt> register. You can confirm that (after pressing Esc to return to normal mode) with the command <tt>:reg</tt> which will list all registers (or just <tt>:reg .</tt> to display the <tt>'''.'''</tt> register). You may be able to copy the missing text from the register display, for example, with the mouse.
+
In insert mode, pressing Ctrl-u deletes text you've typed in the current line, and Ctrl-w deletes the word before the cursor. You can't undo these deletions. However, what you've typed is still in the <code>'''.'''</code> register. You can confirm that (after pressing Esc to return to normal mode) with the command <code>:reg</code> which will list all registers (or just <code>:reg .</code> to display the <code>'''.'''</code> register). You may be able to copy the missing text from the register display, for example, with the mouse.
   
Unfortunately, simply pasting the <tt>'''.'''</tt> register won't help because it will repeat the Ctrl-u or Ctrl-w and will delete the text again. However, you can use another register (register <tt>a</tt> in the following):
+
Unfortunately, simply pasting the <code>'''.'''</code> register won't help because it will repeat the Ctrl-u or Ctrl-w and will delete the text again. However, you can use another register (register <code>a</code> in the following):
   
 
<pre>
 
<pre>
Line 31: Line 31:
 
</pre>
 
</pre>
   
Now Ctrl-u and Ctrl-w will work as before, but they first use Ctrl-g <tt>u</tt> to start a new change, as far as undo is concerned. For example, in insert mode, you might type several lines then accidentally press Ctrl-u which deletes the last line. If you have used the above mapping, you can press Esc to return to normal mode, then <tt>u</tt> to undo, which will recover the last line.
+
Now Ctrl-u and Ctrl-w will work as before, but they first use Ctrl-g <code>u</code> to start a new change, as far as undo is concerned. For example, in insert mode, you might type several lines then accidentally press Ctrl-u which deletes the last line. If you have used the above mapping, you can press Esc to return to normal mode, then <code>u</code> to undo, which will recover the last line.
   
 
The first mapping (for <C-U>) is now included by default in the vimrc_example.vim distributed with Vim.
 
The first mapping (for <C-U>) is now included by default in the vimrc_example.vim distributed with Vim.
   
 
==Explanation==
 
==Explanation==
Generally, when you insert text (after an <tt>i</tt> or <tt>o</tt> or other similar command) you make a single modification to the file that forms one undo block. Pressing Ctrl-u or Ctrl-w while in insert mode is just part of that single modification. After pressing Esc to return to Normal mode, if you press <tt>u</tt> you will undo all your typing. Therefore, you have lost text deleted with Ctrl-u or Ctrl-w.
+
Generally, when you insert text (after an <code>i</code> or <code>o</code> or other similar command) you make a single modification to the file that forms one undo block. Pressing Ctrl-u or Ctrl-w while in insert mode is just part of that single modification. After pressing Esc to return to Normal mode, if you press <code>u</code> you will undo all your typing. Therefore, you have lost text deleted with Ctrl-u or Ctrl-w.
   
However, some insert-mode commands break the undo block so the insertion consists of more than a single modification. One of those commands is Ctrl-g <tt>u</tt>.
+
However, some insert-mode commands break the undo block so the insertion consists of more than a single modification. One of those commands is Ctrl-g <code>u</code>.
   
 
==References==
 
==References==
 
*{{help|i_CTRL-U}} Insert mode: <c-u> deletes text entered in the current line.
 
*{{help|i_CTRL-U}} Insert mode: <c-u> deletes text entered in the current line.
 
*{{help|i_CTRL-W}} Insert mode: <c-w> deletes word before cursor.
 
*{{help|i_CTRL-W}} Insert mode: <c-w> deletes word before cursor.
*{{help|i_CTRL-G_u}} Insert mode: <c-g> <tt>u</tt> starts a new change.
+
*{{help|i_CTRL-G_u}} Insert mode: <c-g> <code>u</code> starts a new change.
 
*{{help|ins-special-special}} Insert mode: Commands which start a new change.
 
*{{help|ins-special-special}} Insert mode: Commands which start a new change.
   
 
==Comments==
 
==Comments==
The following allows you to paste all of <tt>".</tt>, while using backspace to delete the Ctrl-u at the end (assuming you accidentally typed Ctrl-u):
+
The following allows you to paste all of <code>".</code>, while using backspace to delete the Ctrl-u at the end (assuming you accidentally typed Ctrl-u):
   
 
<pre>
 
<pre>
Line 53: Line 53:
 
</pre>
 
</pre>
   
The above command puts the <tt>'...'</tt> string following the expression register <tt>=</tt>. Using Ctrl-r twice inserts text from the following register literally.
+
The above command puts the <code>'...'</code> string following the expression register <code>=</code>. Using Ctrl-r twice inserts text from the following register literally.
   
 
*{{help|c_CTRL-R_CTRL-R}}
 
*{{help|c_CTRL-R_CTRL-R}}

Latest revision as of 05:30, 13 July 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 436 Printable Monobook Previous Next

created 2003 · complexity basic · author John Wright · version 6.0


You can accidentally lose text that you're typing in – text that you can't recover with undo. This tip allows you to recover your lost text, and to avoid future problems with a mapping.

In insert mode, pressing Ctrl-u deletes text you've typed in the current line, and Ctrl-w deletes the word before the cursor. You can't undo these deletions. However, what you've typed is still in the . register. You can confirm that (after pressing Esc to return to normal mode) with the command :reg which will list all registers (or just :reg . to display the . register). You may be able to copy the missing text from the register display, for example, with the mouse.

Unfortunately, simply pasting the . register won't help because it will repeat the Ctrl-u or Ctrl-w and will delete the text again. However, you can use another register (register a in the following):

:let @a = @.
"aP

The above will paste all the text you last inserted, including what was accidentally deleted.

To avoid the problem in the future, put the following in your vimrc:

inoremap <c-u> <c-g>u<c-u>
inoremap <c-w> <c-g>u<c-w>

Now Ctrl-u and Ctrl-w will work as before, but they first use Ctrl-g u to start a new change, as far as undo is concerned. For example, in insert mode, you might type several lines then accidentally press Ctrl-u which deletes the last line. If you have used the above mapping, you can press Esc to return to normal mode, then u to undo, which will recover the last line.

The first mapping (for <C-U>) is now included by default in the vimrc_example.vim distributed with Vim.

Explanation[]

Generally, when you insert text (after an i or o or other similar command) you make a single modification to the file that forms one undo block. Pressing Ctrl-u or Ctrl-w while in insert mode is just part of that single modification. After pressing Esc to return to Normal mode, if you press u you will undo all your typing. Therefore, you have lost text deleted with Ctrl-u or Ctrl-w.

However, some insert-mode commands break the undo block so the insertion consists of more than a single modification. One of those commands is Ctrl-g u.

References[]

Comments[]

The following allows you to paste all of "., while using backspace to delete the Ctrl-u at the end (assuming you accidentally typed Ctrl-u):

:put ='<C-R><C-R>.<BS>'

The above command puts the '...' string following the expression register =. Using Ctrl-r twice inserts text from the following register literally.


Here are a few maps that'll let you use Ctrl-W to delete the previous word, Ctrl-U to delete a line, and Ctrl-Y to paste what you've deleted back, all while remaining in insert mode:

inoremap <silent> <C-W> <C-\><C-O>db
inoremap <silent> <C-U> <C-\><C-O>d0
inoremap <silent> <C-Y> <C-R>"