Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exit or close the vim through shell script

Tags:

linux

vim

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
like image 399
user2622247 Avatar asked Oct 16 '25 15:10

user2622247


1 Answers

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.

communication with Vim

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>'

through operating system

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 :writes, we have to search for its (permanently open) swap file (i.e. .file.swp instead of file):

$ kill `lsof -t /path/to/.file.swp`
like image 137
Ingo Karkat Avatar answered Oct 18 '25 09:10

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!