Mastodon hachyterm.io

I try to learn something new everyday but sometimes there are no big aha moments.

That’s why I’m falling back to writing a boring Vim post. I’m sorry.

Today I wanted to indent a code block. Normally, I heavily rely on auto-formatting with ALE. ALE auto-formats my code up to the language’s standards.

Still, I wanted to know how to indent a whole block of code.

For this, the plugin vim-indent-object proved to be useful. Why?

Vim text objects provide a convenient way to select and operate on various types of objects. These objects include regions surrounded by various types of brackets and various parts of language (ie sentences, paragraphs, etc).

This plugin defines a new text object, based on indentation levels. This is very useful in languages such as Python, in which the syntax defines scope in terms of indentation. Using the objects defined in this plugin, an entire if structure can be quickly selected, for example.

With this plugin it’s easy to select inner code blocks.

Let’s say we have this Elixir code:

defmodule Math do
  def sum(a, b) do
    a + b
  end
end

When my cursor is on the inner indention (the line with a + b), I can type in vaI in normal mode. visual select an Indention level and lines above/below.

Then I can use the shiftwith key mappings >> or << to indent my code.

Further Reading