【问题标题】:Is it possible to object-select a heredoc or "here document" with vim?是否可以使用 vim 对象选择 heredoc 或“此处文档”?
【发布时间】:2010-02-26 18:29:35
【问题描述】:

Vim 的一大优势是object-select,它可以快速处理单词、段落和各种分隔符中的内容。

例如,

vi{

将选择一对 {} 大括号内的所有内容。

是否有任何等效功能可以选择here document 或heredoc:

<<<HTML
    ....
    ....
HTML;

基于 ErichBSchulz 的回答我为heredoc 内部选择想出了以下内容:

nmap <F6> ?<<<<CR>w*kV?<<<<CR>j

?<<<<CR>w    " find beginning tag (after <<<)
*k           " find matching end tag and go up 1 line
V            " enter visual mode
?<<<<CR>j    " find beginning tag and go down 1 line

【问题讨论】:

    标签: vim keyboard-shortcuts heredoc


    【解决方案1】:

    为了选择heredoc,我通常将光标放在第一行,在heredoc标识符上,然后按V*

    V 将开始一个行选择,而 * 将开始一个搜索,到标识符的下一个匹配,heredoc 的结尾...

    【讨论】:

      【解决方案2】:

      &lt;&lt;''heredoc 以空行结束很容易,如果您已经在开头 (?&lt;&lt;^M^M):v} 从这里选择直到空行。

      否则,在你的例子中,我能想到的最好的就是v/^HTML

      【讨论】:

        【解决方案3】:

        这个插件可以让你定义自己的文本对象。

        http://www.vim.org/scripts/script.php?script_id=2100

        我想最好定义一个,这样你就可以说“yih”(在 heredoc 中猛拉),这样你就不必明确地从头开始。

        请注意,我自己并没有玩弄过这个。

        如果你只是想要一个视觉选择,你可以。

         nnoremap <leader>ih ?HTML<cr>V/HTML<cr>
        

        【讨论】:

          【解决方案4】:

          想想之前的帖子,并试图让它们更近一步。这不会是 100% 可靠的(即,如果起始标签出现在 heredoc 中,则会中断,但类似于:

          ?<<< "find begining
          w    " go to starting tag
          *    " find starting tag
          v    " enter visual mode (could use Upper V to do an 'ambientish' object select 
          n    " find end tag
          e    " optional include end tag
          

          编辑:Ken 改进了上面的序列,所以我建议你使用他更好的键映射来进行“内部”heredoc 选择:

          nmap <F6> ?<<<<CR>w*kV?<<<<CR>j
          

          (这是基于php风格的heredocs)

          【讨论】:

          • 谢谢!我实际上已经忘记了这个问题,但你让我走上了正确的道路
          • 谢谢肯,很好的调整,但是你的键映射中有一个错字 - 应该是上 V - 即:nmap ?w*kV?j
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          • 2019-06-21
          • 1970-01-01
          • 2021-02-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多