English 中文(简体)
Run shell script from Vim with currently open file as argument
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签