寻找统一理论来解释|,<bar>和\ |在Vim

时间:2018-06-01 08:12:19

标签: vim

以下是一些实验,用于演示|<Bar>\|在Vim命令的不同场景中的表现。

下面的代码块显示了每个命令(或命令集)的几个Vim命令,然后是对命令行为方式的快速评论。

案例1:Shell命令

:!uname | new
/bin/bash: new: command not found

:!uname <Bar> new
/bin/bash: Bar: no such file or directory

:!uname \| new
usage: uname [-amnprsv]

:!uname | grep i
Darwin

结论:

  • |作为文字|传递给shell。
  • <Bar>作为文字<Bar>传递给I / O重定向。
  • \|作为文字|传递给shell。

所以在这种情况下,所有三个命令的行为都相似,从某种意义上说,你看到的是传递给shell的内容。所以这很容易理解。

案例2:用于shell命令的用户定义命令

:command! A !uname | new
:A
/bin/bash: new: command not found

:command! A !uname <Bar> new
:A
/bin/bash: new: command not found

:command! A !uname \| new
:A
usage: uname [-amnprsv]

:command! A !uname | grep i
:A
Darwin

:command! A !uname <Bar> grep i
:A
Darwin

结论:

  • |<Bar>行为相似(作为|传递给shell)。
  • \|行为不同(作为文字\|传递给shell)。

案例3:shell命令的地图

:map ,a :!uname | new<CR>
,a
Creates a new window when the `:map` command is executed.
Enters `!uname` in the command-line-mode when `,a` is pressed.

:map ,a :!uname <Bar> new
,a
Enters `:!uname | new` in the command mode.

:map ,a :!uname \| new
,a
Enters `:!uname | new` in the command mode.

