Vim Tips Wiki
No edit summary
 
m (Recording keystrokes by "q" for repested jobs moved to Recording keys for repeated jobs: Page moved by JohnBot to improve title)

Revision as of 09:13, 18 October 2007

Previous TipNext Tip

Tip: #144 - Recording keys for repeated jobs

Created: October 21, 2001 9:43 Complexity: basic Author: Sameer Chabungbam Version: 5.7 Karma: 624/212 Imported from: Tip#144

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.


This feature is most useful when you want to do some repeated jobs, which you cant 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 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.


Sameer.

Comments

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?

revscat--AT--swbell.net , November 1, 2002 12:56


I sometimes wind up activating the recording mode by mistake. This was very helpful because now I know how to turn it off when I don't need it (and how to use it when I do).

Anonymous , March 4, 2004 7:40


It was very useful indeed, didn't know how to do a similar thing in emacs! Got another reason to love vim (apart from emacs).


anonymous--AT--somewhere.com , April 9, 2004 10:29


Now recording is my friend, not my enemy.

eolson--AT--starkinvestments.com , May 21, 2004 8:21


I'm trying to record a set of keystrokes that will end me up in insert mode. I want the last command to be "s," to put me in instert mode. But, I can't press q to end the recording process while insert mode; it just inserts a "q" character. How can I do this?

alex.kahn--AT--comcast.net , July 1, 2004 12:17


For anyone interested, I found the answer to my own question in the FAQ (go figure!). After pressing s to enter insert mode, I ended the recording by pressing Ctrl-O (to enter a command while in insert mode) and then pressing q.

Cheers.

alex.kahn--AT--comcast.net , July 1, 2004 12:23


Great tips on this page!! very useful. did not know how to use record feature which i used to turn on by mistake

muthukrishnan--AT--myrealbox.com , October 29, 2004 10:28


Yeah, me too. I only found this page because I wanted to turn off recording which I had accidentally turned on. Now I'd also like to know how to delete whatever got recorded, because I really don't like to think about what might happen if I accidentally press '--AT--'...

Anonymous , April 7, 2005 9:54


To delete a recording just record nothing over it. Eg qxq to erase whatever was bound to --AT--x

mroz--AT--ukgateway.net , May 17, 2005 6:33


How do you make macros work across an entire file?

jvacaq--AT--hotmail.com , May 31, 2005 10:20


Uhmm...I tried this recording stuff. I hit q0, then recorded some stuff, then hit q; then hit --AT--0 to repeat. Worked great. Then I hit --AT--0 again expecting the same operations to be carried out again, but I just got a bell (error) tone. Am I cursed?

Matt

spammymatt94--AT--yahoo.com , June 6, 2005 17:01


Matt - me too :-(

I don't get it - am looking for an answer as to why the register is lost for recorded keystrokes.

me--AT--myplace.net.com.yourmum , June 27, 2005 18:50


Registers can be used for other purposes than macros (:help registers). You can have a look at what happens to your macro before and after executing it for the second time with :registers. Have fun!

vimtips--AT--knackich.de , July 2, 2005 8:11


I use the Macro Recording Feature <q> ALOT.... and the repeat last command <.> What I would like to do is Repeat the Last Two Commands with a single key.

Can this be done without creating a Macro with <q>?

btnowak--AT--hotmail.com , July 8, 2005 11:26


>> How do you make macros work across an entire file? I don't know exactly, but remember that you can use number prefix. For example, if you want to comment out 20 lines, type qqI#<esc>j<esc>q //this puts "Insert # at the beginning of a line, exit insert mode and go down 1 row" in register q (it's qq capital-i at the beginning, in case your font makes uppercase-i's look like lowercase-L's 20--AT--q //executes q-macro 20 times


anonymous , November 17, 2005 10:03


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

vim *.cpp #under console q0 #start recording

1,$s/OldString/NewString/g
wnext

q #stop recording

--AT--0 #give it a try to see whether it works correctly

999--AT--0 #repeat 999 times to complete the dull job

cheng--AT--mailst.xjtu.edu.cn , December 11, 2005 0:13


I think recording should be turned off by default (say, unless you enable it in your vimrc).

Most of the vim users I have seen (some of whom have used vim for years, if not 100% of it's formidable feature set) are aware of the recording feature only as a pittrap to fall into which forces them to kill the process.

I know vim is designed to be a powerful tool first, and userfriendly second, and I respect that; yet this is one command that might be better off disabled by default?

Anonymous , March 7, 2006 17:42


If the "Now recording" message simply said "Now recording (press q to finish)", then there would never be any confusion about how to escape that mode...

deven--AT--ties.org , April 12, 2006 10:31


Hi, 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.


zzapper http://SuccessTheory.com/ , June 5, 2006 5:05


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 --AT--a) I do this:

enew #to start editing a new file

"ap # to paste the contents of register a into the file [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 <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 $)

bd! # to delete the new buffer without saving

--AT--a # to execute my modified recording

I hope this helps some newbies who would like to start utilizing recordings but have a hard time getting the task right on the first try.

I hope someone else can show me a cleaner way of doing this. ;-) I too am new (12 months vim experience)

RichardBronosky (first--AT--last.com) , June 5, 2006 13:20


To edit the contents of register x interactively, use

:let --AT--x=<Ctrl-R>x 

--AT--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.

Neil (neil at wynded dot co dot uk) , June 6, 2006 9:39


... except, of course, I forgot to mention that you'll need to quote the string for the assignment to work

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


Neil (neil at wynded dot co dot uk) , June 6, 2006 9:41


... and, 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.

" edit register y

map <F8>y :let --AT--y="^Ry"<Home><Right><Right><Right><Right><Right><Right><Right><Right>

" edit register q

map <F8>q :let --AT--q="^Rq"<Home><Right><Right><Right><Right><Right><Right><Right><Right>

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...)


rpresser AT gmail DOT com , December 9, 2006 22:41


Ooops. I should have typed <Ctrl-R> instead of ^R, to make it clear what was meant. Sorry.

rpresser AT gmail DOT com , December 9, 2006 22:42