Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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
up vote 3 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

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


    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

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

To execute vim in non-interactive mode, you can use either +{command}/-c {command} or -s parameter which will allow you to execute the vim commands after the first file has been read.

Check few examples below.

Simple standard input and output using pipes can be achieved by the command:

$ echo Example | vim -es '+:wq! /dev/stdout' /dev/stdin | cat

To edit one file and save the changes to another:

$ vim -es '+:0,$s/127/128/g' '+:wq! new_file' /etc/hosts
$ cat file1 file2 | vim - -es '+:0,$s/foo/test/g' '+:wq! file3'

To edit file in-place, you can use -s {scriptin} so the script file is read, in example:

$ echo ':0,$s/foo/test/ge' > cmds.txt
$ echo ':wq' >> cmds.txt
$ vim -s cmds.txt -es file

To parse file and send it to pipeline's output, you may try the following:

$ cat /etc/hosts | vim -es '+:0,$s/127/128/g' '+%print' '+:q!' /dev/stdin | cat


$ cat /etc/hosts | vim -es '+:0,$s/127/128/g' '+:wq! /dev/stdout' /dev/stdin | cat

See more: How to edit files non-interactively (e.g. in pipeline)? at Vim SE

share|improve this answer

Your Answer


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.