Page cover

Unleashing VIM

This page is the how-to note on VIM as new tricks learned.

Multi-lines actions

In VIM, it is quite useful to utilise the visual mode and multi-line actions when you are commenting out multiple lines for troubleshooting or documentation.

Here is how to comment out multi-lines.

  1. Ensure that we are in normal mode.

  2. Move the cursor to the first line you want to comment out and press Ctrl + v to put the editor into visual mode.

  3. Select the lines you want to comment out with VIM's key j or arrow down key.

  4. After that, press Shift + i to put the editor into insert mode inside visual mode and then press # which will add a hash to the first line.

  5. Then press Esc to insert # character on all other selected lines.

Here is how to remove comment from multi-lines.

  1. Ensure that you are in normal mode.

  2. Move the cursor to the first line you want to comment out and press Ctrl + v to put the editor into visual mode.

  3. Select the lines you want to comment out with VIM's key j or arrow down key.

  4. Then press x to delete # character on all lines.

The same workflow can be used to perform any other multi-lines actions in VIM as well.

Macros

It can be quite powerful to use macros in VIM for the same repetitive task in your editor. For instance, a specific set of motions and actions you are about to perform at multiple places can be recorded as a macro.

Here is how to record a macro in VIM.

  1. Ensure that you are in normal mode.

  2. Press q + w to register w as macro.

  3. Perform the commands/actions while it shows recording @w.

  4. If it is in the insert mode, press Esc to get out of it.

  5. Then press q to end the recording which stores the commands/actions in @w.

To reuse the recorded macro, move the cursor to where you want to perform the same commands/actions in VIM, and press @w to recall the macro.

.vimrc

As someone who writes Ansible playbooks and manages infrastructure almost entirely in YAML, having Vim configured just right makes a massive difference to my day-to-day flow. Here’s a breakdown of my simple but practical .vimrc, focused on keeping spacing consistent, catching bad characters, and visualising whitespace — all of which are critical when working with indentation-sensitive formats like YAML.

set ai et ts=2 sw=2 sts=0
highlight NonAscii ctermbg=red guibg=red
syntax match NonAscii "[^\x00-\x7F]"
highlight SpecialKey ctermfg=1
set list
set listchars=tab:T→,trail:␣

Consistent, YAML-friendly indentation

set ai et ts=2 sw=2 sts=0
  • ai automatically indents new lines to match the previous one, saving you repetitive indenting.

  • et converts all tabs to spaces, which is essential for YAML.

  • ts=2 sw=2 enforce 2-space indentation, which is considered best practice for YAML and Ansible.

  • sts=0 makes pressing <Tab> insert exactly shiftwidth worth of spaces rather than mixing spacing.

Catch non-ASCII characters instantly

highlight NonAscii ctermbg=red guibg=red
syntax match NonAscii "[^\x00-\x7F]"

These two lines work together to highlight any sneaky non-ASCII characters (like smart quotes or hidden Unicode) with a bright red background. This is incredibly helpful when copying from websites or PDF documents, where curly quotes and odd whitespace can silently break YAML parsing.

Draw attention to whitespace characters

highlight SpecialKey ctermfg=1
set list
set listchars=tab:T→,trail:␣

Whitespace errors are one of the most common causes of YAML headaches — so I make them visible:

  • set list tells Vim to actually display invisible whitespace characters.

  • listchars defines what to show:

    • Tabs become T→ so I can spot them immediately.

    • Trailing spaces render as a visible symbol.

  • The SpecialKey highlight tweaks colouring so these markers stand out in the terminal.

This mini .vimrc is intentionally focused: keep indentation perfect, highlight bad characters, and make invisible whitespace obvious. It’s not flashy — but it saves me from subtle bugs daily when I’m writing Ansible playbooks and YAML configuration files.

Quickly Re-indent a Whole YAML File

If someone sends you a badly-formatted YAML file (mixed tabs/spaces, messy nesting, etc.), you can re-indent the entire file cleanly using one command inside Vim:

gg=G

What it does:

  • gg jumps to the very top of the file.

  • = is Vim’s built-in auto-indent command.

  • G tells it to apply until the end of the file.

Together, gg=G says: “Re-indent everything from top to bottom.” This uses whatever indentation settings you already have (e.g. et ts=2 sw=2), so after running it, your YAML file instantly snaps into neat, consistent two-space indentation.

Run a Command on Every Line - Efficient Batch Editing with :g and normal

Suppose you want to comment out all lines containing a certain word (like debug:) or you want to add indentation to every line that matches a pattern — Vim’s powerful :g command combined with normal mode commands lets you do this in seconds.

Example: Comment out every line containing debug: in your YAML file

:g/debug:/normal I#

How it works:

  • :g/debug:/ — Finds every line containing debug:.

  • normal I# — Runs normal mode command I# on those lines, which inserts # at the start of the line, effectively commenting it out.

Another example: Increase indentation by 2 spaces on lines with when:

:g/when:/normal 2>>
  • 2>> indents the line twice (2× your shiftwidth, usually 2 spaces each).

This saves you from repetitive manual edits and supercharges bulk modifications without leaving Vim or writing scripts.

Last updated

Was this helpful?