Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

See ":help -c". For example:

vim -c "wqa" my.file.txt
share|improve this answer
up vote 3 down vote accepted

The following works:

echo :wq | vim my.file.txt
share|improve this answer

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.