挑战“VimGolf的简单文本编辑”VimGolf:#1解决方案如何工作?

前端之家收集整理的这篇文章主要介绍了挑战“VimGolf的简单文本编辑”VimGolf:#1解决方案如何工作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Vim进行简单的文字编辑:
http://vimgolf.com/challenges/4d1a34ccfa85f32065000004

我发现很难理解#1解决方案(得分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 
'|' 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但简而言之
这些信息可以阅读周围的帮助:范围:

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

On every line that has a “V”,copy it down and delete the next line. Write
and quit.

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

:g/V/

当前行是4.现在:

t.

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

+d

删除第6行,光标保留在5.所以下一个:g匹配将在第8行。

原文链接:https://www.f2er.com/bash/387826.html

猜你在找的Bash相关文章