5 illustrated uses of VIM for DevOps

Jérôme NAHELOU
CodeShake
Published in
3 min readJan 3, 2023

In this article, I will share with you my 5 best tricks to become more efficient when editing your manifest (Terraform, YAML, …) using VIM.

1. Focus on Top/Middle/Bottom

Manifest files can contain hundreds of lines. When you edit them, you can lose seconds, losing your focus or move cursor position while using your mouse to scroll up/down.

With zt, zz, zb, you can easily update your windows to move the line on top, center or bottom.

To remind you, it’s quite simple: z<top>, z<bottom>, zz to center.

2. Macros

Visual blocks are powerful when working on consecutive rows. But using them on a set of blocks can be repetitive.

In this scenario, you should look at macros.

Using macros can be tricky, but always follow the sequence:

  1. Start recording on a dedicated key using q<key>
  2. Execute commands you want to reproduce (move, replace, append, …)
  3. Stop recording using q
  4. Replay as needed using @<key>

In the following example, I used macros to quickly declare Terraform variables.

3. Registers (clipboards)

VIM provides 26 registers that you can use to copy/cut/paste content.

By default, when you copy or cut text, vim saves it in the "”(unnamed) register. But registers can be bound from a to z to be reused easily even if you copy or cut afterwards.

To do this, press " followed by the key to map and the action to be taken (y to copy, d to cut). Example: "ayy to copy and save the content to a register.

To paste the content, press "<key>p

You can use :reg to display the content of the registers.

Registers 2 through 9 are used by VIM to hold deleted content. Very useful during code refactoring.

4. Sort blocks

I like to sort my Terraform variables. But sorting multiple blocks is quite difficult.

In the following demo, I share with you a very effective trick I found to sort blocks.

5. Marks

Marks are a good alternative to zt, zz, zb we saw earlier. You can bind any key to jump from a location to the mark.

Set a mark using m<key>, then return to mark using `<key>

6. (extra) Cuc, right, left, > and <

Editing YAML is a nightmare, using cuc you can draw a vertical line to ensure you are at the correct indent.

Then select the block you want to indent and play with > or <. Repeat with.

References

--

--

Jérôme NAHELOU
CodeShake

Cloud rider at SFEIR the day, Akita Inu lover #MyAkitaInuIsNotAWolf