Search code examples
vim

Vim suspend/resume startup/shutdown hooks


Is it possible to execute a shell command when I suspend and resume vim? I guess that I can do something once <C-z> is pressed, so this can be considered us a suspend hook. But what about resume (with fg)? And also is it possible to do some work on vim startup and shutdown?

Maybe it's possible to establish handlers for vim's SIGSTOP and SIGCONT signals?


Solution

  • As the help on autocommand-events (suggested by @romainl) says, you can use VimEnter and VimLeave to execute a command when Vim launches or shuts down:

    autocmd VimEnter * call EnterFunc()
    autocmd VimLeave * call LeaveFunc()
    

    The above code will execute a function EnterFunc when launching Vim, and LeaveFunc when exiting Vim.

    To execute a function when vim is suspended or resumed, you can use VimSuspend or VimResume:

    autocmd VimSuspend * call SuspendFunc()
    autocmd VimResume * call ResumeFunc()
    

    However, you should know that VimSuspend and VimResume are available only from Vim 8.2. If you're using Ubuntu you can add this PPA:

    sudo add-apt-repository ppa:jonathonf/vim
    

    and then:

    sudo apt update
    sudo apt install vim
    

    to update vim to a version that supports VimSuspend and VimResume. This is outlined here