commit 927dc3c2c49faeb8be94097d5faa0322928059b1
parent 528381587bd3cc57c7a3b288b94b3240a9adce68
Author: zeertzjq <zeertzjq@outlook.com>
Date: Fri, 20 Jun 2025 07:08:01 +0800
vim-patch:476b65e: runtime(doc): mention using <script> instead of <sfile> in :autocmd (#34580)
fixes: vim/vim#17569
https://github.com/vim/vim/commit/476b65ebac22eb7f4923c11d75da366eda95492e
Diffstat:
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt
@@ -78,9 +78,10 @@ exception is that "<sfile>" is expanded when the autocmd is defined. Example:
:au BufNewFile,BufRead *.html so <sfile>:h/html.vim
Here Vim expands <sfile> to the name of the file containing this line.
-Use <script> instead to avoid that: >
-
- :au BufNewFile,BufRead *.html so <script>:h/html.vim
+However, <sfile> works differently in a function, in which case it's better to
+use `:execute` with <script> to achieve the same purpose:
+>
+ :exe $'au BufNewFile,BufRead *.html so {expand("<script>:h")}/html.vim'
`:autocmd` adds to the list of autocommands regardless of whether they are
already present. When your .vimrc file is sourced twice, the autocommands
@@ -90,7 +91,7 @@ that you can easily clear them: >
augroup vimrc
" Remove all vimrc autocommands
autocmd!
- au BufNewFile,BufRead *.html so <script>:h/html.vim
+ au BufNewFile,BufRead *.html so <sfile>:h/html.vim
augroup END
If you don't want to remove all autocommands, you can instead use a variable