Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get great auto indenting with Vim and Yaml?

Tags:

Right now I am using set autoindent when I edit Yaml files in Vim, and it works okay by just copying the previous line's indent when you start a new line. I can't help but feel like it could be a little smarter, though. What would be great is if starting a new line after foo: "bar" could keep the same indent level, but starting a line after foo: would add an additional level of indent.

like image 399
Ian Greenleaf Young Avatar asked Feb 04 '11 01:02

Ian Greenleaf Young


People also ask

How do I indent a YAML file in Vim?

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.

How do I fix indentation problem in YAML?

Use spaces for indentationedit Indentation is meaningful in YAML. Make sure that you use spaces, rather than tab characters, to indent sections. In the default configuration files and in all the examples in the documentation, we use 2 spaces per indentation level.

Does YAML care about indentation?

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.


2 Answers

Xavier inspired me to hack at this a bit, and I came up with my own little indent script for Yaml. It doesn't handle any of the more advanced Yaml features, but it does what I originally asked for.

To use this, you can just drop it into ~/.vim/after/ftplugin/yaml.vim (assuming you are using set ftplugin on).

like image 144
Ian Greenleaf Young Avatar answered Sep 17 '22 13:09

Ian Greenleaf Young


Specific indentation rules are stored on a language basis in :
$VIMRUNTIME\indent\foo.vim for language foo.

On my default installation, I have got ruby.vim, haml.vim and many other but no yaml.vim.

You could create an indent\yaml.vim to get the indentation rules you want. I have quickly tried to search online if something similar was available but I have not find anything.

like image 43
Xavier T. Avatar answered Sep 18 '22 13:09

Xavier T.