Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure vim so that when I send the process a USR1 signal it saves and quits (as opposed to creating the .swp recovery file)

Tags:

vim

unix

Here's my situation. I have a bunch of vim processes open. Rather than go through one by one and save/quite (:x!) I'd like to send all of the processes a signal - say USR1 - and instead of having it create a recovery .swp file, I'd like it to save the file and exit normally.

Possible?

like image 616
Philip Hallstrom Avatar asked May 03 '10 17:05

Philip Hallstrom


2 Answers

It's not quite as simple as just sending every vim process a signal, but you can get pretty close to what you want using vim's client/server features:

:help remote.txt

(You'll need a version of vim that's been compiled with the +clientserver option.)

Here's an example from the vim help:

vim --servername BLA --remote-send '<C-\><C-N>:wqa<CR>'    

That sends commands to the vim instance running as remote server "BLA", telling it to save all files and exit.

Since you're running multiple vim instances, you'll probably want to use the --serverlist option to get a list of all registered server instances, which you can then iterate over to send the save-and-exit command to each one.

like image 93
Bill Odom Avatar answered Oct 05 '22 00:10

Bill Odom


I've never tried to handle signals in vim. I don't know if it can be done. Here is one possible alternative:

Option: Use just one vim process

Perhaps instead of opening a ton of vim processes, you can open a bunch of tabs in gvim (or vim). That way, when you want to quit you can do :wqa (write and quit all).

It is easier to open tabs in vim if you do the following in your vimrc file:

:map <C-Insert> :tabnew<C-M>
:map <C-Delete> :tabclose<C-M>
:map <C-PgUp> :tabprev<C-M>
:map <C-PgDown> :tabnext<C-M>

The bindings above will allow Ctrl-insert and Ctrl-delete to open and close tabs, and Ctrl-pgup/pgdown will move between tabs much like firefox.

If you wanted those bindings to work in insert mode as well, you could do something like this in your vimrc file

:imap <C-Insert> <C-o>:tabnew<C-M>
:imap <C-Delete> <C-o>:tabclose<C-M>
:imap <C-PgUp> <C-o>:tabprev<C-M>
:imap <C-PgDown> <C-o>:tabnext<C-M>
like image 36
Kimball Robinson Avatar answered Oct 04 '22 22:10

Kimball Robinson