I'm writing a script file in which I have to close the vim files that are opened. So, how can I close or exit from the file through the shell script? I've tried this solution, but had no luck. I've tried it this way:
vim path_to_file/abc +qa
In order to close an existing, running Vim from an external shell script, you either have to tell that Vim instance to quit, or let the operating system do the job.
Vim has client-server functionality built in, see :help remote.txt
. If you know the server name (you can get the list with vim --serverlist
), you can send it commands. E.g. this tells the Vim instance named GVIM
to quit:
$ vim --servername GVIM --remote-send '<C-\><C-N>:quitall!<CR>'
Brute force: kill all running Vim instances :-)
$ killall vim
To find the Vim instance which has a particular file open, the lsof
tool can be used. Because Vim only opens the file itself on :write
s, we have to search for its (permanently open) swap file (i.e. .file.swp
instead of file
):
$ kill `lsof -t /path/to/.file.swp`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With