2022-08-03
Vim settings I'm using for editing YAML (with a sideline into Python)
I normally stick with minimal Vim customizations, partly because as a system administrator
I'm not infrequently editing files as a different user instead of
myself. However, due to Prometheus and other things I'm editing
more and more YAML these days, and YAML files have such a rigid and
annoying requirement for their indentation and formatting that it's
painful to edit them in a stock vi-like Vim setup. Initially I stuck
'modelines' at the top of most of the the Prometheus YAML files,
but by default these are ignored if you're root
so I had to
remember to ':set' them by hand. Recently I decided that enough was
enough, so I'd set our Prometheus server up so that YAML editing
worked properly.
My eventual .vimrc setting comes from this blog post:
autocmd FileType yaml setlocal expandtab shiftwidth=2 softtabstop=2
Some people will set tabstop
as
well (or instead of softtabstop
), but
I'm one of those people who has strong opinions about what an
actual tab is (also),
opinions that I want programs I use to respect.
(I started out simply turning on modelines for root with 'set
modeline
', which is safe on the particular machine I did it on,
then found some instructions on setting autocmds for 'BufRead,BufNewFile'
for YAML file extensions, then finally found the blog entry with
the FileType autocmd. Apparently filetype detection is
on by default in the Ubuntu 22.04 vim default settings.)
Possibly I should also set autoindent
for YAML files. But that feels more questionable and too overly
semi-intelligent. In YAML files I definitely always want those
indentation settings, but whether or not a given new line should
be autoindented is more context dependent.
Although I mostly edit Python code in GNU Emacs, where I have a well developed environment for it, I sometimes reach for vim for quick edits to scripts. Not all of my Python code is in the modern Python 3 style so I can't set a global option for it in my .vimrc, but I should probably consider sticking a vim modeline in my modern code to the effect of:
# vim: expandtab shiftwidth=4 softtabstop=4
That way there would at least be less chance of annoying accidents when I made quick edits with vim.
PS: I'm aware that I could install a variety of Vim plugins to make editing YAML in vim more pleasant. For a number of reasons, I want to stick to base vim features with no add-ons.
(This is partly an entry I write for myself so that I can find these settings later when I'm setting up another vimrc on another system.)