How to execute commands at vim exiting?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to execute commands at vim exiting?

Joan Miquel Torres Rigo
Hi,

Does anybody know if there is some way to execute commands at vim exiting?


I have the following in my ~/.vimrc to make F-12 key switch between
"auto-created" screen session associated to current buffer file name
(future file directory, but I must deal with path slashes before):


-8<------------------------------------------------------
" F12 -> Per file Screen session: {{{
" ==============================
" cd $(dirname %:p) --> Change to current file's directory.
" screen
"       -U      -> Run in UTF-8 mode.
"       -d      -> Deatach if previously attached.
"       -dRR vim_$(basename %:p)
"               -> Use current buffer file name as sessionname; then
reattach it and, if necessary, detach or create it first. Use the
first session if more than one session with same name are available.
"       -e^qa   -> Change default screen's escape key to CTRL-Q to
avoid conflict if vim itself is running in other screen session.
"       -p %:p  -> Use current buffer file full path as preselected
(if available) window.
"               * TODO: Create this window by default when not exists.
noremap <f12> :silent<space>!bash<space>-c<space>'cd<space>$(dirname<space>%:p);screen<space>-UdRR<space>vim_$(basename<space>%:p)<space>-e^qa<space>-p<space>"%:p"<space>-c<space>~/.vim/screenrc'<enter>:redraw!<enter>

" Prepare vim's screen configuration:
" ----------------------------------
" Create ~/.vim directory if not already exists.
silent !mkdir -p ~/.vim
" Copy user's ~/.screenrc if exists.
silent !cp ~/.screenrc ~/.vim/screenrc 2>/dev/null
" Bind F12 to 'detach' command in screen.
silent !echo 'bindkey -k F2 detach' >> ~/.vim/screenrc
" }}}
-8<------------------------------------------------------


This works much fine, but it's tremendously easy to forgive many
screen sessions open when exiting.

The main idea is to capture vim pid (any suggestion about it also will
be gratefull) an use it in the building of the screen's label.

This way I could capture all screens generated in the same vim process
and kill it on exiting.


I thought that I can redirect vim command to some script to invoke vim
and do that after it's execution, but I think there must be better
solution...


Regards.

--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «How to execute commands at vim exiting?»,
присланное в 17:09:18 02 октября 2010, Суббота.
Отправитель: Joan Miquel Torres Rigo:

To execute something just before vim exists, you can use `VimLeave` autocommand,
like that:
    autocmd VimLeave * KillAllScreens
(you must define KillAllScreens autocommand by yourself). About capturing vim's
pid: use ``let g:vimpid=system('echo -n $PPID')+0''

Текст сообщения:

> Hi,
>
> Does anybody know if there is some way to execute commands at vim exiting?
>
>
> I have the following in my ~/.vimrc to make F-12 key switch between
> "auto-created" screen session associated to current buffer file name
> (future file directory, but I must deal with path slashes before):
>
>
> -8<------------------------------------------------------
> " F12 -> Per file Screen session: {{{
> " ==============================
> " cd $(dirname %:p) --> Change to current file's directory.
> " screen
> "       -U      -> Run in UTF-8 mode.
> "       -d      -> Deatach if previously attached.
> "       -dRR vim_$(basename %:p)
> "               -> Use current buffer file name as sessionname; then
> reattach it and, if necessary, detach or create it first. Use the
> first session if more than one session with same name are available.
> "       -e^qa   -> Change default screen's escape key to CTRL-Q to
> avoid conflict if vim itself is running in other screen session.
> "       -p %:p  -> Use current buffer file full path as preselected
> (if available) window.
> "               * TODO: Create this window by default when not exists.
> noremap <f12>
> :silent<space>!bash<space>-c<space>'cd<space>$(dirname<space>%:p);screen<s
> pace>-UdRR<space>vim_$(basename<space>%:p)<space>-e^qa<space>-p<space>"%:p"
> <space>-c<space>~/.vim/screenrc'<enter>:redraw!<enter>
>
> " Prepare vim's screen configuration:
> " ----------------------------------
> " Create ~/.vim directory if not already exists.
> silent !mkdir -p ~/.vim
> " Copy user's ~/.screenrc if exists.
> silent !cp ~/.screenrc ~/.vim/screenrc 2>/dev/null
> " Bind F12 to 'detach' command in screen.
> silent !echo 'bindkey -k F2 detach' >> ~/.vim/screenrc
> " }}}
> -8<------------------------------------------------------
>
>
> This works much fine, but it's tremendously easy to forgive many
> screen sessions open when exiting.
>
> The main idea is to capture vim pid (any suggestion about it also will
> be gratefull) an use it in the building of the screen's label.
>
> This way I could capture all screens generated in the same vim process
> and kill it on exiting.
>
>
> I thought that I can redirect vim command to some script to invoke vim
> and do that after it's execution, but I think there must be better
> solution...
>
>
> Regards.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Joan Miquel Torres Rigo
2010/10/2 ZyX <[hidden email]>:

