In this article, you will learn how to set up a minimum vim configuration to get up and running with vim for ansible.
There are many powerful text editors in the market like Vscode, and Sublime Text that takes less setup time and less learning curve. The Vscode text editor is much more suitable for ansible projects since it has support for redhat ansible extensions.
On the other hand, the vim text editor is equally powerful as Vscode but has a steep learning curve. You have to spend some days understanding different vim modes, keybindings, and other vim features.
Why vim? Either to meet the situational demand or could be a personal choice. In some environments, you will get access only to vim or nano which comes by default with the servers and cannot install external tools. This is what happened to me when I was working on an ansible project where I was able to use only vim and nano but had no approvals to install any external tools locally.
In the upcoming section, you will learn how to set up YAML configuration for vim since playbooks are written in YAML format.
VIM Configuration File
The vim configurations are stored in the file named .vimrc under the user’s home directory.
$ ls -l ~/.vimrc
ls: cannot access '/home/nixzie/.vimrc': No such file or directory
If the file is not available then you can create the file.
$ touch ~/.vimrc
$ ls -l ~/.vimrc
-rw-rw-r-- 1 nixzie nixzie 0 Feb 17 23:03 /home/nixzie/.vimrc
VIM Configurations
1. VIM Line Numbers
The following settings will enable line numbers. Either set “number” or “relativenumber”. The relativenumber option will set the current cursor position as line zero, and the number above and below the current cursor position will be set as 1.
NOTE: Double quotes can be prefixed with any line to mark the line as a comment. I have added comments for all the settings in this article.
" line number
set number
(or)
" relative line number
set relativenumber
2. VIM Syntax Highlighting & Color Scheme
You can find the list of installed color schemes from within the vim editor by pressing the “ESC” key followed by typing “:colorscheme”, then leave a space and press the key “<CTRL+D>”.
:colorscheme
blue darkblue delek elflord gruvbox koehler murphy peachpuff shine torte
codedark default desert evening industry morning pablo ron slate zellner
The following settings will set the color scheme and enable automatic syntax highlighting. Here I have picked “slate” as the color scheme.
"set colorscheme
colorscheme slate
"set syntax highlighting
syntax on
3. VIM File Type Configuration
By setting the following values, vim will automatically detect the file type and apply auto indentation depending upon the file type.
" filetype settings
filetype on
filetype plugin indent on
4. VIM Horizontal and Vertical Cursor Marker
By setting the following values, vim will automatically insert a horizontal and vertical marker that points to the current cursor position.
" Horizontal and Vertical cursor marker
set cursorline
set cursorcolumn
5. VIM Line Wrapping
By default, vim will allow the line only till the viewable window. If you wish to skip the line wrap and allow the line to extend beyond the viewable window then set the following value.
" Turn off line wrapping
set nowrap
You can also add a vertical line at a certain column position. This can be useful for manual indentation at a certain position.
" Set Column border at position 100
set cc=100
6. VIM Indentation Guide
By default, vim does not add an indentation guide. The indentation guide is nothing but the vertical lines for the indentation level. You have two options. Either download plugins like “indentLine” or add the following settings.
" Indentation Guide
let g:indentLine_char_list = ['|', '¦', '┆', '┊']
7. VIM Spaces & Tabs
By default, vim set the tab space to 8 columns and uses tabs instead of spaces. We have already enabled the file type settings which will take care of the indentation and spacing of relevant file types.
The following settings will set the tab space to 4 and convert tabs to spaces. In any case, if vim is not able to detect the file type this will be used for indentation and tab stop.
" Tab stop set to 4
set tabstop=4
" Multiple space as tab stops
set softtabstop=4
" Convert tabs to spaces
set expandtab
" Width for autoindets
set shiftwidth=4
" Indent next line the same as previous line
set autoindent
VIM Create YAML File Type Configuration
As foretold in the previous section, file type settings will automatically detect the yaml file and set the indentation to 2 spaces. In any case, if vim is not detecting the yaml file type then you can add the following function to the configuration file.
Nothing fancy here. All I did was created a function and moved all the settings inside the function. When you open a yaml file then the function will be called and the settings will be applied.
Why use the function? It is a personal choice. It gives me the flexibility to apply different settings based on file type. For example, I can create a function for bash scripts where I can add different settings.
"If the file type is yaml then call apply_yaml_settings function
autocmd FileType yaml call s:yaml_settings()
"apply yaml settings function
function! s:yaml_settings()
set number
set cursorline
colorscheme slate
syntax on
set tabstop=2
set softtabstop=0
set expandtab
set shiftwidth=2
set autoindent
let g:indentLine_char_list = ['|']
endfunction
After adding the above settings to the ~/.vimrc file, this is how my vim for yaml looks like.
Wrap Up
In this short article, I have shown basic settings to get up and running with yaml files. The vim text editor is much more powerful when you add plugins and customizations. You should also try vscode for ansible which is easy to set up and offers much more functionality.