I'm trying to get confident with autocommands, but <<the
":normal" command is a bit tricky.>>
Now, I'd like to have an au for emails that removes the remaining
quoted text in my reply (I'm using Mutt as MUA). Suppose that I
always close an email with "Best regards", so that a typical
response with remaining quoted text is (sorry for the ">"s):
** Begin example **
Best regards
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec
> iaculis tincidunt massa. Nullam non massa ut dui pulvinar
> tempus. Mauris volutpat.
** End example **
Inside a normal vim instance if I do ":.,$s/^>.*\n//" the text
with the ">"s is removed and so the lines. Anyway, if I use an
au like:
autocmd BufWrite execute "normal :silent! .,$s/^>.*\n//"
there are 2 problems:
1) it's triggered every time the buffer is written;
2) even if it deletes the text, the lines are not removed.
As for 1), I don't really know how to tell Vim that I only want
the au to be applied just before I *close* the file/editor -- I've
used all kind of autocmd-events without success. As for 2), I just
don't understand why it's behaving differently.
TIA, any help would be appreciated.
chr
--
SDF Public Access UNIX System - http://sdf.lonestar.org
Try using the BufWinLeave event, which is triggered when a buffer stops
being displayed in a window, e.g. when you ":q" the last split-window
containing it, or when you quit Vim.
If you want a modified filme to be automatically written when you
|abandon| it, set the 'autowriteall' option.
So I suggest the following (untested), if you are sure you always want
to delete from the cursor line to the end of the file, but only for
filetype "mail" and only for lines starting in a greater-than sign:
in your .vimrc or _vimrc:
set awa
in ~/.vim/after/ftplugin/mail.vim (on Unix) or
~/vimfiles/after/ftplugin/mail/mail.vim (on Windows) (create the
directories and file if they don't yet exist):
au BufWinLeave <buffer> .,$g/^>/d
Note that for an ex-command, no "normal" prefix is necessary. Also, the
above assumes Vim 7 "or later", with, of course, +autocmd compiled-in.
Best regards,
Tony.
--
Politicians are the same all over. They promise to build a bridge even
where there is no river.
-- Nikita Khrushchev
That wasn't correct. I was using (*uncorrectly*):
autocmd BufWrite *.txt execute "normal :silent! .,$s/^>.*\n//"
(with the *.txt because I was testing on txt files.)
> > there are 2 problems:
> > 1) it's triggered every time the buffer is written;
> > 2) even if it deletes the text, the lines are not removed.
> >
> > As for 1), I don't really know how to tell Vim that I only want
> > the au to be applied just before I *close* the file/editor -- I've
> > used all kind of autocmd-events without success. As for 2), I just
> > don't understand why it's behaving differently.
[...]
> Try using the BufWinLeave event
[...]
Yes Tony, I tried also with that event. I think I've tried all the
relevant events.
[...]
> So I suggest the following (untested), if you are sure you always want
> to delete from the cursor line to the end of the file, but only for
> filetype "mail" and only for lines starting in a greater-than sign:
>
> in your .vimrc or _vimrc:
> set awa
>
> in ~/.vim/after/ftplugin/mail.vim (on Unix) or
> ~/vimfiles/after/ftplugin/mail/mail.vim (on Windows) (create the
> directories and file if they don't yet exist):
> au BufWinLeave <buffer> .,$g/^>/d
Tried all your suggestions (in console vim and Gvim, 7.1, and *nix
and win (BTW, I think the pattern for the win ftplugin should be
~/vimfiles/after/ftplugin/mail.vim, without a mail/)): no success.
> Note that for an ex-command, no "normal" prefix is necessary. Also, the
> above assumes Vim 7 "or later", with, of course, +autocmd compiled-in.
[...]
Yes, that's simply correct.
Well, maybe I'm missing something or Vim is not interpreting the
event. An indication of the latter hypothesis is that, if I change
the "BufWinLeave" (or other similar event) with "BufWrite" or
"BufWinEnter" Vim actually does a :.,$g/^>/d
Thanks for your help, Tony.
Best regards.
The execution order of autocommands is
BufWrite -> BufWritePost -> BufLeave -> BufWinLeave
Last chance of changing the text before writing is BufWrite, thus
editing commands given with BufWinLeave are ignored.
I think you cannot do what you want with autocommands.
Why not just create a custom command or mapping for the purpose?
--
Andy
Mmh, probably the fact that editing just before leaving a buffer
with an autocmd is not possible is documented somewhere, but
(from :h BufWinLeave):
"Before a buffer is removed from a window. Not when it's still
visible in another window. Also triggered when exiting. It's
triggered before BufUnload or BufHidden."
seems to indicate that it's actually possible.
> Why not just create a custom command or mapping for the purpose?
Ok, Thanks. I accept your advice. Now, I've added the following
map in my ~/.vim/after/ftplugin/mail.vim
cmap wq :.,$g/^>/d<CR>:x
that does what I was expecting from the autocmd.
Try to append a write to the autocmd, something like:
autocmd BufWinLeave <buffer> .,$g/^>/d | write
Regards,
Ag.
At least this should be (splitted in) two autocmds or :g enclosed with "exec"
autocmd BufWinLeave <buffer> exec '.,$g/^>/d' | write
It seems to work.
> Regards,
> Ag.
--
Andy
Thanks Ag. and Andy. Both actually work, but with the one without
the "exec" the window is filled with lines like:
"tests.txt" 44L, 1824C written
Anyway, I have now 2 solutions for what was trying to do:
autocmd BufWinLeave <buffer> exec '.,$g/^>/d' | write
cmap wq :.,$g/^>/d<CR>:x
Best regards.