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
    Jul 3 '12 at 19:20
  • I just want to save and close as soon as I open the file
    – Lizard
    Jul 3 '12 at 19:23
  • 3
    Then not opening the file in the first place would be exactly equivalent.
    – chepner
    Jul 3 '12 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
    Jul 3 '12 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
    Jul 4 '12 at 5:54

See ":help -c". For example:

vim -c "wqa" my.file.txt

The following works:

echo :wq | vim my.file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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