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.