Site icon techbeatly

Setup Vim editor for YAML, Ansible, Kubernetes, OpenShift

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:

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:

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.

Exit mobile version