Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a plain text file with entries as

firstAuthorLastName_titleFirstWord_YYYY

that I change to

firstAuthorLastNameYYYYtitleFirstWord

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

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

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

1 Answer 1

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 :-(

Alternatives

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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