bgenchel
bgenchel

Reputation: 4059

How do you run a vimscript on an open buffer in VIM

I have a series of find and replace operations that I'd like to perform on many different files. Instead of running them by hand, I'd like to put them in a script and run them. I've done this, placing all my search and replace commands in series in a file, let's call it instructions.vim

Now, I would like to be able to run this script inside vim on the buffer I am currently working on. How do I do that? Google yields things like autocmd, which attaches the script run (I think) to a keyboard shortcut. I don't want to do that. I just want to run my script. But there doesn't seem to be any useful information online to help with this.

Any tips? Thanks!

Upvotes: 0

Views: 797

Answers (2)

gregory
gregory

Reputation: 12973

First record your series of substitutions as a macro (e.g. save macro as a). Then load all the files you wish to change into the argo list (use :arg command which supports wildcards, e.g.: **/*.txt). Finally, apply the macro to the files, making sure to write the file after the change:

:argdo normal @a | w

If you already have the commands in a saved file with the format of one command per line (e.g: :%s/x/y/g) then you can use argdo in conjunction with source:

:argdo source file | w

I prefer to test my changes on a sample file and then save those changes as a macro. A workflow like this provides a refining step before committing a broad sweeping change.

Upvotes: 1

Ziyadsk
Ziyadsk

Reputation: 489

You just read the script with an ex command :

source *file_name* 

Upvotes: 2

Related Questions