I am writing a bash script (.sh file) to run a program called "lenstool". Before fully running as I type directly in the terminal, "lenstool" opens a text file in vim. I must quit vim by typing :q and hitting enter. Then the program would do what it is supposed to do. Now, I want run "lenstool" a few thousand times. How can I automatically exit vim within a bash script file?

  • I'm confused about the question. Is lenstool a program you wrote? Can you change it? – James Nov 2 '16 at 21:35
  • No, it is a package that I installed and I would like to learn how to exit programs in the terminal in general. – Milad P. Nov 2 '16 at 21:36
  • Can you change which editor is called by lenstool? Maybe it uses the EDITOR environment variable, so you can switch vim to something more "batch-friendly". Or maybe you can change the way vim gets called and use its ex mode. – Henning Kockerbeck Nov 3 '16 at 0:14
  • 1
    Another thought, which version of lenstool are you using? A quick search found at least two that seem to fit, one written in C and Perl and one written in Python. The latter one has an API. So maybe it would be easier to write your script in Python and use that API, instead of wrangling vim around ;) – Henning Kockerbeck Nov 3 '16 at 0:17

One solution that comes to my mind is doing:

id=$(ps -A | grep vim | awk '{print $1}')

to get pid(s) of the running vim program(s) and then kill it with:

kill $id

of course you need to save your work first and then do this. I'm not sure how your program saves what is has done!

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.