0

This is the current state of my autocmds.vim file.

" Option for specific filetypes                                                           
autocmd BufRead,BufNewFile *.md    setlocal textwidth=80 spell spelllang=en_gb            
autocmd BufRead,BufNewFile *.txt   setlocal textwidth=80 spell spelllang=en_gb            
autocmd BufRead,BufNewFile *.tf    setlocal tabstop=2 shiftwidth=2 expandtab              
autocmd BufRead,BufNewFile *.json  setlocal tabstop=2 shiftwidth=2 expandtab

" yaml
augroup filetype_yaml
    autocmd!
    autocmd BufNewFile,BufReadPost *.{yaml,yml}
    autocmd FileType yaml |
        \ setlocal foldlevelstart=20 |
        \ setlocal foldmethod=indent |
        \ setlocal expandtab |
        \ setlocal shiftwidth=2 |
        \ setlocal softtabstop=2 |
        \ setlocal tabstop=2
augroup END

" sh
augroup filetype_sh
    autocmd!
    autocmd BufNewFile,BufReadPost *.sh
    autocmd FileType sh |
        \ setlocal expandtab |
        \ setlocal shiftwidth=2 |
        \ setlocal softtabstop=2 |
        \ setlocal tabstop=2                                                              
augroup END    

The problems are:

  • The yaml and sh groups do not work. The settings do not apply when editing yaml or bash scripts.

  • If any files that are opened I get this message when nvim starts, whether or not it's a shell file.

    --- Autocommands ---
    --- Autocommands ---
    filetypedetect BufNewFile
    *.sh call dist#ft#SetFileTypeSH(getline(1))
    Press ENTER or type command to continue

If I comment out the .sh group I still get the unwanted --- Autocommands --- message, but only once.

1
  • Consider putting local settings in ~/.vim/after/ftplugin/sh.vim (for example).
    – D. Ben Knoble
    Jul 28 at 21:28

2 Answers 2

4

The syntax for defining an autocmd is:

autocmd [event-list] [pattern] [commands]

Where [event-list] is a comma-separated list of events, the [pattern] is what to apply it to (what exactly it matches depends on the event(s), but it's often the filename), and [commands] is one or more commands; where multiple commands can be separated by a |.

The syntax for viewing which autocmds are defined is:

autocmd [event-list] [pattern]

Where [event-list] and [pattern] are optional (e.g. autocmd prints all autocommands, autocmd Filetype all filetype ones, autocmd filetype sh all sh filetype ones).


In your first, you have:

autocmd BufNewFile,BufReadPost *.{yaml,yml}

This is missing the [commands] part, so it's taken as a "view all autocommands for BufNewFile,BufReadPost *.{yaml,yml}". You can check this from the Vim commandline:

:autocmd BufNewFile,BufReadPost *.{yaml,yml}
--- Autocommands ---

No autocommands yet; let's define one:

:autocmd BufNewFile,BufReadPost *.{yaml,yml} echo 'a'

And now we can see it:

:autocmd BufNewFile,BufReadPost *.{yaml,yml}
--- Autocommands ---
BufNewFile
    *.{yaml,yml}
              echo 'a'
BufRead
    *.{yaml,yml}
              echo 'a'

One of the nice things about Vim is that anything you put in your vimrc can be entered as a command in the : commandline; it's a great way to figure things out.

Since you already have autocmd FileType sh, you can simply remove these for both the yaml and sh filetypes, as they're superfluous; Vim's built-in filetype detection should already set this for *.sh, *.yml, and *.yaml files.

1

I believe your autocmd miss à command:

" yaml
augroup filetype_yaml
  autocmd!
  autocmd BufNewFile,BufReadPost *.{yaml,yml} setlocal foldlevelstart=20 |
   \ setlocal foldmethod=indent |
   \ setlocal expandtab |
   \ setlocal shiftwidth=2 |
   \ setlocal softtabstop=2 |
   \ setlocal tabstop=2
augroup END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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