πΎ Archived View for home.hedy.dev βΊ posts βΊ vim-column-editing.gmi captured on 2024-05-10 at 10:34:32. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
Apr 10, 2024 (updated on Apr 18, 2024)
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. Spaces will be shown as `_` throughout the example.
If you don't already have Vim/Neovim setup to highlight trailing spaces, you can do so temporarily now by running `:match Underlined '\s\+