Advent 2023: (n)vim Plugins: tabular

Yesterday, I discussed vim-surround. Today I'm going to discuss another plugin I've used a ton: tabular.

What problem does it solve?

Let's say you have a PHP associative array declaration:

    "name" => "Matthew",
    "status" => "online",
    "url" => ""

Now, for some folks, this declaration is fine. However, I tend to prefer aligning the declarations, as it helps me visually parse the block more easily.

Now, I could go to each line, and insert space before the => assignment operators. This works, and for a short declaration like in this example, it's relatively quick. But when the declaration is larger, or the sizes of the keys vary a lot, this can become tedious quickly.

Another place I've used it is Markdown tables:

| Name | Status | URL |
| ---- | ------ | --- |
| Matthew | online | |
| An Example | offline | |
| API | online | |

This demonstrates the problem even better, as you can see that the columns do not align, which makes understanding if each row has all the required columns harder.


The tabular plugin allows you to easily tabularize text. You visually select the rows you want to line up, and then invoke it, providing a search string (a regular expression) for matching the character(s) to align against. It then does the work of determining how much space to add to each column to get things to line up.

Going back to the original example, I'd select all the entries within the array, and then:

  • : to enter command mode
  • Tabularize /=> to invoke tabular, and tell it to align against =>
  • Enter

and then you get:

    "name"   => "Matthew",
    "status" => "online",
    "url"    => ""

In the second example, I'd select all rows, including the header, and use | as the search. This results in:

| Name       | Status  | URL             |
| ----       | ------  | ---             |
| Matthew    | online  |        |
| An Example | offline |     |
| API        | online  | |

Hint: use your tab key

The Tab key is your friend, both in vim's command mode, as well as in your shell. I generally type :Tabu[Tab], and it expands to :Tabularize for me. And, of course, you could bind it to a keystroke or function if you wanted.


Interestingly, I use tabular less and less.


My coding standard tools can do code alignment, and since I have to run this anyways, I can save myself some time by just not doing alignment during my coding session.

With markdown tables, my current markdown plugins and language server configuration autoindents for me. As I finish a row, it automatically reformats the table to tabularize everything, which means I don't need to take any extra steps to format.

Final Thoughts

I keep this plugin around because when I do need it, it's super convenient, less error-prone than doing it manually, and saves me time.