Vim:用字符填充行

时间:2009-08-22 20:27:42

标签: vim

如何在一行或多行的末尾重复添加字符,将行填充到特定列?

例如:
('x'代表第40列,不是该行上的字符;并且文本后面没有空格或制表符)

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

成为

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

2 个答案:

答案 0 :(得分:24)

\=submatch()repeat()的组合:

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

答案 1 :(得分:5)

万一以后有人会遇到这种情况,我有另一种方式可以使用 (我认为)在极少数人需要手动填充某些行的情况下更容易记住。

输入文字:

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

您输入的内容:

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

输出文字:

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

希望您能弄清楚如何调整命令的各个部分,使其完全符合您的要求。 注意 超出所需列间距的文本将被截断(在第3行演示)。