> Ответ на сообщение <<How to execute commands at vim exiting?>>,
> присланное в 17:09:18 02 октября 2010, Суббота.
> Отправитель: Joan Miquel Torres Rigo:
>
> To execute something just before vim exists, you can use `VimLeave` autocommand,
> like that:
>    autocmd VimLeave * KillAllScreens
> (you must define KillAllScreens autocommand by yourself). About capturing vim's
> pid: use ``let g:vimpid=system('echo -n $PPID')+0''
>

Thank you a lot.

I'm still a newbie in vim scripting and I must to end up reading
autocommands documentation but seems it will do just that I need.

Note: If I understood well, the command you given will run
KillAllScreens once per each buffer but, of course, this is trivial to
solve by using some flag variable.

Regards.

--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «Re: How to execute commands at vim exiting?»,
присланное в 12:27:51 05 октября 2010, Вторник.
Отправитель: Joan Miquel Torres Rigo:

> Note: If I understood well, the command you given will run
> KillAllScreens once per each buffer but, of course, this is trivial to
> solve by using some flag variable.

From :help VimLeave:
> Before exiting Vim, just after writing the .viminfo file.  Executed only once,
> like VimLeavePre.  To detect an abnormal exit use v:dying.  When v:dying is 2
> or more this event is not triggered.
Note the second sentence.

Текст сообщения:

> 2010/10/2 ZyX <[hidden email]>:
> > Ответ на сообщение <<How to execute commands at vim exiting?>>,
> > присланное в 17:09:18 02 октября 2010, Суббота.
> > Отправитель: Joan Miquel Torres Rigo:
> >
> > To execute something just before vim exists, you can use `VimLeave`
> > autocommand,
> >
> > like that:
> >    autocmd VimLeave * KillAllScreens
> >
> > (you must define KillAllScreens autocommand by yourself). About capturing
> > vim's pid: use ``let g:vimpid=system('echo -n $PPID')+0''
>
> Thank you a lot.
>
> I'm still a newbie in vim scripting and I must to end up reading
> autocommands documentation but seems it will do just that I need.
>
> Note: If I understood well, the command you given will run
> KillAllScreens once per each buffer but, of course, this is trivial to
> solve by using some flag variable.
>
> Regards.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Joan Miquel Torres Rigo
2010/10/5 ZyX <[hidden email]>:
>
> From :help VimLeave:
>> Before exiting Vim, just after writing the .viminfo file.  Executed only once,
>> like VimLeavePre.  To detect an abnormal exit use v:dying.  When v:dying is 2
>> or more this event is not triggered.
> Note the second sentence.

True.

I got confused by the syntax specification with the '*' pattern.

Thank you again.

--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Joan Miquel Torres Rigo
In reply to this post by Nikolay Aleksandrovich Pavlov
2010/10/2 ZyX <[hidden email]>:
>
> To execute something just before vim exists, you can use `VimLeave` autocommand,
[...]
> About capturing vim's
> pid: use ``let g:vimpid=system('echo -n $PPID')+0''

Thanks again for your help.

I got it working, but I had a new idea:

I added a dialog call to ask about if existing attached screen must be
killed or preserved.

If dialog is installed (if not it unconditionally kills screen) and
user select to not kill it, then screen session is preserved but, of
course, vim exits anyway.

This is not a big problem, but it will become perfect if we could
abort to exiting vim in this case.


But I think this time will not be possible because of the autocmd
nature. Am I wrong?


PD: Current related ~/.vimrc is that comes below:

-8<------------------------------------------------------
" F12 -> Attached Screen session: {{{
" ==============================

" Build mapping: {{{
" cd $(dirname %:p) --> Change to current file's directory.
" screen
"       -U      -> Run in UTF-8 mode.
"       -d      -> Deatach if previously attached.
"       -dRR vim_$(basename %:p)
"               -> Use current buffer file name as sessionname; then
reattach it and, if necessary, detach or create it first. Use the
first session if more than one session with same name are available.
"       -p %:p  -> Use current buffer file full path as preselected
(if available) window.
"               * TODO: Create this window by default when not exists.
"       -c ~/.vim/screenrc
"               -> Use modified screenrc config file (see below).
noremap <f12> :silent<space>!bash<space>-c<space>'cd<space>$(dirname<space>%:p);screen<space>-UdRR<space>vim_${PPID}<space>-p<space>"%"<space>-c<space>~/.vim/screenrc'<enter>:redraw!<enter>
" }}}

