I have a number of multi million line sets of data to edit before using a program to analyse them.

I have a set of regexp (23 of them) which do the work. Things like :%s/\n\n/\r to get rid of multiple blank lines

How can I turn this set of commands into a script?

2 Answers 2


You can add the following lines to your vimrc:

function! MyRegex()
    execute '%s/\n\n/\r'
    " Add other regexes here

And you can then open your buffer and use :call MyRegex() to apply your regexes.

execute allows you to execute a command from a script (See :h :execute)

You might also want to have a look at :h substitute() which you can use from a script too.

  • 1
    Oh I did not realized that you was using vi specifically... I'm not sure how vi handle this actually, you'll probably need the answer from someone else. (Also that's not an answer but if you have the choice of of using another editor you really should switch to vim it will ease your life a lot)
    – statox
    Jan 18, 2017 at 13:36
  • 2
    @nerak99 is it truly vi? Many systems have it linked to vim or another clone.
    – Herb
    Jan 18, 2017 at 18:09
  • 2
    If you are using a mac and an up to date linux there is a huge chance for vi to be an alias of vim. To check use :version in your editor and you'll see which software you are using. Also if you have the choice don't hesitate to switch to vim it has a lot more of features and is much easier to use. (But I really think you already use vim without knowing it)
    – statox
    Jan 20, 2017 at 14:50
  • 1
    It's probably Vim. Vi doesn't split a line with \r in the replacement text. It needs a ^M.
    – Antony
    Jan 20, 2017 at 18:24
  • 1
    I used this as a solution. M. Brabandt is correct in that it answers to vi but is actually vim. There was no .vimrc but I just made one and it worked OK.
    – nerak99
    Jan 23, 2017 at 12:33

Sed stands for stream editor and it can execute regexes over files, line by line. A sed script would probably serve you better than trying to do something in vi, if vim is not an option.

Just put your regex lines (without the :% which is specific to vi) in a file, call it myscript:


Then run that file as a sed script over your target files like so:

sed -n -f myscript myfiles/*.txt

Statox's answer works better if you can use vim. If you're stuck with vi then try this.

  • I tried sed first and found that the regex substitutes that worked in Vim needed adjusting to work in sed. This might have lain in issues with \(<...>\) or escaping rules. I did not manage to work it out but making a script in .vimrc worked like a charm
    – nerak99
    Jan 30, 2017 at 9:40
  • It was probably more related to the fact that vim's regex engine is slightly different than "standard" regular expressions. The vimhelp page on pattern-matching doesn't really point this out directly, but I have noticed some differences to be sure.
    – Tim
    Feb 1, 2017 at 17:30

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.