Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a plain text file with entries as


that I change to


with the following s// command in Vim.^[1]


But I want to do this command over and over again. It seems that I should be able to do this with a Windows batch script and the -c flag to gvim.exe. But I can't get it to work.

I have a Windows batch script with the following.

gvim -c "%s/\([a-z]\+\)_\([a-z]\+\)_\([0-9]\{4}\)/\1\3\2/g" herron.bib

But I get the message E486: Pattern not found: \([a-z]\+\)_\([a-z]\+\)_\([0-9]\{4}\). Is there a way to reuse/automate this s// command? I know that I can up arrow through my ex command history, but I'd like something a little more permanent and reliable.

[^1] FWIW, this is a BibTeX .bib file that I export from Zotero. Further downstream I use vim-pandoc to write notes in markdown. The underscores make it a little more difficult to use the vim-pandoc "tidy" function, so I remove them. I update the .bib several times a week and re-export it from Zotero, so I also need to run this regex several times a week.

share|improve this question
up vote 4 down vote accepted

I don't see anything wrong, and apparently the pattern is correctly passed to Vim, so it should work :-(


Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

That said, you can use Vim non-interactively:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"

Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.

Vim alternatives

Based on your description, it's not entirely clear whether you want full automation, or just easier recall. If you don't mind applying the command manually (but quickly) from within Vim, how about:

a) a custom command

:command! TidyNames %s/\([a-z]\+\)_\([a-z]\+\)_\([0-9]\{4}\)/\1\3\2/g

b) a custom mapping

:nnoremap <Leader>tn :%s/\([a-z]\+\)_\([a-z]\+\)_\([0-9]\{4}\)/\1\3\2/g<CR>

Both of these can be placed into your ~/.vimrc, or (with <buffer> modifier) in a filetype-specific ~/.vim/after/ftplugin/tex.vim.

share|improve this answer
Thanks! command! did the trick! Thanks for the thorough lesson and I'll check out all of these. But you're right, "easier recall" is the best way to say this. Thanks! – Richard Herron Sep 24 '13 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .