Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(Add info on repeating a change, from vim_use.)
(2 intermediate revisions by 2 users not shown)
Line 22: Line 22:
   
 
Or, you might use insert mode to type "<code>hello </code>". Press Esc for normal mode, then move the cursor, and press <code>.</code> to insert "hello " again.
 
Or, you might use insert mode to type "<code>hello </code>". Press Esc for normal mode, then move the cursor, and press <code>.</code> to insert "hello " again.
  +
  +
==Copy a word to multiple locations==
  +
Type <code>yiw</code> to copy the current word. Move the cursor to a new location, then type <code>ciw<C-R>0<Esc></code> to change the word, replacing it with the copied text. After moving the cursor to a new location, press <code>.</code> to repeat the operation (the current word will be replaced with the word that was originally copied). See [[replace a word with yanked text]].
  +
  +
==Copy a line to multiple locations==
  +
Move the cursor to the wanted line and press <code>Y</code> to copy it. Move the cursor to a new location, then press <code>p</code> to paste the line after the current line, or <code>P</code> to paste before the current line. After moving the cursor to a new location, press <code>.</code> to repeat the paste.
  +
  +
It is also possible to paste the copied line over the destination (replacing the current line). To do that once, copy the wanted line with <code>Y</code>, then move to the destination, press <code>V</code> to visually select the destination line, then <code>p</code> to paste the copied line over the selected line.
  +
  +
A different procedure should be used if the replace operation needs to be repeated. Move the cursor to the wanted line and type <code>0y$</code> to copy the line, without the line ending (<code>0</code> moves to the beginning of the line; <code>y$</code> copies to the end of the line). Move the cursor to a new location, then type <code>S<C-R>0<Esc></code> (<code>S</code> then Ctrl-R then <code>0</code> then Escape). After moving the cursor to a new location, press <code>.</code> to repeat the operation (the current line will be replaced with the line that was originally copied).
  +
  +
Explanation: Pressing <code>S</code> deletes all text from the current line (but not the line ending) and starts an insert operation. In insert mode, pressing Ctrl-R inserts the contents of a register. Register <code>0</code> (zero) contains the text that was copied with <code>y$</code>.
   
 
==See also==
 
==See also==
Line 35: Line 47:
   
 
==Comments==
 
==Comments==
*plugin {{script|id=2136|text=repeat.vim}} to repeat your own complex mappings and functions; see comments in script header for use
+
*plugin [[Script:2136|repeat.vim]] ({{script|id=2136|text=repeat.vim}}) to repeat your own complex mappings and functions; see comments in script header for use
*{{tt|@@}} to repeat last played macro
+
*<code>@@</code> to repeat last played macro
*{{tt|n}}, {{tt|N}}, {{tt|;}}, {{tt|,}} to repeat searches
+
*<code>n</code>, <code>N</code>, <code>;</code>, <code>,</code> to repeat searches
*{{tt|&amp;}} to repeat subsititution
+
*{{code|&}} to repeat subsititution
 
*{{help|prefix=no|id=i_CTRL-A}} also known as {{help|prefix=no|id=i_CTRL-R_.}}
 
*{{help|prefix=no|id=i_CTRL-A}} also known as {{help|prefix=no|id=i_CTRL-R_.}}
 
*[[Undo and Redo]]
 
*[[Undo and Redo]]

Revision as of 21:36, 16 October 2012

Tip 44 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


The "." command repeats the last change made in normal mode. For example, if you press dw to delete a word, you can then press . to delete another word (. is dot, aka period or full stop).

The "@:" command repeats the last command-line change (a command invoked with ":", for example :s/old/new/).

You can move the cursor before using either of the repeat commands.

Suppose you press dd to delete a line. Next, you might move the cursor, then press 5. (5 then dot). That will delete 5 lines.

In normal mode, press J to join the next line onto the current line. Press . to join more lines.

Or, you might use insert mode to type "hello ". Press Esc for normal mode, then move the cursor, and press . to insert "hello " again.

Copy a word to multiple locations

Type yiw to copy the current word. Move the cursor to a new location, then type ciw<C-R>0<Esc> to change the word, replacing it with the copied text. After moving the cursor to a new location, press . to repeat the operation (the current word will be replaced with the word that was originally copied). See replace a word with yanked text.

Copy a line to multiple locations

Move the cursor to the wanted line and press Y to copy it. Move the cursor to a new location, then press p to paste the line after the current line, or P to paste before the current line. After moving the cursor to a new location, press . to repeat the paste.

It is also possible to paste the copied line over the destination (replacing the current line). To do that once, copy the wanted line with Y, then move to the destination, press V to visually select the destination line, then p to paste the copied line over the selected line.

A different procedure should be used if the replace operation needs to be repeated. Move the cursor to the wanted line and type 0y$ to copy the line, without the line ending (0 moves to the beginning of the line; y$ copies to the end of the line). Move the cursor to a new location, then type S<C-R>0<Esc> (S then Ctrl-R then 0 then Escape). After moving the cursor to a new location, press . to repeat the operation (the current line will be replaced with the line that was originally copied).

Explanation: Pressing S deletes all text from the current line (but not the line ending) and starts an insert operation. In insert mode, pressing Ctrl-R inserts the contents of a register. Register 0 (zero) contains the text that was copied with y$.

See also

References

Comments