One option that I find quite flexible and intuitive is to use the
:normal command with a range, in order to repeat a sequence of Normal mode commands on multiple lines of text.
In this particular case, you can use that sequence to:
- Add more dots to all lines, ensuring the dots will go past a specific column
- Then, delete from a specific column until the end of the current word (which should truncate the dots), left-aligning the numbers.
- Finally, remove one extra dot from the lines which have 3-digit numbers, in order to right-align them.
Assuming this block of text is present in the middle of a file that has unrelated contents, you can start by visually selecting only the block that you want to reformat. Move to the first line of the block, then use
V to start linewise Visual mode, then
} to jump to the next blank line (right after the end of the block), then
h to go up to the previous line, which will be the end of the block. At this point, you'll have a Visual selection around the block you want to reformat.
Then, you can use a
:normal command to insert more dots to every line (so you have enough on every line to truncate at a specific column.) You can use
f. to move the cursor to the first
. in the line, followed by
20i. to insert twenty dots at that location. This assumes that there is no
.s in the items and that 20 dots are enough to insert. You might need to adapt this command if this assumption is invalid in your case. But assuming it's correct, the command would be:
Note that you don't need to type the
'<,'> part! That's added automatically by Vim when you type
: from a Visual selection. That part instructs Vim to execute the following command (in this case,
:normal) only in the Visual range.
You also don't need an
<Esc> to terminate the insertion, the
:normal command will implicitly leave Insert mode at the end of its execution.
After you run this command, you'll have 20 extra dots on every line in your block.
The execution of this command will deactivate your Visual selection, you can now select it back again using the
Now it's time to truncate the dots at a specific column. Let's say you want to truncate them at column 42. Then you can execute the
42l Normal-mode command to move right 42 characters (which should put you on column 43 and in the middle of the string of dots), followed by the
dE command to delete until the end of the current word (which should be the end of the dots, since they're followed by a space.) Putting it all together:
Again, you don't need to type the
'<,'> part, since you had a Visual selection (from the earlier
If everything went well, this sequence will have aligned the numbers and at this point we only need a tweak for the lines which have 3-digit numbers. If you remove a single dot from those lines, you'll have right-aligned all numbers.
Move to the first line with a 3-digit number, then start linewise Visual selection with
V, followed by
h to select the block of lines that have 3-digit numbers.
Here, you'll want to use Normal-mode command
f. to move to the beginning of the string of dots, followed by
x to delete a single character. So:
Done! Everything is properly aligned.
This procedure makes quite a few assumptions about your text, such as which characters are in the items (no dots), that adding 20 extra dots is enough, which column to truncate on, that you only have 2- and 3-digit numbers and that they're in order, so that all the 3-digit numbers are at the end.
But the point of this approach is that you can use some of Vim's powerful commands (such as
:normal) in order to repeat simple operations on many lines of text.
If you manage to come up with a suitable strategy (such as adding more dots, then truncating at a specific location, etc.) and manage to come up with simple operations you can use on blocks of lines, it can help you come up with very efficient ways to handle one-off formatting tasks (for which a plug-in is probably overkill) without making them overly repetitive.
You can of course mix and match other commands. For example,
:s with a pattern might be useful to add more dots at the proper location, or drop one from the lines with 3-digit numbers.
:g is also very useful. Visual-Block mode is particularly useful when dealing with columns. Recording and replaying macros are also a very useful tool.
Hopefully you'll find these suggestions useful and be able to incorporate some of these techniques into your Vim tool belt to help you tackle editing and formatting tasks with very efficient operations.