使用Vim编辑简单的文本:http://vimgolf.com/challenges/4d1a34ccfa85f32065000004
我发现很难理解第一个解决方案(得分13)。
对不起,这篇文章没有粘贴解决方案,因为我不知道这样做是否合适
解决方案以:g
命令为中心。从帮助:
:g :global E147 E148
:[range]g[lobal]/{pattern}/[cmd]
Execute the Ex command [cmd] (default ":p") on the
lines within [range] where {pattern} matches.
所以基本上,解决方案在带有"V"的行上执行一些ex命令,这些正是需要编辑的。你可能注意到了早期的解决方案依赖于复制线条,而不是真正的改变他们。这个解决方案特别显示了一个有趣的模式:
3jYjVp3jYjVp3jYjVpZZ
^ ^ ^
可以用宏来简化:
qa3jYjVpq3@aZZ
使用:g
命令的解决方案基本上做同样的事情。第一个执行的命令为t.
。从帮助:
:t
:t Synonym for copy.
:[range]co[py] {address} :co :copy
Copy the lines given by [range] to below the line
given by {address}.
给出的地址是.
,表示当前行:
Line numbers may be specified with: :range E14 {address}
{number} an absolute line number
. the current line :.
$ the last line in the file :$
% equal to 1,$ (the entire file) :%
't position of mark t (lowercase) :'
'T position of mark T (uppercase); when the mark is in
another file it cannot be used in a range
/{pattern}[/] the next line where {pattern} matches :/
?{pattern}[?] the previous line where {pattern} matches :?
/ the next line where the previously used search
pattern matches
? the previous line where the previously used search
pattern matches
& the next line where the previously used substitute
pattern matches
所以ex命令t.
表示"将当前行复制到当前行下面"。然后,有一个bar
:bar :bar
'|' can be used to separate commands, so you can give multiple commands in one
line. If you want to use '|' in an argument, precede it with ''.
和d
命令,这显然删除了行。它的取值范围是+
,表示"当前行+ 1"。你可以通过.+1
,但简称+
。这些信息可以在:range
:
The default line specifier for most commands is the cursor position, but the
commands ":write" and ":global" have the whole file (1,$) as default.
Each may be followed (several times) by '+' or '-' and an optional number.
This number is added or subtracted from the preceding line number. If the
number is omitted, 1 is used.
就是这样。
:g/V/t.|+d<CR>ZZ
在每一行有"V"的地方,把它抄下来并删除下一行。写和辞职。
我没有提到的一件事是为什么:g
命令执行三次而不是6次甚至更多(在整个过程中重复行)。:g
命令从第一行开始定位光标,然后移到$。但是如果命令改变了当前行,:g
将从那里继续。所以:
:g/V/
当前行为4。现在:
t.
将光标移动到第5行。然后:
+d
删除第6行,光标保留在第5行。所以下一个:g
匹配将在第8行。