Re: [vim/vim] cannot use vim9 block inside nested autocmd (Issue #20918)

h_east (Vim Github Repository) <[email protected]> Sun, 02 Aug 2026 14:30:30 -0700
Newsgroups gmane.editors.vim.devel
Message-ID <vim/vim/issues/20918/[email protected]>

----==_mimepart_6a6fb6f650fad_d7116028504d
Content-Type: text/plain; charset="UTF-8"

h-east left a comment (vim/vim#20918)

At script level the block is only read when the argument of :autocmd *starts*
with "{".  In your example the argument of the outer :autocmd is

    autocmd BufWinEnter * ++once {

which ends with "{" but does not start with it, so the outer :autocmd keeps
just that one line.  The lines after it are executed as ordinary script lines
and the final "}" gives E1128.

This is the documented limitation: |:autocmd-block| points at |:command-repl|,
which says "No nesting is supported".  It is also not a regression.  The check
has been "the argument starts with {" since patch 8.2.3268, which added blocks
for :autocmd in 2021.

In a :def function it does work, because there the block is recognized by a
*trailing* "{" instead.  That went in as patch 8.2.3271, on the same day.  So
this is a workaround for now:

```vim
    vim9script
    def Setup()
      autocmd FileType fzf autocmd BufWinEnter * ++once {
          autocmd SafeState * ++once if true
              |     echo
              | endif
      }
    enddef
    Setup()
```

The registered autocmd then holds the whole block, and it fires as you expect.

Two rules for the same syntax is not a good state, and I will have a look at
making the script level accept a trailing "{" as well.

About building older versions: on Debian tgetent is in libtinfo, not in
libncurses.  Configure with

    ./configure --with-tlib=tinfo

-- 
Reply to this email directly or view it on GitHub:
https://github.com/vim/vim/issues/20918#issuecomment-5160427706
You are receiving this because you are subscribed to this thread.

Message ID: <vim/vim/issues/20918/[email protected]>

-- 
-- 
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/20918/5160427706%40github.com.

----==_mimepart_6a6fb6f650fad_d7116028504d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div style=3D"display: flex; flex-wrap: wrap; white-space: pre-wrap; align-=
items: center; "><img height=3D"20" width=3D"20" style=3D"border-radius:50%=
; margin-right: 4px;" decoding=3D"async" src=3D"https://avatars.githubuserc=
ontent.com/u/518808" /><strong>h-east</strong> left a comment <a href=3D"ht=
tps://github.com/vim/vim/issues/20918#issuecomment-5160427706">(vim/vim#209=
18)</a></div>
<p dir=3D"auto">At script level the block is only read when the argument of=
 :autocmd <em>starts</em><br>
with "{".  In your example the argument of the outer :autocmd is</p>
<pre class=3D"notranslate"><code class=3D"notranslate">autocmd BufWinEnter =
* ++once {
</code></pre>
<p dir=3D"auto">which ends with "{" but does not start with it, so the oute=
r :autocmd keeps<br>
just that one line.  The lines after it are executed as ordinary script lin=
es<br>
and the final "}" gives E1128.</p>
<p dir=3D"auto">This is the documented limitation: |:autocmd-block| points =
at |:command-repl|,<br>
which says "No nesting is supported".  It is also not a regression.  The ch=
eck<br>
has been "the argument starts with {" since patch 8.2.3268, which added blo=
cks<br>
for :autocmd in 2021.</p>
<p dir=3D"auto">In a :def function it does work, because there the block is=
 recognized by a<br>
<em>trailing</em> "{" instead.  That went in as patch 8.2.3271, on the same=
 day.  So<br>
this is a workaround for now:</p>
<div class=3D"highlight highlight-source-viml" dir=3D"auto"><pre class=3D"n=
otranslate">    <span class=3D"pl-c1">vim9script</span>
    <span class=3D"pl-c1">def</span> <span class=3D"pl-en">Setup</span>()
      <span class=3D"pl-k">autocmd</span> <span class=3D"pl-c1">FileType</s=
pan> <span class=3D"pl-s">fzf</span> <span class=3D"pl-k">autocmd</span> <s=
pan class=3D"pl-c1">BufWinEnter</span> <span class=3D"pl-s">*</span> <span =
class=3D"pl-k">++once</span> {
          <span class=3D"pl-k">autocmd</span> <span class=3D"pl-c1">SafeSta=
te</span> <span class=3D"pl-s">*</span> <span class=3D"pl-k">++once</span> =
<span class=3D"pl-k">if</span> true
              |     <span class=3D"pl-c1">echo</span>
              | <span class=3D"pl-k">endif</span>
      }
    <span class=3D"pl-c1">enddef</span>
    <span class=3D"pl-en">Setup</span>()</pre></div>
<p dir=3D"auto">The registered autocmd then holds the whole block, and it f=
ires as you expect.</p>
<p dir=3D"auto">Two rules for the same syntax is not a good state, and I wi=
ll have a look at<br>
making the script level accept a trailing "{" as well.</p>
<p dir=3D"auto">About building older versions: on Debian tgetent is in libt=
info, not in<br>
libncurses.  Configure with</p>
<pre class=3D"notranslate"><code class=3D"notranslate">./configure --with-t=
lib=3Dtinfo
</code></pre>

<p style=3D"font-size:small;-webkit-text-size-adjust:none;color:#666;">&mda=
sh;<br />Reply to this email directly, <a href=3D"https://github.com/vim/vi=
m/issues/20918#issuecomment-5160427706">view it on GitHub</a>, or <a href=
=3D"https://github.com/notifications/unsubscribe-auth/ACY5DGBCK2WIOVKNUWVKM=
JD5H6XHNAVCNFSNUABEKJSXA33TNF2G64TZHM2DAOJZG42DQMR3JFZXG5LFHM2TANBSGM3TIMBT=
G2QXMAQ">unsubscribe</a>.<br />Triage notifications, keep track of coding a=
gent tasks and review pull requests on the go with GitHub Mobile for <a hre=
f=3D"https://github.com/notifications/mobile/ios/ACY5DGDMXPMSWVNZYVFCGXL5H6=
XHNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJWGA2DENZXGA3KM=
4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ">iOS</a> and <a hre=
f=3D"https://github.com/notifications/mobile/android/ACY5DGAJFV3JWODU3GZ6RE=
D5H6XHNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJWGA2DENZXG=
A3KM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ">Android<=
/a>. Download it today!
<br />You are receiving this because you are subscribed to this thread.<img=
 src=3D"https://github.com/notifications/beacon/ACY5DGFCRQ7Q3H7KHVUYFK35H6X=
HNBFCNFSM6AAAAAC4WW6XHCWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3=
TUL5UWJTYAAAAACM4V4C5KM4TFMFZW63VKON2WE43DOJUWEZLE.gif" height=3D"1" width=
=3D"1" alt=3D"" /><span style=3D"color: transparent; font-size: 0; display:=
 none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: =
0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span>&lt;vim/vi=
m/issues/20918/5160427706</span><span>@</span><span>github</span><span>.</s=
pan><span>com&gt;</span></span></p>

<script type=3D"application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/vim/vim/issues/20918#issuecomment-5160427706"=
,
"url": "https://github.com/vim/vim/issues/20918#issuecomment-5160427706",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>

<p></p>

-- <br />
-- <br />
You received this message from the &quot;vim_dev&quot; maillist.<br />
Do not top-post! Type your reply below the text you are replying to.<br />
For more information, visit <a href=3D"http://www.vim.org/maillist.php">htt=
p://www.vim.org/maillist.php</a><br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;vim_dev&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">vim_dev+uns=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
vim_dev/vim/vim/issues/20918/5160427706%40github.com?utm_medium=3Demail&utm=
_source=3Dfooter">https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/=
20918/5160427706%40github.com</a>.<br />

----==_mimepart_6a6fb6f650fad_d7116028504d--