【问题标题】:VIM How to insert output of a shell command at a different place in the fileVIM 如何在文件的不同位置插入 shell 命令的输出
【发布时间】:2011-02-28 04:42:52
【问题描述】:

我想出了一个 perl 脚本,它输出一个模板,用于在我的 C 代码中定义函数/结构的情况下记录函数和结构。

要使用它,我直观地选择结构的定义,将其拉到原始定义的正上方,然后在这个粘贴的结构上调用脚本。它用该结构的文档替换它。

现在有没有一种方法可以避免粘贴?我正在寻找一种调用 shell 命令的方法,但该命令的输出应该粘贴到文件中的其他位置,而不一定要粘贴在上面。

IOW :'a,'b!perl ~/bin/document.pl 替换标记 a 和标记 b 之间的文本,我想在标记 a 上方添加 document.pl 的输出。

【问题讨论】:

    标签: vim


    【解决方案1】:

    一种可能的解决方案是修改 perl 脚本,使其在最后也输出其输入。然后你会得到想要的结果。

    【讨论】:

    • 是的,我可以这样做,但我想保持脚本干净。我只能为 VI 创建另一个脚本。感谢您的建议
    【解决方案2】:

    如果你有 zsh 作为你的 shell,你可以使用协程:

    'a,'b!coproc perl ~/bin/document.pl ; tee >&p | cat <&p
    

    要在文本之前获得输出(此命令将其放在之后),您应该使用稍微复杂的命令:

    'a,'b!coproc perl ~/bin/document.pl ; tee >&p | cat <(<&p) -
    

    独立于系统的解决方案,使用 vim 和临时缓冲区:

    'a,'byank a | new | 0put a | $d | execute "%!perl ~/bin/document.pl" | %d a | bw! | 'a-1put a
    

    【讨论】:

      【解决方案3】:

      试试这样的:

      function! MyFunc() range
        " Preserve the register.
        let old_reg = @a
        exec a:firstline.','.a:lastline.'yank a'
        " Change to do what you need with register a.
        " Insert output before a:firstline
        exec (a:firstline - 1).'read !your magic with '.@a
        " Restore the register
        let @a = old_reg
      endfunction
      
      " :2,5MyOwn  will process lines from 2 to 5 and insert the output before line 2
      command! -bar -range -nargs=? MyOwn <line1>,<line2>call MyFunc()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-19
        • 1970-01-01
        • 2019-07-09
        • 1970-01-01
        • 2012-06-30
        • 2013-02-22
        • 1970-01-01
        相关资源
        最近更新 更多