Vim Tips Wiki
(link to just added recursive recording tip)
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
+
{{Duplicate|398}}
 
{{TipImported
 
{{TipImported
 
|id=144
 
|id=144
 
|previous=143
 
|previous=143
 
|next=146
 
|next=146
|created=October 21, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Sameer Chabungbam
 
|author=Sameer Chabungbam
|version=5.7
+
|version=6.0
 
|rating=624/212
 
|rating=624/212
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
The most useful feature that I find in VIM is the "recording" feature ({{help|recording}}). I have used this to automatically insert function headers, re-indent lines, and convert some 34 source files into HTML.
+
One of Vim's most useful features is its ability to record what you type for later playback. {{help|recording}} This is most useful for repeated jobs which cannot easily be done with "<code>.</code>".
   
  +
To start recording, press <code>q</code> in normal mode followed by a letter (<code>a</code> to <code>z</code>). That starts recording keystrokes to the specified register. Vim displays '''recording''' in the status line. Type any normal mode commands, or enter insert mode and type text. To stop recording, again press <code>q</code> while in normal mode.
This feature is most useful when you want to do some repeated jobs, which you can't do easily using ".". You can set about writing a function, define a mapping, etc, but then these things might take time. By recording, you can try out and find the actual keystrokes that does the job.
 
   
 
To playback your keystrokes, press <code>@</code> followed by the letter previously chosen. Typing <code>@@</code> repeats the last playback.
To start recording, press "q" in normal mode followed by any of "0-9a-z". This will start recording the keystrokes to the register you choose. You can also see the word "recording" in the status(?) line. You can start the key sequences that you want to record. You can go to insert mode and type if you want.
 
 
To stop recording, press "q" in the normal mode.
 
 
To playback your keystrokes, press "@" followed by the character you choose. Pressing "@@" will repeat the same again.
 
   
 
==See also==
 
==See also==
*[[Recursive repeats for quick editing of structured text|A way to record recursive macros]]
+
* [[Recursive repeats for quick editing of structured text|A way to record recursive macros]]
  +
* [[Easy playback of recorded keys]]
  +
** [[macros]]
   
 
==Comments==
 
==Comments==
 
To delete a recording just record nothing over it. For example, <code>qxq</code> erases whatever was recorded to register <code>x</code>.
This is well and good, but how do you make macros work across buffers or a split-screen? I frequently compare two files in split-screen mode, and can't get recorded macros to work it I have to move between screens. Any ideas?
 
  +
:or <code><nowiki>:call setreg('x', '')</nowiki></code>
  +
:or <code><nowiki>:let @x = ''</nowiki></code>
   
 
----
 
----
 
Here is an example to replace string OldString with NewString contained in multiple *.cpp files:
To delete a recording just record nothing over it. Eg qxq to erase whatever was bound to @x
 
 
----
 
Here is an example for replace string OldString with NewString contained in multiple *.cpp files:
 
 
 
<pre>
 
<pre>
vim *.cpp #under console
+
vim *.cpp
q0 #start recording
+
qx # start recording to register x
:1,$s/OldString/NewString/g
+
:%s/OldString/NewString/g
 
:wnext
 
:wnext
q #stop recording
+
q # stop recording
 
@x # playback to see if it works correctly
 
 
999@x # repeat 999 times to complete the job
@0 #give it a try to see whether it works correctly
 
999@0 #repeat 999 times to complete the dull job
 
 
</pre>
 
</pre>
   
 
----
 
----
  +
One way to edit a recording (for example, to register <code>x</code>) is to paste it into a new buffer, then edit the buffer, then copy the results back into the register. For example:
Just in case it hasn't been made 100% clear dont use numeric registers for recordings. eg q0,q1..q9 as these are the Vi(m) shuffle memories of recent changes. Use instead letters a-z the cutest is q as it's very quick to type qq
 
 
Recordings allow you to do repetitive edits. There is often a cleverer way to do this but recordings are so easy to implement.
 
 
----
 
I'm looking for a way to edit a register (which contains a recording, but registers are just registers regardless of what they hold.) similar to doing q: and q/ to edit command history and search history respectively.
 
 
I often times make a recording that is perfect except for one little change. And it tortures me to do :registers and see my mistake there taunting me, and not be able to do something like q"a to edit register "a.
 
 
Right now to edit the recording in "a (for the newbies out there that is register a, or the recording that was recorded using qa, or the recording that would be executed by @a) I do this:
 
 
 
<pre>
 
<pre>
:enew #to start editing a new file
+
:new # new buffer
"ap # to paste the contents of register a into the file
+
"xp # paste register x into the buffer
  +
[edit the keystrokes]
[edit the keystrokes, which is most often just inserting a "home" keystroke at the beginning with <C-V><home>]
 
<Esc> # to get out of insert mode
+
<Esc> # return to normal mode
<home>"ay$ # to go to the beginning of the line and yank into register a everything to the end of the line (represented in grep by a $)
+
0"xy$ # go to beginning of line; into register x, yank to end of line
:bd! # to delete the new buffer without saving
+
:bd! # delete the new buffer without saving
@a # to execute my modified recording
+
@x # execute modified recording
 
</pre>
 
</pre>
   
 
Alternatively, edit the contents of register <code>x</code> in the command line:
----
 
To edit the contents of register x interactively, use
 
 
 
<pre>
 
<pre>
:let @x="<Ctrl-R>x"
+
:let @x="<Ctrl-R><Ctrl-R>x"
 
</pre>
 
</pre>
   
 
The name <code>@x</code> identifes register <code>x</code>. At the command line, Ctrl-R pastes the contents of the named register into the command line, thus making it available for editing.
@x is a way to assign to a named register (in this case register x)
 
At the command line, Ctrl-R pastes the contents of the named register into the command line, thus making it available for editing.
 
 
At the risk of further digression, Ctrl-R / can be a very useful way of using the string that you just searched for.
 
 
----
 
for ultimate ease of use, use key bindings that bring up the :let command for you. I use F8, but something with <Leader> might be better for you.
 
 
<pre>
 
" edit register y
 
:map <F8>y :let @y="^Ry"<Home><Right><Right><Right><Right><Right><Right><Right><Right>
 
" edit register q
 
:map <F8>q :let @q="^Rq"<Home><Right><Right><Right><Right><Right><Right><Right><Right>
 
</pre>
 
 
There are eight <Right>s there; that positions the cursor right after the first quote mark.
 
 
If there's only one register you need to edit frequently, then just change <F8>y to <F8>.
 
 
(I thought about changing this into a function or a command that would take a register name as an argument, but it was much easier to figure out how to do it as a map, even if it means you have to repeat the map for several different registers you edit frequently...)
 
   
  +
:be careful about newlines
I should have typed <Ctrl-R> instead of ^R, to make it clear what was meant. Sorry.
 
   
 
----
 
----

Revision as of 02:56, 3 August 2012

Duplicate tip

This tip is very similar to the following:

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

Tip 144 Printable Monobook Previous Next

created 2001 · complexity basic · author Sameer Chabungbam · version 6.0


One of Vim's most useful features is its ability to record what you type for later playback. :help recording This is most useful for repeated jobs which cannot easily be done with ".".

To start recording, press q in normal mode followed by a letter (a to z). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text. To stop recording, again press q while in normal mode.

To playback your keystrokes, press @ followed by the letter previously chosen. Typing @@ repeats the last playback.

See also

Comments

To delete a recording just record nothing over it. For example, qxq erases whatever was recorded to register x.

or :call setreg('x', '')
or :let @x = ''

Here is an example to replace string OldString with NewString contained in multiple *.cpp files:

vim *.cpp
qx            # start recording to register x
:%s/OldString/NewString/g
:wnext
q             # stop recording
@x            # playback to see if it works correctly
999@x         # repeat 999 times to complete the job

One way to edit a recording (for example, to register x) is to paste it into a new buffer, then edit the buffer, then copy the results back into the register. For example:

:new      # new buffer
"xp       # paste register x into the buffer
[edit the keystrokes]
<Esc>     # return to normal mode
0"xy$     # go to beginning of line; into register x, yank to end of line
:bd!      # delete the new buffer without saving
@x        # execute modified recording

Alternatively, edit the contents of register x in the command line:

:let @x="<Ctrl-R><Ctrl-R>x"

The name @x identifes register x. At the command line, Ctrl-R pastes the contents of the named register into the command line, thus making it available for editing.

be careful about newlines