Wikia

Vim Tips Wiki

Use the last-inserted-text register

Talk0
1,610pages on
this wiki
Tip 1029 Printable Monobook Previous Next

created 2005 · complexity basic · author bwana147 · version 5.7


I sometimes want to insert text at the beginning of a line, but if I happen to already be at the beginning of that line, I use the i command instead of I. Then, I realize that the same text has to be inserted at the beginning of other lines as well: I go there and hit ".", and the text gets inserted wherever I am in the line (which of course is seldom the beginning). So I have to "u"ndo, hit "I", retype my text and go one moving around and hit "." whenever I want the same text at the beginning of that line.

What bothers me is the retyping. I would like to change the "repeat last command", so that it becomes "insert at the beginning" instead of "insert wherever you happen to be".

Here comes the ". register. All I do now, is "u"ndo to remove the text that was wrongly inserted in the middle of the line, hit "I" to start inserting at the beginning of the line, then <C-R>. (that's Ctrl-R followed by a dot), which inserts whatever I had just inserted with the "i" command, and <Esc>, of course.

Now the "repeat command" (.) will rightfully insert at the beginning of the lines where I invoke it, and I didn't have to retype my inserted text in full.

CommentsEdit

Is this the same as

  • do edit in wrong place
  • undo, go to beginning of line, repeat with u0.

This tip lets you use '.' after the first fix. Your suggested solution requires you to do '0.' every time you want to insert the line again.


See also :help gI.


I finally sussed this useful tip.

  • do edit in wrong place
  • u to undo
  • I to insert at beginning of line
  • <C-R>. to insert undone text

Now dot works fine.

uI<C-R>.<Esc>j.

<C-R>. is better done as <C-A> see :help i_CTRL-A.


Advertisement | Your ad here

Around Wikia's network

Random Wiki