Using bash is it possible to open a file in vim and then automatically save and close e.g.

"ZZ" | vim my.file.txt

obviously the above isn't going to work, but how would I do the equivalent? - I know there doesn't seem to be a use case for this - but I have one.

  • What is the use case? Do you just want to trigger particular autocmds?
    – chepner
    Commented Jul 3, 2012 at 19:20
  • I just want to save and close as soon as I open the file
    – Lizard
    Commented Jul 3, 2012 at 19:23
  • 3
    Then not opening the file in the first place would be exactly equivalent.
    – chepner
    Commented Jul 3, 2012 at 19:31
  • in most cases yes - but what if you have a hook with git that opens the commit message in vim and pulls in external info? - When i run my command i want it so pull this extra info and then save and close the vim file automatically
    – Lizard
    Commented Jul 3, 2012 at 19:33
  • 1
    You could do git commit -m '<put your extra info here>'. But your use case is different from your question: "opening a file, inserting some stuff, saving and closing it" is not the same as "opening a file, saving and closing it". If you are not clear you are missing opportunities to get valuable answers.
    – romainl
    Commented Jul 4, 2012 at 5:54

2 Answers 2


See ":help -c". For example:

vim -c "wqa" my.file.txt

The following works:

echo :wq | vim my.file.txt

You must log in to answer this question.

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