Ansible Quick Tip: YAML Space indentation settings for VIM editor

Subscribe to our newsletter and never miss any upcoming articles

Listen to this article

In this short article I will demonstrate how we can set up Vim for easier YAML editing. To avoid unnecessary syntax errors it’s always recommended to configure vim for auto indentation.

Create or update .vimrc file in your home directory

~ $ touch ~/.vimrc


Copy paste following content in vimrc file

~ $ cat ~/.vimrc
syntax on
filetype plugin indent on
"Get the 2-space YAML as the default when hit carriage return after the colon
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
set is hlsearch ai ic scs
nnoremap <esc><esc> :nohls<cr>


I recommend you just not to copy paste what I have written above. Try to understand the parameters I’ve used here.

autocmd — You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim.

FileType — Vim can detect the type of file that is edited. This is done by checking the file name and sometimes by inspecting the contents of the file for specific text.

setlocal — When you use :setlocal a global option, the new value is local.

ts — Set global tab spacing.

sts — Number of spaces that a counts for while performing editing operations, like inserting a

sw — shiftwidth (the number of spaces to use when indenting or — -de-indenting — -a line)

expandtab or et — (spaces instead of tabs). The expandtab property will ensure that when you hit tab it will actually use spaces.

That’s all!

Hope you like the article. Please let me know your feedback in response section.

No Comments Yet