Problem with 'hidden' and :bufdo

5 views
Skip to first unread message

Craig Barkhouse

unread,
Nov 24, 2009, 8:49:24 PM11/24/09
to vim...@googlegroups.com

Hi all,

 

I’ve had some occasions where I’ve been editing multiple files at once, and for some reason some of the files did not get syntax highlighting turned on.  I could never really pinpoint the circumstances until today when I decided to use a binary search approach to track down what it might be.

 

The problem occurs with 7.2.293 that I’ve compiled as well as stock 7.2 with the following 3-line .vimrc:

set hidden

filetype on

syntax on

 

And then invoke Vim as follows in a directory that has several .c files (it seems you could use any :bufdo command here, this is just an example):

vim -c “bufdo normal gg” *.c

 

The problem I see is that the first and last buffers have syntax highlighting, but all the intermediate buffers do not.  For example, if there are 10 .c files, then only buffers 1 and 10 will have syntax highlighting.

 

There are several different components interacting in this scenario, so I’m not sure which are actually necessary to cause the problem, and which are just there to expose the problem.  I suspect the problem may be in the interaction between ‘hidden’ and :bufdo, since both of those involve buffers.

 

Craig

 

P.S.  I actually don’t remember why I set ‘hidden’ in my .vimrc, it’s just been that way for so long!

 

John Beckett

unread,
Nov 25, 2009, 1:10:12 AM11/25/09
to vim...@googlegroups.com
Craig Barkhouse wrote:
> vim -c "bufdo normal gg" *.c
>
> The problem I see is that the first and last buffers have
> syntax highlighting, but all the intermediate buffers do not.

At ':help :bufdo' we see that bufdo disables the Syntax
automcommand event (for speed). That means the buffers you had
not previously visited do not have syntax highlighting. I do not
know the official way to overcome this issue, but I press F7
once (twice??) where I have this in vimrc:

" Toggle syntax off/on (from Help).
noremap <F7> :if exists("syntax_on") <Bar>
\ syntax off <Bar>
\ else <Bar>
\ syntax enable <Bar>
\ endif <CR>

John

Christian Brabandt

unread,
Nov 25, 2009, 1:38:18 AM11/25/09
to vim...@googlegroups.com
On Wed, November 25, 2009 2:49 am, Craig Barkhouse wrote:

> And then invoke Vim as follows in a directory that has several .c
> files (it seems you could use any :bufdo command here, this is just an
> example): vim -c "bufdo normal gg" *.c
>
> The problem I see is that the first and last buffers have syntax
> highlighting, but all the intermediate buffers do not. For example,
> if there are 10 .c files, then only buffers 1 and 10 will have syntax
> highlighting.

When issuing :bufdo, the Syntax autocommand event is added to the
'eventignore' option, so that bufdo can perform faster. This is
described at :h :bufdo

If you don't like this, you could possibly set this option within the
:bufdo command like this:

vim -c "bufdo set ei=| normal gg" *.c

> P.S. I actually don't remember why I set 'hidden' in my .vimrc, it's
> just been that way for so long!

It enables you to switch buffers, even when the current buffer is
modified. Otherwise you would have save the current buffer before
switching to another one.

regards,
Christian

Craig Barkhouse

unread,
Nov 25, 2009, 1:22:24 PM11/25/09
to vim...@googlegroups.com

Thanks Christian (and John Beckett) for the explanation.

This still doesn't quite explain the interaction with 'hidden'. If I comment out the "set hidden" line in my .vimrc, reducing it to the following 2-line .vimrc:
filetype on
syntax on

... then I do in fact get syntax highlighting for every buffer. (Unfortunately then 'nohidden' is set, which I don't want, for exactly the reason you pointed out.) Your suggested workaround does the trick. So does this:

vim -c "set nohidden" -c "bufdo normal gg" -c "set hidden" *.c

Reply all
Reply to author
Forward
0 new messages