I'm doing some Ansible work (on RHEL 8), so I want the YAML configs for vim. I have a yaml.vim file containing the syntax/etc, and a ~/.vimrc file:

autocmd BufNewFile,BufRead *.yaml,*.yml so ~/.vim/yaml.vim
autocmd FileType yaml,yml set ai ts=2 sw=2 et number

While logged in as user ansible, that worked just peachy with *.yml files. But then I copied the same files to the same places under ~ (root), and when I was root (via su or su -), vim was clueless about yaml.

I tried changing the ~/.vim/yaml.vim in .vimrc to ~root/.vim/yaml.vim -- no joy.

With some digging I discovered that vi doesn't read .vimrc when you're root but vim does!!?? But vim didn't work either.

Out of exasperation I tried rebooting my Linux server. Now it doesn't work for user ansible either!!??

I tried :source ~/.vimrc and it kinda worked. That picked up the .vimrc settings but not the syntax-highlight stuff in yaml.vim, even if I did a :e! to force it to re-read the file and see the file suffix. But that only worked for root!? And only a few times. Now it doesn't work. :source ~/.vim/yaml.vim doesn't do anything either.

I looked at vim -V and :scriptnames and both indicate .vimrc and yaml.vim are being sourced. E.g. :scriptnames output:

  1: ~/.vimrc
  2: /usr/share/vim/vim80/plugin/getscriptPlugin.vim
  3: /usr/share/vim/vim80/plugin/gzip.vim
  4: /usr/share/vim/vim80/plugin/logiPat.vim
  5: /usr/share/vim/vim80/plugin/manpager.vim
  6: /usr/share/vim/vim80/plugin/matchparen.vim
  7: /usr/share/vim/vim80/plugin/netrwPlugin.vim
  8: /usr/share/vim/vim80/plugin/rrhelper.vim
  9: /usr/share/vim/vim80/plugin/spellfile.vim
 10: /usr/share/vim/vim80/plugin/tarPlugin.vim
 11: /usr/share/vim/vim80/plugin/tohtml.vim
 12: /usr/share/vim/vim80/plugin/vimballPlugin.vim
 13: /usr/share/vim/vim80/plugin/zipPlugin.vim
 14: ~/.vim/yaml.vim

So why am I not picking up the FileType settings (set ai ts=2 sw=2 et number) from .vimrc (item 1) or the YAML syntax highlighting from yaml.vim (item 14)? Especially since this worked, for non-root, until I rebooted !?

  • Do you have file types enabled with filetype on (or better filetype plugin indent on) on your vimrc? When you open a YAML file, if you use :set ft? does it tell you filetype=yaml? If it doesn't, then that should explain why your autocmd FileType is not working. – filbranden Mar 25 at 17:22
  • I think you're on the right track. :set ft says "filetype=". This is true for .yml and .yaml. I added filetype on to my .vimrc and it picked up the ai ts=2 &etc from the autocmd FileType. Now :set ft says "filetype=yaml". I'm 99.9% sure I never had filetype on in there before, so I don't know why it worked before, and then it didn't. Still have a problem with the yaml.vim file. I assume it's a valid file because it worked before, but I did make a few changes there so it's possible I broke something. Is there an authoritative YAML syntax file? – Gary Mar 25 at 18:23
  • Yes, there's an authoritative YAML syntax file, and now with filetype on I'd expect you'll be sourcing it. Check your :scriptnames, I'm betting you'll see /usr/share/vim/vim80/ftplugin/yaml.vim there (and also /usr/share/vim/vim80/indent.yaml.vim). Can you confirm that? In any case, I'll probably write an answer for you shortly. – filbranden Mar 25 at 18:26
  • BTW I strongly recommend not modifying ts from 8. Instead, if you want a different level of indentation when you use a Tab in insert mode, set sts (softtabstop) instead. You can set it to 2 in this case. It's quite possible that the default YAML plugin will do that already though, so you might not need to manually change that. – filbranden Mar 25 at 18:27
  • 1
    PS if you want to deal with syntax, best to put it in ~/.vim/syntax/yaml.vim. Also use setlocal – D. Ben Knoble Mar 25 at 20:53

Your Answer

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

Browse other questions tagged or ask your own question.