【问题标题】:Run shell script from Vim with currently open file as argument使用当前打开的文件作为参数从 Vim 运行 shell 脚本
【发布时间】:2011-10-24 17:03:55
【问题描述】:

我刚刚开始使用 Vim。

这是一个我在BBedit 中经常使用的shell 脚本。

#!/bin/sh

filename=$(basename "${BB_DOC_PATH##*/}" .ly)
directory=${BB_DOC_PATH%/*}/

cd "${directory}"

lilypondPDFoutput="${directory}"$filename".pdf"

/Applications/Lilypond.app/Contents/Resources/bin/  lilypond -dno-point-and-click -ddelete-intermediate-    files "$BB_DOC_PATH"

wait

open "${lilypondPDFoutput}"

BB_DOC_PATH 是一个变量,代表当前打开文件的路径。 (例如/Users/me/Documents/file.ly

我将如何将此脚本放在我的 .vimrc 中,并使用像 :typeset 这样的简单命令来调用它?

注意:我正在排版一个Lilypond 文件。

【问题讨论】:

    标签: shell vim lilypond


    【解决方案1】:

    你可以使用类似的东西:

    :!your_script %
    

    如果您在 PATH 中有 your_script,它应该可以正常工作。有关文档,请参阅 :!file modifiers

    【讨论】:

      【解决方案2】:

      OP 询问如何将脚本放在 .vimrc 中。由于 Vim 导入文件执行行延续的奇怪方式,这有点棘手。应该是这样的:

      command Typeset call Typeset()
      fun Typeset()
          let $TYPESET_PATH = expand("%:p")
          let $TYPESET_ROOT = expand("%:p:r")
          let $TYPESET_DIR = expand("%:p:h")
          !sh -icx '
          \ cd "${TYPESET_DIR}"
          \; lilypondPDFoutput="${TYPESET_ROOT}.pdf"
          \; /Applications/Lilypond.app/Contents/Resources/bin/lilypond -dno-point-and-click "$TYPESET_PATH"
          \; wait
          \; open "${lilypondPDFoutput}"
          \'
      endfun
      

      这就是现在在完全不同的环境(Lilypond/Win32;Vim for Cygwin)中对我有用的东西。

      " Quick compile command for Lilypond.
      command Typeset call Typeset()
      fun Typeset()
        let $TS_NAME = expand("%:t")
        let $TS_DIR = expand("%:p:h")
        let $TS_PDF = expand("%:t:r") . ".pdf"
        !sh -icx ' cd "${TS_DIR}" && lilypond "${TS_NAME}" && cygstart "${TS_PDF}" '
      endfun
      

      注意:Lilypond/Win32 不理解正斜杠路径。因此,我在其论点中消除了路径。你也可以这样做。您已经使用“cd”设置了路径。同样对于我的环境,我取出了点击选项以及“等待”,并将“打开”更改为“cygstart”。那时外壳部分足够短,我不需要 Vim 要求的相当神秘的行延续。同时我添加了快捷操作符,以便任何阶段的错误都会停止该过程。

      【讨论】:

      • 我将您的函数粘贴到我的 .vimrc 中,但运行 :Typeset 会引发错误 - 请参阅上面对我的问题的修改。
      • 我可以尝试帮助调试它,所以我需要知道您遇到的错误的具体情况。
      • 好的,这看起来像是我的愚蠢的复制和粘贴错误。删除“cd”之前的分号。由于我无法在我的系统上完全重现您的环境,因此我们很可能会在使其正常工作之前遇到其他错误。
      • 糟糕!您似乎在我完成上述问题的编辑之前回复了我的评论。
      • 读取 \; 的行/Applications/Lilypond.app... 应改为 \; /Applications/Lilypond.app/Contents/Resources/bin/lilypond -dno-point-and-click "$TYPESET_PATH"
      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多