如何在一行或多行的末尾重复添加字符,将行填充到特定列?
例如:(
'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
Eevee.. 25
的组合\=
,子匹配() ,以及重复() :
:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))
\ v在正则表达式开始时使所有标点符号特别; 我是出于习惯而做的,所以我不必记住什么是特殊的,什么不是.它不应该是语法高亮,除非额外的破折号是无效的语法?尝试ctrl-L重绘屏幕. (3认同)
原来如此.在您进行搜索或替换后,Vim(启用了hlsearch)突出显示匹配的所有内容,并且我的正则表达式匹配所有内容,因此整个文档突出显示,模糊了语法颜色.使用:noh关闭上次搜索的突出显示. (3认同)
Jason Cemra.. 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-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行上演示).
的组合\=
,子匹配() ,以及重复() :
:%s/\v^.*$/\= submatch(0) . " " . repeat("-", 39 - len(submatch(0)))
为了防止将来有人偶然发现这种情况,我有一种替代方式,我会使用哪种(我认为)更容易记住,在极少数情况下需要手动填充一些线.
输入文本:
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-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行上演示).