When editing YAML files for Kubernetes, Ansible, Docker Compose or CI/CD workflows, indentation mistakes are one of the easiest ways to break a configuration.
A full editor setup with plugins and linting is nice, but sometimes you are connected to a server over SSH and plain vi is all you have.
Here is a small setup I use when editing YAML files:
:set number
:set ruler
:set autoindent
:set tabstop=2
:set shiftwidth=2
:set expandtab
:set showmatch
For checking hidden characters, especially tabs, I also use:
:set list
To turn that view off again:
:set nolist
What the settings do
| Setting | What it does |
|---|---|
set number | Shows line numbers. Useful when an error message points to a specific line. |
set ruler | Shows the current cursor position, usually line and column, at the bottom of the editor. |
set autoindent | Keeps the current indentation level when starting a new line. |
set tabstop=2 | Displays tab characters as two columns wide. |
set shiftwidth=2 | Uses two spaces worth of indentation for shift commands like >> and <<. |
set expandtab | Inserts spaces instead of tab characters when supported by your vi implementation. Very useful for YAML. |
set showmatch | Briefly jumps to the matching bracket when typing a closing bracket. Not YAML-specific, but still helpful. |
set list | Displays hidden characters. This helps spot tabs, often shown as ^I. |
Important YAML note
YAML indentation should use spaces, not tabs.
That is why set expandtab is useful. If your vi supports it, pressing Tab will insert spaces instead of a real tab character.
If your minimal vi does not support expandtab, avoid the Tab key and use spaces manually.
Before saving an important YAML file, I usually run:
:set list
Then I quickly check whether any indentation contains ^I.
If I see ^I, I replace the tab with spaces.
Optional config
If your system uses ~/.exrc, you can add the basic settings there:
set number
set ruler
set autoindent
set tabstop=2
set shiftwidth=2
set expandtab
set showmatch
I would not enable set list permanently because it makes files harder to read. I prefer enabling it only when checking YAML files.
If you are on a very small system and one setting fails, check what your local vi supports with:
:set all
Final thought
This is not a YAML linter and it will not fully validate your file.
But for quick edits on a Linux server, these few vi settings make YAML editing safer:
- line numbers
- visible cursor position
- consistent indentation
- spaces instead of tabs, where supported
- visible hidden characters when needed
Small setup, useful habit.