" Make screen session to be killed on exit and also prompting if
dialog is installed: {{{
" ----------------------------------------------------------------------------------
autocmd VimLeave * !screen -ls vim_${PPID} | grep vim_${PPID} &&
dialog --yesno "Leave existing screen sessions alive?" 6 40 || kill
$(screen -ls vim_${PPID} | perl -ne 's/^.*?(\d{2,}).*$/\1/&&print') &&
clear
" }}}

" Prepare vim's screen configuration: {{{
" ----------------------------------
" Create ~/.vim directory if not already exists.
silent !mkdir -p ~/.vim
" Create/overwrite with user's ~/.screenrc if exists.
silent !cat ~/.screenrc > ~/.vim/screenrc 2>/dev/null
" Bind F12 to 'detach' command in screen.
silent !echo 'bindkey -k F2 detach' >> ~/.vim/screenrc
" Change default screen's escape key to CTRL-Q to avoid conflict if
vim itself is running in other screen session.
silent !echo 'escape ^qa' >> ~/.vim/screenrc
" }}}

" }}}
-8<------------------------------------------------------



--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: How to execute commands at vim exiting?

Joan Miquel Torres Rigo
A bit better solution:

autocmd VimLeave * !screen -ls vim_${PPID} | grep vim_${PPID} &&
dialog --yesno "Leave existing screen sessions alive?" 6 40 && screen
-r vim_${PPID} || kill $(screen -ls vim_${PPID} | perl -ne
's/^.*?(\d{2,}).*$/\1/&&print') && clear


...this way vim exits anyway but, at least, attach the screen helping
user to propperly close windows and running proceses.

2010/10/10 Joan Miquel Torres Rigo <[hidden email]>:

> 2010/10/2 ZyX <[hidden email]>:
>>
>> To execute something just before vim exists, you can use `VimLeave` autocommand,
> [...]
>> About capturing vim's
>> pid: use ``let g:vimpid=system('echo -n $PPID')+0''
>
> Thanks again for your help.
>
> I got it working, but I had a new idea:
>
> I added a dialog call to ask about if existing attached screen must be
> killed or preserved.
>
> If dialog is installed (if not it unconditionally kills screen) and
> user select to not kill it, then screen session is preserved but, of
> course, vim exits anyway.
>
> This is not a big problem, but it will become perfect if we could
> abort to exiting vim in this case.
>
>
> But I think this time will not be possible because of the autocmd
> nature. Am I wrong?
>
>
> PD: Current related ~/.vimrc is that comes below:
>
> -8<------------------------------------------------------
> " F12 -> Attached Screen session: {{{
> " ==============================
>
> " Build mapping: {{{
> " cd $(dirname %:p) --> Change to current file's directory.
> " screen
> "       -U      -> Run in UTF-8 mode.
> "       -d      -> Deatach if previously attached.
> "       -dRR vim_$(basename %:p)
> "               -> Use current buffer file name as sessionname; then
> reattach it and, if necessary, detach or create it first. Use the
> first session if more than one session with same name are available.
> "       -p %:p  -> Use current buffer file full path as preselected
> (if available) window.
> "               * TODO: Create this window by default when not exists.
> "       -c ~/.vim/screenrc
> "               -> Use modified screenrc config file (see below).
> noremap <f12> :silent<space>!bash<space>-c<space>'cd<space>$(dirname<space>%:p);screen<space>-UdRR<space>vim_${PPID}<space>-p<space>"%"<space>-c<space>~/.vim/screenrc'<enter>:redraw!<enter>
> " }}}
>
> " Make screen session to be killed on exit and also prompting if
> dialog is installed: {{{
> " ----------------------------------------------------------------------------------
> autocmd VimLeave * !screen -ls vim_${PPID} | grep vim_${PPID} &&
> dialog --yesno "Leave existing screen sessions alive?" 6 40 || kill
> $(screen -ls vim_${PPID} | perl -ne 's/^.*?(\d{2,}).*$/\1/&&print') &&
> clear
> " }}}
>
> " Prepare vim's screen configuration: {{{
> " ----------------------------------
> " Create ~/.vim directory if not already exists.
> silent !mkdir -p ~/.vim
> " Create/overwrite with user's ~/.screenrc if exists.
> silent !cat ~/.screenrc > ~/.vim/screenrc 2>/dev/null
> " Bind F12 to 'detach' command in screen.
> silent !echo 'bindkey -k F2 detach' >> ~/.vim/screenrc
> " Change default screen's escape key to CTRL-Q to avoid conflict if
> vim itself is running in other screen session.
> silent !echo 'escape ^qa' >> ~/.vim/screenrc
> " }}}
>
> " }}}
> -8<------------------------------------------------------
>
>
>
> --
> Joan Miquel Torres__________________________________
> Linux Registered User #164872
> http://www.mallorcaweb.net/joanmiquel
> BULMA: http://bulma.net http://breu.bulma.net/?l2301
>



--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php