How can I store a series of search and replace statements so I can run them later in sequence?

Possibly after I have close and re-opened vim...

For instance I might run the following in sequence to convert a pasted table into a wiki formatted table with headers:

  • 1,1s/^\|\t/|!/g
  • 1,1s/$/|/g
  • 2,$s/^\|\t/|/g
  • 2,$s/$/|/g

4 Answers 4


Put them in a function (into your vimrc or a file in the plugin folder):

function! Stuff()

You can then use :call Stuff()<cr> to run them or make a custom command:

command DoStuff call Stuff()

And just do :DoStuff<cr>

If you wanna run them directly from a buffer first copy them and then do :@"<cr>

You can also copy commands you've run earlier by doing q: and copying them from there

  • Ah okay so I can just add that to my .vimrc then?
    – leeand00
    Mar 3, 2015 at 2:06
  • PLEASE NOTE: "E183: User defined commands must start with an uppercase letter" this caught me up. as I am not used to naming functions starting with capital letters, my first attempt would not work. My function definition was failing silently, as the file with functions was sourced from .vimrc within a try-catch block so no apparent error, but functions named with lowercase letter will not load properly. Oct 28, 2016 at 9:56

You can run all of them in the same command by joining them with a | (vertical bar):

:1,1s/^\|\t/|!/g | 1,1s/$/|/g | 2,$s/^\|\t/|/g | 2,$s/$/|/g

It will then be available in your command history (: or q :), which should be persistent across Vim sessions as long as you have your .viminfo set up corectly.

  • how should the .viminfo be setup? (and, please don't say 'correctly' ;-)) Oct 28, 2016 at 9:40
  • @PeterPerháč It should work correctly by default, as far as I know. (There might be situations where the viminfo file is owned by root, though, preventing the regular user from reading it, which might be caused by using sudo vim or sudo -s/su instead of sudoedit.)
    – Doorknob
    Oct 28, 2016 at 12:11

One of the simplest ways is save these commands to a file and :source the file.

Example, add the following to foo.vim:


Now you can source the file via :source foo.vim to run those commands.

If you find yourself using these commands often then you may want to create a command (possibly using a function)

For more help see:

:h :source
  • I endorse this approach! Collect a few of these files (e.g. dofoo.vim dobar.vim) in a directory and then :source {filename} the one you need.
    – roblogic
    Mar 4, 2015 at 3:03

One easy way to save any kind of action is to record it as a macro. These are stored in the named registers, "a to "z, and with the default 'viminfo' setting, these registers will be saved to the .viminfo file and thus persisted across Vim restarts. (See :h 'viminfo'.)

So for example, the following series of keystrokes would store your first example substitution command in the "a register:


(where <CR> designates a press of the Return key.)

You can then replay this macro by pressing @a from Normal mode.

You could of course store your example entire sequence of substitutions in a single macro if they are always to be run in succession.

So long as your 'viminfo' is set up to save registers, then this macro will persist until you (or a poorly written script) overwrites this register, so although technically your commands could be saved forever, this method is better suited for storing things for a finite and reasonably short-lived period of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.