在VimGolf挑战"使用Vim进行简单文本编辑":#1解决方案如何运作?

huj*_*eng 23 vim

使用Vim进行简单的文本编辑:http: //vimgolf.com/challenges/4d1a34ccfa85f32065000004

我发现很难理解#1解决方案(得分13).

对不起,这篇文章没有粘贴任何解决方案,因为我不知道这样做是否合适.

sid*_*yll 42

解决方案以: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.
Run Code Online (Sandbox Code Playgroud)

基本上,该解决方案在具有"V"的行上执行一些ex命令,这些命令正是需要编辑的行.您可能已经注意到,早期的解决方案依赖于重复行,而不是真正地改变它们.此解决方案特别显示了一个有趣的模

3jYjVp3jYjVp3jYjVpZZ
^     ^     ^
Run Code Online (Sandbox Code Playgroud)

哪个可以通过宏减少:

qa3jYjVpq3@aZZ
Run Code Online (Sandbox Code Playgroud)

使用该: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}.
Run Code Online (Sandbox Code Playgroud)

给出的地址是.,这意味着当前行:

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
Run Code Online (Sandbox Code Playgroud)

所以ex命令t.意味着"将当前行复制到当前行之下".然后,有一个栏:

                                                     :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 '\'.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

就是这样.

:g/V/t.|+d<CR>ZZ
Run Code Online (Sandbox Code Playgroud)

在每个具有"V"的行上,将其复制并删除下一行.写并退出.


我没有提到的一件事是为什么:g命令执行三次而不是6次甚至更多次(沿着进程重复行).该:g命令开始将光标定位在第一行,然后下移到$.但是如果命令改变当前行,:g则从那里继续.所以:

:g/V/
Run Code Online (Sandbox Code Playgroud)

当前行是4.现在:

t.
Run Code Online (Sandbox Code Playgroud)

这会将光标移动到第5行.然后:

+d
Run Code Online (Sandbox Code Playgroud)

删除第6行,光标保持为5.因此下一个:g匹配将在第8行.