1

I've been using the command mode for making replacements in a file. Something like :%s/foo/bar/g and to make a series of replacements:

:%s/i/1/g | :%s/j/2/g | :%s/k/3/g | :%s/l/4/g | ...

This line exists in the command history but I was curious if these commands can be pulled from a file with contents like:

%s/i/1/g
%s/j/2/g
%s/k/3/g
%s/l/4/g
.
.
.

If this is a possibility, it will be easy to have several replacement combinations in different files. Thanks for your thoughts and comments.

3

Yes. You can store the list of Ex commands to a file and then use the :source command to execute the commands in the file.

By convention, you would save the commands to a file using the *.vim extension. That will also get Vim to help you with proper syntax highlighting, automatic indentation, etc. for the vim filetype.

If you store the file under the ~/.vim directory (or some other location listed in 'runtimepath'), then you can also use the :runtime command to execute it. (It's essentially the same as :source, but you don't need to specify the full path to your runtime directory.)

If you currently have the list of commands you want to save into a script in the command history, you might want to use the q: normal mode command to open a window with the command-line history, so you can copy the commands from there and paste them into a buffer where you can edit, rearrange them and save them into a *.vim file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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