Vim: padding out lines with a character

Tag: vim Author: zr880205 Date: 2009-08-06

How can I repeatedly add a character at the end of one or more lines, padding out the line(s) to a specific column?

For instance:
('x' represents column 40, not a character on the line; and there are no spaces or tabs after the text)

line one                               x
line two                               x
line three                             x
line eleventy-billion                  x

becomes

line one ------------------------------x
line two ------------------------------x
line three ----------------------------x
line eleventy-billion -----------------x

Best Answer

A combination of \=, submatch(), and repeat():

:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))

comments:

+1 I read that thing and thought 'that won't work' but sure enough...
O_o. Not exactly the three keystroke solution I was hoping for ;) I mostly get what you're doing here, but what does the \v at the beginning do? Also, why does running that substitution nuke the syntax coloring?
\v at the start of a regex makes all punctuation special; I do it out of habit so I don't have to remember what's special and what's not. It shouldn't nuke syntax highlighting, unless the extra dashes are invalid syntax? Try ctrl-L to redraw the screen.
Oh, I see. After you do a search or substitution, Vim (with hlsearch turned on) highlights everything that matches, and my regex matches.. everything, so your whole document is highlighted, obscuring the syntax colors. Use :noh to turn off the highlighting from the last search.
Yes, search highlighting applies to the entire document, regardless of what the range on your :s was.