Wednesday, January 2, 2019

Re: Dynamically creating an autocmd group

Am 02.01.2019 um 15:01 schrieb David Fishburn:> :ver
> VIM - Vi IMproved 8.1 (2018 May 18, compiled Dec 30 2018 14:48:55)
> MS-Windows 32-bit GUI version with OLE support
> Included patches: 1-662
>
> I have the following augroup:
>
> augroup MyTest
> autocmd!
> autocmd FocusGained * :if has('clipboard') | echomsg "has clipboard" | endif
> autocmd User * :echomsg "anything"
> augroup END
>
> I want to be able to dynamically create additional entries in this
> augroup based on which version of Vim I am running. I would prefer
> not to just use:
>
> if v:version < 7.1
> augroup ..
> augroup END
> else
> augroup ..
> augroup END
> endif
>
> Since I am checking multiple things and multiple versions and don't
> want to repeat the code over and over again.
>
> I have tried some simple things:
>
> :exec "augroup dave \ autocmd! \ autocmd User Test echomsg 'it worked' \ augroup END"
> :au dave
> --- Autocommands ---
> Press ENTER or type command to continue
>
> exec "augroup dave | autocmd! | autocmd User Test echomsg 'it worked' | augroup END"
> exec "augroup dave \| autocmd! \| autocmd User Test echomsg 'it worked' \| augroup END"
>
> None of them seem to work, just can't get that syntax correct.
>
> If someone can help that would be great.
> The syntax must also support the if statement:
> autocmd FocusGained * :if has('clipboard') | echomsg "has clipboard" | endif
>
> TIA,
> David

It seems to me you just missed some basic syntax (?).

You can write:

:augroup MyTest
:augroup END
:augroup dave
:augroup END

" clear existing autocmds of each group:
:autocmd! MyTest
:autocmd! dave

" add autocommands
:autocmd MyTest FocusGained * :if has('clipboard') | echomsg "has clipboard" | endif
:autocmd dave User Test :echomsg 'It works!'

--
Andy

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: