Wednesday, August 24, 2011

Re: Autocommand event for new buffers including startup buffer

On 24/08/11 19:00, Taylor Hedberg wrote:
> I normally use `autocmd BufNewFile,BufRead * ‥` to execute a command
> in each new buffer as it is opened. However, these events are not
> triggered in the "No Name" buffer that is created when Vim is launched
> without arguments.
>
> In my .vimrc:
>
> autocmd BufNewFile,BufRead * let b:foo = 1
>
> If I launch Vim with `vim test.txt`, then b:foo is defined in the
> resulting buffer. If I launch with just `vim`, however, it is undefined
> in the "startup" buffer, but any subsequent buffers I create, say with
> `:e`, do have b:foo defined.
>
> Is there an autocommand event that applies to the default startup buffer
> as well as all subsequently created buffers? Using something like
> `BufNewFile,BufRead,VimEnter` doesn't work for me in this case, because
> VimEnter triggers too late to be useful for the result I'm trying to
> achieve. I need an event which occurs at the same time (or earlier) in
> the buffer creation process as the other two.
>

Which effect do you try to achieve exactly? BufWinEnter is triggered
when newly loading a buffer (including the first buffer, even if it is a
[No Name] buffer), or when "unhiding" (into a window) a buffer which was
loaded but hidden (and not even displayed in a different window), but
from your description I can't tell with certainty if it's early enough
for you. In particular it happens (unlike BufRead) after processing
modelines and (I think) after the FileType event if the file has a
nonempty autodetected 'filetype'. Of course a [No Name] file never has a
'filetype' unless you set one yourself after opening the file.

After starting Vim as

vim --cmd 'au BufRead,BufNewFile * let b:foo = 1'

I never see b:foo defined in a [No Name] buffer, no matter if it is the
initial buffer, or a buffer created in a new window by :new, or in the
current window by :enew. :e (with no filename) doesn't work for me in a
[No Name] buffer, it gives me

E32: No file name

while in a named buffer it reloads the current file from disk, but only
if used with an exclamation mark or if 'autowriteall' is on; otherwise
it gives error "E37: No write since last change (add ! to override)"
(regardless of the status of 'hidden').

Best regards,
Tony.
--
How come wrong numbers are never busy?

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