2009-08-22 52 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行中顯示)。