Better vim folds for Ansible playbooks

Most IDEs have the ability to fold text based on syntax. This is an essential feature if we are working with large playbooks. Vim has several methods that can do folding. However when we use yaml then we better make some adjustments.

YAML uses indentations to structure our code. Vim has an indent based fold method. That seems like a perfect match, but it isn’t.

We use the following playbook as an example.

---
- name: webpage
  hosts: webservers
  tasks:
    - name: Install apache2
      apt:
        name: apache2
        state: present
    - name: set up index.html
      template:
        src: index.html
        dest: /var/www/index.html
        owner: www-data
        group: www-data
        mode: '0200'

Let’s see what foldmethod=indent gets us.

---
- name: webpage
  hosts: webservers
  tasks:
    - name: Install apache2
      apt:
        name: apache2   (level 1, lines 1)-----------
    - name: set up index.html
      template: 
        src: index.html   (level 1, lines 4)---------

At first glance it looks decent but the problem is that it uses the first line of each indented block as a headline. What we typically would expect from folding is that we can hide entrire tasks. We can not do that with indent based folding.

So let us try something else to improve that. As most IDEs vim can do syntax based folding. But it does not recognize yaml by default so we have to install a filetype plugin like https://github.com/pedrohdz/vim-yaml-folds

Typically it is recommended to use a plugin manager for installation. But to keep things as simple as possible we do it manually. By doing so we get a basic understanding of how things fit together.

Filetype plugins live in their own directory which defaults to ~/.vim/after/ftplugin/ on debian but also may be elsewhere depending on the system you are on and how vim was installed. As an option we could choose a different location like $VIMRUNTIME for instance.

If the directory does not exist we can simply create it

mkdir -p ~/.vim/after/ftplugin

Now we copy the plugin itself to that directory naming it yaml.vim.

wget -O ~/.vim/after/ftplugin/yaml.vim testfile https://raw.githubusercontent.com/pedrohdz/vim-yaml-folds/master/after/ftplugin/yaml/folding.vim

We have to enable filetype plugins in our vimrc with

filetype plugin on

Vim can detect filetypes based on their content. If it detects that we are editing a yaml file it will load the filetype plugin.

If it does not detect the filetype automatically we can set the filetype manually with set filetype=yaml or we can do filetype detection based on the file extension. That is what by legacy systems like Windows do as well for filetype “detection”.

au BufRead,BufNewFile *.yml set filetype=yaml

Now let us check how it looks like with the plugin enabled

---
- name: webpage
  hosts: webservers
  tasks:
    - name: Install apache2
      apt:   (level 1, lines 2)---------------
    - name: set up index.html
      template:   (level 1, lines 6)----------

While this is just some basic yaml folding it is way better than the indent based method. Having a good folding method allows us to navigate through the document extremely quickly. We can close everything by default and only open the part of the code that we are working on. This can boost our productivity by factor 2 or more. Focussed work is the key to getting to maximum productivity. While it is not efficient working with 100 open tabs in a browser window we shouldn’t do the same things with our Ansible playbooks either.

Contact