结论:

  • |行为不同(:map与另一个Vim命令之间的命令分隔符,在这种情况下为new
  • <Bar>\|行为相似(执行map-rhs时它们变为|

案例4:接受参数的Vim命令

:new | vnew
Creates split windows

:new <Bar> vnew
Creates a single split window with file named `<Bar> vnew`

:new \| vnew
Creates a single split window with file named `| vnew`

结论:

  • |表现不同(作为命令分隔符)。
  • \|<Bar>行为相似(作为文字参数)。

案例5:用于接受参数的Vim命令的用户定义命令。

:command! A new | vnew
:A
Creates split windows.

:command! A new <Bar> vnew
:A
Creates split windows

:command! A new \| vnew
:A
Creates a single split window with file named `| vnew`

结论

  • |<Bar>的行为相似(在命令运行时期间作为命令分隔符)。
  • \|表现不同(在命令运行时期间作为new的文字参数)。

这与上面的案例2一致。

案例6:接受参数的Vim命令的映射。

:map ,a :new | vnew
,a
Creates a new window when the `:map` command is executed.
Enters `new` in the command-line-mode when `,a` is pressed.

:map ,a :new <Bar> vnew
,a
Enters `:new | vnew` in the command-line-mode when `,a` is pressed.

:map ,a :new \| vnew
,a
Enters `:new | vnew` in the command-line-mode when `,a` is pressed.

结论:

  • |行为不同(:map与另一个Vim命令之间的命令分隔符,在这种情况下为new
  • <Bar>\|行为相似(执行map-rhs时它们变为|

这与上面的案例3一致。这是有道理的,因为这种情况与案例2基本相同,即:map将键击重新映射到命令行模式命令。

案例7:不接受参数的Vim命令

:bn | bn
Goes to the 2nd next buffer.

:bn <Bar> bn
E488: Trailing characters

:bn \| bn
Goes to the 1st next buffer. What happened to `\| bn`?!

结论:

  • |<Bar>\|的所有三个行为都不相同。

案例8:用于不接受参数的Vim命令的用户定义命令。

:command! A bn | bn
:A
Goes to the 2nd next buffer.

:command! A bn <Bar> bn
:A
Goes to the 2nd next buffer.

:command! A bn \| bn
:A
Goes to the 1st next buffer.

结论: - |<Bar>行为相似(在命令运行时期间作为命令分隔符)。 - \|的行为就像\|\|中被bn \| bn字面用作:map ,a :bn | bn ,a Switches to the next buffer when `:map` command is executed. Enters `:bn` in the command-line-mode. :map ,a :bn <Bar> bn ,a Enters `:bn | bn` in the command-line-mode. :map ,a :bn \| bn ,a Enters `:bn | bn` in the command-line-mode 一样,产生了令人困惑的行为,如前一种情况所示。

此行为与案例2和案例5一致。

案例9:不接受参数的Vim命令的映射。

|

这与上面的案例3和案例6一致。

我的问题

关于如何在各种命令中解释<Bar>\|cpoptions假设Vim的默认设置(默认情况下),有哪些权威规则(根据权威Vim文档或参考) :map,普通:commandos.makedirs(r'\\SERVERNAME\SharedFolder\NewFolder') 命令等)?

1 个答案:

答案 0 :(得分:0)

发布此问题后,我一直在研究Vim的:help以收集尽可能多的线索。这是一个几乎似乎有用的理论。我收录了Vim :help的相关文档,我可以找到它来支持每一点。

  1. :!|<Bar>\|按字面传递给shell命令。

                                                            *:!cmd* *:!* *E34*
    :!{cmd}                 Execute {cmd} with the shell.  See also the 'shell'
                            and 'shelltype' option.
    
                            ...
    
                            A '|' in {cmd} is passed to the shell, you cannot use
                            it to append a Vim command.  See |:bar|.
    
  2. 许多命令行模式命令(例如map但不是:command:!)在遇到|时结束,因为这是命令分隔符。< / p>

                                                            *: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 '\'.
    
    These commands see the '|' as their argument, and can therefore not be
    followed by another Vim command:
    
        :argdo
        :autocmd
        :bufdo
        :cdo
        :cfdo
        :command
        ...
        :make
        :normal
        ...
        :[range]!
    

    请务必在上面的列表中注意,虽然:command:!使用|作为参数,但:map却没有。这在下面的第4点非常重要。

  3. 除非文档中针对某些命令行模式命令的<Bar>\|记录了某些特殊行为,否则这些行为将按字面意思使用。这可以解释上面第1点中<Bar>\|的行为。但它无法解释<Bar>在下面第4点的行为。

  4. 使用:command时,|<Bar>在执行用户定义的命令时,在运行时替换命令文本中的行为类似于命令分隔符。

                                                            *:bar* *:\bar*
    
    ...
    
    These commands see the '|' as their argument, and can therefore not be
    followed by another Vim command:
        :argdo
        :autocmd
        :bufdo
        :cdo
        :cfdo
        :command
        ...
        :make
        :normal
        ...
        :[range]!
    

    不清楚为什么<Bar>在本文档中|的行为与:command的行为相似。

  5. 使用:command时,在执行用户定义的命令时,\|在运行时在替换命令文本中作为文字\|参数传递。

                                                            *: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 '\'.
    
  6. 使用:map|map命令和第二个Vim命令之间的命令分隔符。执行map命令并定义用户定义的命令后,将立即执行第二个Vim命令。执行用户定义的命令时不执行它。这直接来自上面第2点引用的Vim文档。

  7. 使用:map<Bar>\|在用户定义的命令的运行时就像一个命令分隔符。

                                                            *map_bar* *map-bar*
    Since the '|' character is used to separate a map command from the next
    command, you will have to do something special to include  a '|' in {rhs}.
    There are three methods:
       use       works when                    example      ~
       <Bar>     '<' is not in 'cpoptions'     :map _l :!ls <Bar> more^M
       \|        'b' is not in 'cpoptions'     :map _l :!ls \| more^M
       ^V|       always, in Vim and Vi         :map _l :!ls ^V| more^M
    
    (here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
    cannot use the <> notation "<C-V>" here).
    
    All three work when you use the default setting for 'cpoptions'.
    

    此外,

                                            *key-notation* *key-codes* *keycodes*
    These names for keys are used in the documentation. ...
    
    ...
    
    <Bslash>        backslash               \        92     *backslash* *<Bslash>*
    <Bar>           vertical bar            |       124     *<Bar>*
    <Del>           delete                          127
    
    ...
    
    For mapping, abbreviation and menu commands you can then copy-paste the
    examples and use them directly.  Or type them literally, including the '<' and
    '>' characters.  This does NOT work for other commands, like ":set" and
    ":autocmd"!
    

    此外,

    SPECIAL CHARACTERS
    
    The ":map" command can be followed by another command.  A | character
    separates the two commands.  This also means that a | character can't be used
    inside a map command.  To include one, use <Bar> (five characters).  Example:
    
        :map <F8> :write <Bar> !checkin %:S<CR>
    
  8. 仍然无法解释的行为:

    • 为什么<Bar>的行为与|中的:command相似? (请注意,<Bar><Bar>命令中以:!字面意思传递给shell。我不明白为什么它应该像|中的:command一样。 )