1

I am using cloud-init to parametrize cloud clusters. This is achieved via #cloud-config files, which usually have a .tpl or .tf.tpl extension. Here are a few examples, one of them illustrated below:

#cloud-config
# vim: syntax=yaml
#
# This is the configuration syntax that the write_files module
# will know how to understand. Encoding can be given b64 or gzip or (gz+b64).
# The content will be decoded accordingly and then written to the path that is
# provided. 
#
# Note: Content strings here are truncated for example purposes.
write_files:
- encoding: b64
  content: CiMgVGhpcyBmaWxlIGNvbnRyb2xzIHRoZSBzdGF0ZSBvZiBTRUxpbnV4...
  owner: root:root
  path: /etc/sysconfig/selinux
  permissions: '0644'
- content: |
    # My new /etc/sysconfig/samba file

    SMBDOPTIONS="-D"
  path: /etc/sysconfig/samba
- content: !!binary |
    f0VMRgIBAQAAAAAAAAAAAAIAPgABAAAAwARAAAAAAABAAAAAAAAAAJAVAAAAAAAAAAAAAEAAOAAI
    AEAAHgAdAAYAAAAFAAAAQAAAAAAAAABAAEAAAAAAAEAAQAAAAAAAwAEAAAAAAADAAQAAAAAAAAgA
    AAAAAAAAAwAAAAQAAAAAAgAAAAAAAAACQAAAAAAAAAJAAAAAAAAcAAAAAAAAABwAAAAAAAAAAQAA
    ....
  path: /bin/arch
  permissions: '0555'
- encoding: gzip
  content: !!binary |
    H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA=
  path: /usr/bin/hello
  permissions: '0755'

This format represents yaml in a way. I haven't found any plugin specifically for such files. Are there any plugins or should I configure vim to use a similar syntax highlighting for such files?

In case I should use another syntax, which one would you recommend? And how should I configure vim to use another syntax as an alias?

1
  • Plugin requests are off-topic here.
    – D. Ben Knoble
    Feb 20 at 12:25

1 Answer 1

1

To use filetype=yaml for *.tpl (which includes *.tf.tpl) files, you would create the following file ~/.vim/ftdetect/tpl.vim with this content:

autocmd BufRead,BufNewFile *.tpl set filetype=yaml

This way, cloud-config files will become aliases of YAML files.

Also see the answers to How to create a new vim filetype? Read :help new-filetype for documentation on how to add new filetypes to Vim and what can be done beyond defining aliases. Feel free to refine your question or ask a new one for concrete problems in the process.

The detected filetype can be overridden using a modeline as you seem to have tried. Note the differences between the syntax and filetype options (see :help 'syntax' and :help 'filetype').
The modeline to treat a file as YAML would be:

# vim: set filetype=yaml

As for the recommendations on plugins and filetypes, that's really up to you. You're working with those cloud-config files and you should use whatever makes you productive. Nobody else will be able to give you an answer of what works best for you.

1
  • 1
    Unfortunately yamllint complains about a missing space on the very first of every file starting with #cloud-config, which might be annoying if using that linter with vim. Adding a ignore-shebangs: true to ~/.yamllint.yml silences that warning.
    – sampi
    Apr 12 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.