5

I know it's a dumb question, but I don't find the explicit answer in the docs.

Learn Vimscript the Hard Way's chapter 12 is about autocmds.

Here are a few examples:

:autocmd BufNewFile * :write
:autocmd BufWritePre *.html :normal gg=G
:autocmd BufNewFile,BufRead *.html setlocal nowrap
:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
:autocmd FileType python     nnoremap <buffer> <localleader>c I#<esc>

where the first 2 feature a : leading their {cmd} argument, whereas the other 3 don't.

Does autocmd assume that its last argument is preceded by : if I don't write it explicitly?

1 Answer 1

5

The last argument to autocmd is an ex command and the leading : is optional in all ex commands, in cmdline and in vim script, though it works a bit differently in vim9script. In documentation a colon is usually meant to make it clear that what is being talked about is indeed an ex command instead of, say, an expression.

You can also use any number of colons in ex commands,

autocmd BufNewFile * ::::::::::::::write

and vim will simply ignore them. It is even permissible to use ranges with arbitrary colons for example,

::::1,2:::g/a/::::p

Or, if you really want to confuse people

::::1,2:::g:a::::p
1
  • 2
    "Coming soon: obfuscated vimscript contest"
    – D. Ben Knoble
    Commented Apr 8, 2022 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.