Sunday, January 28, 2024

[SOLVED] How to pad all lines with spaces to a fixed width in Vim (perhaps using sed, awk, etc.)?

Issue

How can I pad each line of a file to a certain width (say, 63 characters), padding with spaces if need be?

For simplicity, let’s assume that all lines are guaranteed to be less than 63 characters.

I use Vim and would prefer a way to do it there, where I can select the lines I wish to apply the padding to, and run some sort of a printf %63s current_line command.

However, I’m certainly open to using sed, awk, or some sort of linux tool to do the job too.


Solution

Vim

:%s/.*/\=printf('%-63s', submatch(0))


Answered By - kev
Answer Checked By - Marie Seifert (WPSolving Admin)