2009-08-22 48 views
10

如何在一行或多行的末尾重复添加字符,将行填充到特定列?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 

回答

18

\=submatch(),并repeat()组合:

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

+1我读了那个东西,想'那不行',但是果然...... – bmb 2009-08-22 21:15:57

+0

O_o。并不完全是我期待的三种按键解决方案;) 我主要得到你在这里做什么,但是\ v在开始时会做什么?另外,为什么运行这个替换会导致语法着色? – 2009-08-22 21:51:24

+3

\ v在正则表达式开始时使所有标点符号都是特殊的;我没有习惯,所以我不必记住什么是特别的,什么不特别。它不应该核心语法突出显示,除非额外的破折号是无效的语法?尝试按Ctrl-L重画屏幕。 – Eevee 2009-08-22 22:42:02

2

为防万一任何人在将来遇到这种情况,我有一种替代方法,我使用 (我认为)在罕见的情况下更容易记住,那就是需要人工填充一些行。

输入文本:

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行中显示)。