Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The Gnats bug tracker has a command (edit-pr) that opens a ticket in $EDITOR. My $EDITOR is VIM, and when this command opens the ticket in $EDITOR, I'd like to automatically perform some scripted edits without having to make any additional keystrokes.

Is there a way to write a VIM script that can be run non-interactively like this?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

I've never used gnats, so it's hard to say what exactly edit-pr is expecting to be done. But from what you've given us, you could try changing the EDITOR env variable to a custom shell script, or maybe even a python script or something.

EDITOR=/path/to/script.(sh|py) edit-pr <args>

But like I said, I don't know what kind of edits it wants, or what needs to be done in the editor. Maybe if you gave us an example of what you'd want to automate?

share|improve this answer
    
The gnats command was just an example. I was looking for a general solution and I think your answer is the way to go. Thanks! –  Matt Pfefferle Jun 1 '11 at 17:40
add comment

Vim commands can be specified on the command line when you run the program like this:

vim file.txt "+:s/foo/bar/g"

You can keep adding commands by adding "+[cmd]" arguments. You can either end there and the editor will open, run those commands and wait or you, or you can add a "+:wq" to save and close the file.

You could also write a script using vimscript syntax and either wrap it in a function to call when the file opens or in a separate script to import and apply to the buffer using the syntax above.

share|improve this answer
add comment

Even though you've already received an answer to your question, others might find this useful.

Vim commands can also be specified in a file which is executed using the -s option:

    vim -s commands.txt file

Example:

    echo 'gg=G' > indent.txt
    echo ':wq' >> indent.txt
    vim -s indent.txt file

This will use Vim to indent the contents of file, save and exit.

share|improve this answer
add comment

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.