sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #170: Repeating a sequence of commands without defining a macro

 tip karma   Rating 61/34, Viewed by 3663 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   November 27, 2001 23:45      complexity:   intermediate
author:   Stanislav Sitar      as of Vim:   6.0


You have just finished a complicated modification of a file,
involving numerous replace commands :%s/xxx/yyyy/g,
and other ex commands.

Then you realize, you have done it a little bit wrong,
and you have to begin all the operation again,
just to change one replace string, or do one more operation
"somewhere 10 commands ago".

Or you realize, you will have to do the same stuff tomorrow
with another file.

or you realize, you want to perform the same sequence
of commands, you have typed a few days ago

You should have made it a macro (normal command q),
but you haven't.

Nothing is lost yet.

You go to the command line (by typing :)
and press Ctrl+F.
(Ctrl+F in other modes scrolls the screen)

You get a temporary window, listing the history of command line.
It is possible to yank appropriate lines here,
make a new file called $VIMRUNTIME/macros/something.vim
put those lines here, edit them and save

:help cedit

Then you can call the macro using
:source something.vim

You might want to set variable 'history' to a higher
number then default in your vimrc file
:set history=300
:help history
:help vimrc

 rate this tip  Life Changing Helpful Unfulfilling 

<< <Tab> = <C-I> and <Esc> = <C-[> | Do you know the "g/" and "g?" commands? >>

Additional Notes

jmcpherson@bigfoot.com, December 5, 2001 11:38
You can also bring up the command line history while in Normal mode by typing:


which is just a touch faster than :<Ctrl-F>. (-: (Actually, I didn't even know about Ctrl-F in : mode until I read this tip. Neat!)
sathishvj@yahoo.com.__spam_stop, May 2, 2003 14:48
Lemme add a note that I saw elsewhere.

Once you execute a command line or search command you can retrieve the text thus:

i<C-R>:                 - for pasting the recent command line command
i<C-R>/                 - for pasting the recent search command

* I wish every editor had an in built VIM *
f@farhan-ahmad.net___spam_stop, June 3, 2005 13:10
Man, I can't believe I didn't know about the command-line window. This is awesome! You can actually edit a command (fine tune a regex e.g.) and then run it right from there (by pressing <CR>).

The normal save commands also work, so you can easily edit the sequence of the commands in the command-line window and once you are done just do ":w myMacro.vim" to save the macro in the current directory (or as the author of the comment suggested use the $VIMRUNTIME... path for easier accessibility).
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo