Skip to content

Setup Vim editor for YAML, Ansible, Kubernetes, OpenShift

Avatar photo

https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan

Are you new to Vim and working with YAML files? This post is for you! If you’re already a Vim expert, feel free to skip this quick guide.

Ansible, Kubernetes, and OpenShift rely heavily on YAML files for configurations, playbooks, and resource definitions. Whether you are managing automation tasks, deploying applications, or configuring clusters, handling YAML files becomes a critical skill. The tricky part? YAML’s strict reliance on correct indentation and white spaces means a single mistake can break your playbook execution or cause resource deployment failures.

While you can edit YAML files with visual editors like VSCode, Sublime, or Atom, many Linux users prefer Vim for its speed, simplicity, and CLI-based efficiency. With Vim, you can quickly make changes and test configurations directly from the terminal.

Here’s how to configure your Vim editor to edit YAML files efficiently, saving time and avoiding errors when working with Ansible, Kubernetes, or OpenShift.

Vim Variables for Easy YAML Editing

Vim provides several parameters and variables that help you work with indentation-based files like YAML. Here’s my tuned setup for easier YAML editing:

set et ts=2 ai sw=2 nu

Explanation of Parameters:

  • ts (tabstop): Defines the width of a tab. Example: tabstop=2 sets tab width to 2 spaces.
  • et (expandtab): Converts tabs to spaces (important for YAML).
  • sw (shiftwidth): Determines the width for auto-indent operations. Example: shiftwidth=2.
  • nu (number): Displays line numbers.
  • sts (softtabstop): Adjusts the number of spaces Vim inserts when you hit Tab. Example: softtabstop=4.
  • cuc (cursorcolumn): Highlights the current column for better visibility.

Enable Syntax Highlighting

Syntax highlighting is on by default in most Vim installations. However, ensure it’s enabled by adding the following line to your ~/.vimrc:

syntax on

Choose a Colorscheme

Vim allows you to use different colorschemes to improve visibility. To see all installed colorschemes, type the following in Vim:

:colorscheme [TAB]

Choose a scheme that works best for you, such as:

:colorscheme desert

Other popular options include gruvbox, dracula, and solarized. Install them if they’re not available by default.

Show Line Numbers

To display line numbers for better navigation, add:

set number 

Make Your Cursor Stand Out

Highlight the current line to make your cursor position more visible:

set cursorline

You can also highlight the cursor column for even better visibility:

set cursorcolumn

Enable Indentation Visibility

To visualize indentation levels clearly, you can use the IndentLine plugin by Yggdroot or similar tools.

Make Configurations Permanent

To set all the above configurations permanently, edit your ~/.vimrc file and add:

autocmd FileType yaml setlocal et ts=2 ai sw=2 nu sts=0
set cursorline
set number
set syntax on

Putting It All Together

Once you’ve configured Vim with the above settings, here’s what you’ll get:

  • Automatic indentation for YAML files.
  • Line numbers for easy navigation.
  • Syntax highlighting to avoid YAML errors.
  • A highlighted cursor line and visible indentation levels for clarity.

Here’s an example of a properly configured Vim editor for editing YAML files with Ansible playbooks:

With these settings, editing YAML files for Ansible, Kubernetes, and OpenShift becomes faster and error-free. As a Linux user, Vim remains one of the most lightweight and powerful editors for CLI-based workflows.

If you want to explore further Vim customizations, you can refer to this gist or other detailed Vim guides.

Do you want to learn more about Ansible practical use cases? Check the latest book from the author as follows. Available on Packt and Amazon.

Ansible for Real Life Automation

Disclaimer:

The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo


https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of 𝗔𝗻𝘀𝗶𝗯𝗹𝗲 𝗳𝗼𝗿 𝗥𝗲𝗮𝗹-𝗟𝗶𝗳𝗲 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Comments

7 Responses

  1. zerodmg says:

    Hey , thanks !!!! very useful

  2. Narayan Banik says:

    set cursorlin –> set cursorline

  3. Chris says:

    if you dont use cuc then set cursorline doesn’t work

  4. Jeffrey says:

    Hey Gineesh, thanks for the tips! Peace and blessings. 🤓👍

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.