16

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
Yewge
  • 1,894
  • 2
  • 15
  • 15

2 Answers2

29

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

:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))
0xdb
  • 3,539
  • 1
  • 21
  • 37
Eevee
  • 47,412
  • 11
  • 95
  • 127
  • 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? – Yewge Aug 22 '09 at 21:51
  • 3
    \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 Aug 22 '09 at 22:42
  • 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 – Yewge Aug 23 '09 at 01:07
  • 3
    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 Aug 23 '09 at 01:33
  • 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. – Yewge Aug 23 '09 at 14:15
  • 1
    Yes, search highlighting applies to the entire document, regardless of what the range on your :s was. – Eevee Aug 23 '09 at 17:17
  • Thanks for that, and for checking back here and continuing to answer my ignorant questions. – Yewge Aug 23 '09 at 17:27
11

Just in case anyone stumbles across this in the future, I have an alternative way that I use which (I think) is easier to remember on the rare occasion that one would need to manually pad out some lines.

Input Text:

Here are some words
They do not have equal length
I want to insert characters after them until column 40
How to do?

What you type:

gg                // Position cursor anywhere on first line you want to pad
q1$40A-<Esc>d40|q // Looks more complex than it is.
                  // Here, in English:
                  // 1. Record a macro in slot 1
                  // 2. Go to the end of the line, then *A*ppend a '-' 40 times
                  // 3. Delete from cursor position (which is still at the end of the line) to column 40
                  // 4. Stop recording
:1,4normal @1     // Repeat macro "1" for every line

Output Text:

Here are some words-----------------
They do not have equal length-------
I want to insert characters after t-
How to do?--------------------------

Hopefully you can figure out how to adjust the various parts of the command to make it do exactly what you want. Note that text which is longer than your desired column span will be truncated (demonstrated on line 3).

Jason Cemra
  • 563
  • 5
  • 15
  • that's a very suscinct & creative answer that even manages to get a macro in too! – Kes Jun 04 '20 at 16:15