6

を使用vim newfilenameしてファイルを開くと、このファイルが終了しない場合vim、という名前の新しいファイルが作成されますnewfilename

ただし、MacVimこのようには機能しません---つまりmvim newfilename、(alias mvim="open -a macvim")はエラーになります:newfilename does not exist

()が新しいファイルを作成して開くMacVimように構成する方法はありますか?mvim newfilenamealias mvim="open -a macvim"

4

2 に答える 2

7

エラーメッセージはからopenではなく、から来ていると思いますvim。エイリアスを関数に置き換えることができます。

mvim () {
    local f
    for f; do
        test -e "$f" || touch "$f"
    done
    open -a macvim "$@"
}

これにより、必要に応じて、開く前に空のファイルが作成されます。

editこれに関する @Peter Lyons のコメントは見られませんでした。この解決策を最初に提案した彼の功績に敬意を表すべきです。ピーターが彼の回答を提出したい場合は、喜んでこの回答を削除します。

于 2011-09-18T09:00:04.933 に答える
3

open コマンドに mvim エイリアスは必要ありません。代わりに、ほとんどの MacVim Snaphot にバンドルされているmvim ランチャー スクリプトを使用できます。その mvim をパスに追加した後、 を実行すると、mvim newfilegvim と同じように、新しい MacVim ウィンドウで新しいファイル バッファーが開きます。

上記にリンクされている MacVim mvim スクリプト:

#!/bin/sh
#
# This shell script passes all its arguments to the binary inside the
# MacVim.app application bundle.  If you make links to this script as view,
# gvim, etc., then it will peek at the name used to call it and set options
# appropriately.
#
# Based on a script by Wout Mertens and suggestions from Laurent Bihanic.  This
# version is the fault of Benji Fisher, 16 May 2005 (with modifications by Nico
# Weber and Bjorn Winckler, Aug 13 2007).
# First, check "All the Usual Suspects" for the location of the Vim.app bundle.
# You can short-circuit this by setting the VIM_APP_DIR environment variable
# or by un-commenting and editing the following line:
# VIM_APP_DIR=/Applications

if [ -z "$VIM_APP_DIR" ]
then
    myDir="`dirname "$0"`"
    myAppDir="$myDir/../Applications"
    for i in ~/Applications ~/Applications/vim $myDir $myDir/vim $myAppDir $myAppDir/vim /Applications /Applications/vim /Applications/Utilities /Applications/Utilities/vim; do
        if [ -x "$i/MacVim.app" ]; then
            VIM_APP_DIR="$i"
            break
        fi
    done
fi
if [ -z "$VIM_APP_DIR" ]
then
    echo "Sorry, cannot find MacVim.app.  Try setting the VIM_APP_DIR environment variable to the directory containing MacVim.app."
    exit 1
fi
binary="$VIM_APP_DIR/MacVim.app/Contents/MacOS/Vim"

# Next, peek at the name used to invoke this script, and set options
# accordingly.

name="`basename "$0"`"
gui=
opts=

# GUI mode, implies forking
case "$name" in m*|g*|rm*|rg*) gui=true ;; esac

# Restricted mode
case "$name" in r*) opts="$opts -Z";; esac

# vimdiff, view, and ex mode
case "$name" in
    *vimdiff)
        opts="$opts -dO"
        ;;
    *view)
        opts="$opts -R"
        ;;
    *ex)
        opts="$opts -e"
        ;;
esac

# Last step:  fire up vim.
# The program should fork by default when started in GUI mode, but it does
# not; we work around this when this script is invoked as "gvim" or "rgview"
# etc., but not when it is invoked as "vim -g".
if [ "$gui" ]; then
    # Note: this isn't perfect, because any error output goes to the
    # terminal instead of the console log.
    # But if you use open instead, you will need to fully qualify the
    # path names for any filenames you specify, which is hard.
    exec "$binary" -g $opts ${1:+"$@"}
else
    exec "$binary" $opts ${1:+"$@"}
fi
于 2012-04-26T15:44:06.760 に答える