tags:

views:

155

answers:

1

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
+8  A: 

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

:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))
Eevee
+1 I read that thing and thought 'that won't work' but sure enough...
bmb
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?
Steve K
\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.
Eevee
Ah. Thanks for the info about \v.And it's valid syntax (well, the dashes are in a commented out line, at least). And redrawing doesn't help. But *searching* restores it, weirdly enough. Vim syntax coloring is a black art to me
Steve K
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.
Eevee
You're right about the this being the result of everything in the document being highlighted rather than anything to do with syntax coloring. But for some reason, even if you remove the '%' from the beginning of the substitution, and hence just apply it to a single line, the whole document still gets highlighted.
Steve K
Yes, search highlighting applies to the entire document, regardless of what the range on your :s was.
Eevee
Thanks for that, and for checking back here and continuing to answer my ignorant questions.
Steve K