Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong indentation when editing Yaml in Vim

Tags:

vim

yaml

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?

like image 804
Glueon Avatar asked Nov 16 '14 22:11

Glueon


People also ask

Do indentations matter in YAML?

Indentation. The suggested syntax for YAML files is to use 2 spaces for indentation, but YAML will follow whatever indentation system that the individual file uses. Indentation of two spaces works very well for SLS files given the fact that the data is uniform and not deeply nested.

How do I change the indentation in Vim?

Start in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

What does indentation mean in YAML?

In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line. To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently.


1 Answers

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.

like image 98
kiminoa Avatar answered Oct 04 '22 22:10

kiminoa