如何在一行或多行的末尾重复添加字符,将行填充到特定列?
例如:(
'x'表示第40列,不是行上的字符;文本后面没有空格或制表符)
line one x
line two x
line three x
line eleventy-billion x
Run Code Online (Sandbox Code Playgroud)
变
line one ------------------------------x
line two ------------------------------x
line three ----------------------------x
line eleventy-billion -----------------x
Run Code Online (Sandbox Code Playgroud)
Eev*_*vee 25
:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))
Run Code Online (Sandbox Code Playgroud)
为了防止将来有人偶然发现这种情况,我有一种替代方式,我会使用哪种(我认为)更容易记住,在极少数情况下需要手动填充一些线.
输入文本:
Here are some words
They do not have equal length
I want to insert characters after them until column 40
How to do?
Run Code Online (Sandbox Code Playgroud)
你输入什么:
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
Run Code Online (Sandbox Code Playgroud)
输出文字:
Here are some words-----------------
They do not have equal length-------
I want to insert characters after t-
How to do?--------------------------
Run Code Online (Sandbox Code Playgroud)
希望您能弄清楚如何调整命令的各个部分,使其完全符合您的要求.请注意,将截断超过所需列跨度的文本(在第3行上演示).