Apr 10, 2024 (updated on Nov 08, 2024)
1073 words
The visual block mode in Vim lets you edit text simultaneously across adjacent lines, similar to the "Alt-drag" feature in modern editors, but there's more you can do with it.
One of the basic formatting style that can be done to plain text content would be putting blocks of lines into columns, i.e., from this:
List title 1 - Item 1 - Item 2 - Item 3 List title 2 - Item 1 - Item 2 - Item 3
To this:
List title 1 List title 2 - Item 1 - Item 1 - Item 2 - Item 2 - Item 3 - Item 3
You can easily convert the text in between those two formats just by using visual block mode to do something similar to paste(1) (see the appendix).
Let's start with the vertical form, and put your cursor on the position marked by the block (▋)
List title 1 - Item 1 - Item 2 - Item 3 List title 2 - Item 1 - Item 2 ▋ Item 3
Begin visual block selection mode with `<C-v>`. Select the first character at each line with `3k`, then extend selection to the entire block using `$h`.
Your cursor should now be on the "2" with the entire "List title 2" and the three list items below it selected:
List title 1 - Item 1 - Item 2 - Item 3 List title ▋ - Item 1 - Item 2 - Item 3
Now hit `x` (or `d`) to remove the block and save it in one of the default registers temporarily.
List title 1 - Item 1 - Item 2 - Item 3
Now, we prepare the first list to be able to paste the second list that was just deleted. Pad spaces at the end of each line of the first list[^1]. Spaces will be shown as `_` throughout the example.
[^1]: Alternatively, you can skip this step by using `:set virtualedit=all`. Thanks to Martin Bays for pointing it out.
If you don't already have Vim/Neovim setup to highlight trailing spaces, you can do so temporarily now by running `:match Underlined '\s\+