Autocommand script

12 views
Skip to first unread message

A. Wik

unread,
Aug 22, 2020, 5:02:20 AM8/22/20
to vim...@googlegroups.com
Hi all,

I was wondering (a) if there is a better way to do it than this
script, and (b) whether all the backslashes and pipe characters (|)
are really necessary and why.

if has("autocmd")
au BufLeave * let x=0 | while x<=bufnr('$') |
\ if bufexists(x) |
\ set buflisted hidden bufhidden=hide |
\ endif |
\ let x = x+1 |
\ endwhile
endif " has("autocmd")

Regards,
Albert.

Tom Ryder

unread,
Aug 22, 2020, 11:15:50 AM8/22/20
to A. Wik, vim...@googlegroups.com
On Sat, Aug 22, 2020 at 09:01:56AM +0000, A. Wik wrote:
>if has("autocmd")
> au BufLeave * let x=0 | while x<=bufnr('$') |
>\ if bufexists(x) |
>\ set buflisted hidden bufhidden=hide |
>\ endif |
>\ let x = x+1 |
>\ endwhile
>endif " has("autocmd")

>I was wondering...
>(a) if there is a better way to do it than this script...

Can you explain what you're trying to do?

>(b) whether all the backslashes and pipe characters (|) are really
>necessary and why.

The pipe characters are necessary to define a concatenated list of
commands to :autocmd. The backslashes are line-continuation characters
to break up what would otherwise be a long line.

The easiest way to avoid both in this context is to define a function to
group the commands:

function! MyFunction() abort
...
endfunction

if has('autocmd')
autocmd BufLeave * call MyFunction()
endif

--
Tom Ryder <https://sanctum.geek.nz/>
Maybe we can bring back the light.

A. Wik

unread,
Aug 22, 2020, 11:37:10 AM8/22/20
to vim...@googlegroups.com
On Sat, 22 Aug 2020 at 14:55, Tom Ryder <t...@sanctum.geek.nz> wrote:
>
> On Sat, Aug 22, 2020 at 09:01:56AM +0000, A. Wik wrote:
> >if has("autocmd")
> > au BufLeave * let x=0 | while x<=bufnr('$') |
> >\ if bufexists(x) |
> >\ set buflisted hidden bufhidden=hide |
> >\ endif |
> >\ let x = x+1 |
> >\ endwhile
> >endif " has("autocmd")
>
> >I was wondering...
> >(a) if there is a better way to do it than this script...
>
> Can you explain what you're trying to do?

I'm trying to undo the "nobuflisted" status that some scripts set on
some buffers. In practice, the main thing is to keep the Vim help
buffers from disappearing from ":ls".

I suppose there is no need to iterate through all buffers. Maybe
"bufnr('%') would work to determine the exact buffer to apply the
options to, ie. only the buffer currently being left?

> >(b) whether all the backslashes and pipe characters (|) are really
> >necessary and why.
>
> The pipe characters are necessary to define a concatenated list of
> commands to :autocmd. The backslashes are line-continuation characters
> to break up what would otherwise be a long line.

I know what they do. I was just wondering, because this is not
normally necessary, but I think I understand now -- it is necessary
for concatenation of what would otherwise be limited to a single
command.

> The easiest way to avoid both in this context is to define a function to
> group the commands:
>
> function! MyFunction() abort
> ...
> endfunction

Why the "!" and "abort"?

-aw
Reply all
Reply to author
Forward
0 new messages