How do I automate a set of VIM commands that I use all the time?

1

I'm running some text files written in Markdown through Pandoc to get html, but it renders the tags strangely with weird line breaks and white space in tags, so I've been running a few sed commands in VIM.

Should I be looking at Vimscripts or at macros? Or a shellscript (if I want to combine the VIM commands with the shell Pandoc command)?

I'm running gVIM on Windows 7, and I'm running Pandoc through the regular Windows cmd shell, though I usually do it from inside VIM.

Joe

Posted 2011-04-04T18:30:09.590

Reputation: 23

Why not use sed to run the sed commands? – Ignacio Vazquez-Abrams – 2011-04-04T18:48:27.057

That's what I'm doing, but I have to run a few of them, and I have to do it every time I convert a file. I'd like to be able to automate the conversion+several sed commands all at once, preferably from within VIM. – Joe – 2011-04-04T18:58:47.590

Answers

1

I think it depends on where most of the file processing and decision making is being done and on the scripting facilities available to you outside of Vim. Since most of the processing you're doing seems to be running a sequence of programs, perhaps in a pipeline, I'd be inclined to put all that into a shell script or a batch file and launch that from Vim. (You wrote that you use sed. Do you have access to a Unix shell as well?) If you don't have access to a Unix shell and you find yourself limited by the batch file command set, then you could put all the commands into a Vim function. Putting the commands into a Vim function gives you a little more flexibility and easier programming than trying to quote everything just right for a Vim command or mapping.

You can put shell commands inside functions pretty easily like this

function Foo()
    !echo "hello"
endfunction

or this:

function Bar()
    call system('echo "hello"')
endfunction

See

:help usr_41.txt

and especially

:help 41.6
:help 41.7

as well as

:help system()

for more on this. For more on commands and mappings, see

:help usr_40.txt

You can write a command to call your function like this:

:command Foo call Foo()

so that you can execute your function with

:Foo

garyjohn

Posted 2011-04-04T18:30:09.590

Reputation: 29 085

0

ed is pretty easy to script with.

printf "%s\n" "%s/$/...end/" "g/delete.this.line/d" w q | ed filename

glenn jackman

Posted 2011-04-04T18:30:09.590

Reputation: 18 546