使用当前打开的文件作为参数从 Vim 运行 shell 脚本

发布于 2024-12-11 09:10:32 字数 700 浏览 0 评论 0原文

我刚刚开始使用 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 文件。

I've just started using Vim.

Here is a shell script which I use frequenty from within BBedit.

#!/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 is a variable which represents the path of the currently open file. (e.g. /Users/me/Documents/file.ly)

How would I go about placing this script in my .vimrc, and invoking it with simple command like :typeset?

Note: I am typesetting a Lilypond file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2024-12-18 09:10:32

您可以使用类似的内容:

:!your_script %

如果 PATH 中有 your_script ,它应该可以正常工作。请参阅 :!文档的文件修饰符

You could use something like:

:!your_script %

If you have your_script in the PATH it should work fine. See :! and file modifiers for docs.

梦回旧景 2024-12-18 09:10:32

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”。那时 shell 部分已经足够短了,我不需要 Vim 所要求的相当神秘的行延续。同时,我添加了快捷操作符,以便任何阶段的错误都会停止该过程。

The OP asked how to place the script in the .vimrc. This gets just a little tricky because of the odd way Vim import files do line continuation. It would be something like this:

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

Here's what is actually now working for me in quite a different environment (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

Notes: Lilypond/Win32 does not understand forward-slash paths. Therefore I eliminated the path in its argument. You could do the same. You have already set the path with "cd". Also for my environment I took out the point and click option, as well as the "wait", and changed "open" to "cygstart". At that point the shell part was short enough I did not need the rather arcane line continuation demanded by Vim. At the same time I added shortcut operators so that an error at any stage would stop the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文