Wrong indentation when editing Yaml in Vim

VimYaml

Vim Problem Overview


Vim does not seem to correctly react at a dash symbol in YAML files therefore breaking the format.

For example I have a block which should look like this:

  handlers:
        - name: restart exim4
          service: name=exim4 state=restarted

When I finish typing restart exim4 and type service: Vim reindents my final service line:

  handlers:
        - name: restart exim4
        service: name=exim4 state=restarted

So clearly Vim tries to align sentences column-wise but that's not what is needed in YAML. I want to create an array with two values.

How to fix that?

Vim Solutions


Solution 1 - Vim

In order to get the nice 2-space YAML as the default when I hit carriage return after the colon, I added this to my .vimrc:

autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab

This also plays nice with the dash for the OP's expected result.

Solution 2 - Vim

Install this plugin:

https://github.com/chase/vim-ansible-yaml

It is made with Ansible in mind, but in theory it will work with all kinds of YAML files. You will have to :set filetype=ansible unfortunately, if you are not working with ansible related files.

Solution 3 - Vim

You can use this autocommand to make Vim properly indent YAML files (put it to your .vimrc):

" Fix auto-indentation for YAML files
augroup yaml_fix
    autocmd!
    autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab indentkeys-=0# indentkeys-=<:>
augroup END

Basically, for YAML file it instructs Vim to:

  • Use 2 spaces for indentation.
  • Use spaces instead of tabs.
  • Skip re-indenting lines after inserting a comment character (#) at the beginning of a line, or a colon.

Solution 4 - Vim

You can disable reindent when you type : key:

:set indentkeys-=<:>

Please edit ~/.vimrc file, and add these lines:

filetype plugin indent on
autocmd FileType yaml setl indentkeys-=<:>

Note: autocmd comes after filetype.


You can trigger reindent by typing CTRL-F in INSERT mode, for example:

hello: world
    foo: bar<C-F>

Solution 5 - Vim

You can add a comment in your YAML to tell Vim special config for this file. For example:

# vim: set shiftwidth=2 tabstop=2 softtabstop=-1 expandtab:
foo:
  bar:
  - a
  - b

Then everyone, who use this file with a default vim, can share the file creator's configuration. It works well especially when cooperating.

Solution 6 - Vim

I've found https://github.com/stephpy/vim-yaml to work great. It's a vim plugin that does indentation (and syntax highlighting) of yaml files. Installing it solves the specific problem that you've asked about too.

You need to install the plugin (see the doco in the GitHub repo) and as long as your filetype=yaml it'll correct your indenting. It'll help you both

  1. as you're typing the snippet you've provided, or
  2. if you already have some yaml written, you can select it (with V for line-wise selection then use j or k to select more lines) then trigger the vim formatting with =

Solution 7 - Vim

Here's the augroup I have for yaml:

augroup filetype_yaml
    autocmd!
    autocmd BufNewFile,BufReadPost *.{yaml,yml} set filetype=yaml foldmethod=indent
    autocmd FileType yaml |
        setlocal shiftwidth=2 |
        setlocal softtabstop=2 |
        setlocal tabstop=2
augroup END

Solution 8 - Vim

I have found a very good answer instead of updating .vimrc.

here by Ignacio Vazquez-Abrams enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlueonView Question on Stackoverflow
Solution 1 - VimkiminoaView Answer on Stackoverflow
Solution 2 - VimK. NorbertView Answer on Stackoverflow
Solution 3 - VimEugene YarmashView Answer on Stackoverflow
Solution 4 - VimkevView Answer on Stackoverflow
Solution 5 - VimYan QiDongView Answer on Stackoverflow
Solution 6 - VimTom SaleebaView Answer on Stackoverflow
Solution 7 - VimMosheView Answer on Stackoverflow
Solution 8 - Vimkartik chawdaView Answer on Stackoverflow