Wednesday, August 29, 2012

Re: Autocmds

On Wednesday, August 29, 2012 6:52:04 AM UTC-5, Jan wrote:
> On Tuesday, 28 August, 2012 at 04:12:26 BST, Ben Fritz wrote:
>
> >> When I try this:
>
> >>
>
> >> autocmd BufRead * if &ft == 'perl' | setlocal makeprg=perl\ -cw\ % | endif
>
> >>
>
> >> makeprg doesn't get set, I guess because vim sets the filetype to "perl" only after BufRead has happened (the :help doesn't say). The :help does hint at using BufWinEnter instead, which works for this case, but it seems suboptimal having these autocmd commands executed every time I display the buffer, when really I only need them executed the once, after buffer read/load.
>
> >
>
> >The event you want for the loading of the file is FileType. As mentioned before.
>
>
>
> But FileType autocmds get executed at every display of the buffer, not just when loading the file.
>
>

This didn't sound right, but I did an experiment, and by default it is quite true. In my setup, actually, it is also true.

But if you set the 'hidden' option, it is no longer true. FileType only fires when actually reading the file with 'hidden' set, for me at least.

Looking again at :help 'hidden', I see that when it is not set, the buffer is "unloaded" when it is abandoned. Unloading a buffer frees the text, so the next time you edit the buffer Vim must re-read the file, therefore re-triggering all the associated autocmds.

I don't have 'hidden' set because I am forgetful and don't like risking that I might do a bunch of work, go to compile, and realize I forgot to save a header file or something. But I know a LOT of people think 'hidden' is useful enough that it ought to be the default. I guess in this situation Vim behaves somewhat surprisingly without 'hidden' set. At this point I wonder how many of my autocmds would break were I to set 'hidden'...

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

No comments: