MacVim:如何通过使用“alias mvim =” open -a macvim“从命令行创建新文件?”

6

当我使用vim newfilename打开一个不存在的文件时,vim会创建一个名为newfilename的新文件。

然而,MacVim不是这样工作的——也就是说,mvim newfilenamealias mvim="open -a macvim")将导致错误:newfilename不存在

有没有一种方法可以配置MacVim,使得mvim newfilenamealias mvim="open -a macvim")会创建一个新文件并打开它?


我正在运行mvim版本7.3.237,它可以很好地创建新文件。你在运行哪个版本? - Peter Lyons
@peter-lyons:很奇怪,我也有同样的版本。我是从官方网站下载了macvim二进制文件,而不是编译源代码。这是问题所在吗? - Liw
我从这里得到了快照58(我正在运行Janus)。也许可以尝试一下这个?https://github.com/b4winckler/macvim/downloads。否则,你可以在你的shell中将mvim别名为一个小的shell函数,该函数使用“touch”创建一个文件(如果它不存在),然后启动mvim。 - Peter Lyons
@peter-lyons:非常感谢您提供的信息。我刚刚意识到我正在使用alias mvim="open -a macvim"而不是官方的mvim脚本。官方的mvim脚本可以很好地创建新文件,但在打开现有的MacVim窗口中的文件时存在问题。请参见:https://dev59.com/33A75IYBdhLWcg3wGU6I - Liw
很高兴你解决了这个问题。感谢你提供的链接,它链接到一个邮件列表讨论,提供了一个很好的解决新窗口问题的方法! - Peter Lyons
2个回答

7
我猜测错误信息来自于open而不是vim。您可以用一个函数来代替您的别名;
mvim () {
    local f
    for f; do
        test -e "$f" || touch "$f"
    done
    open -a macvim "$@"
}

如果需要,在打开文件之前,这将创建空文件。
编辑:没有看到@Peter Lyons的评论; 感谢他首先提出这个解决方案。如果Peter想提交他的答案,我将很乐意删除这个答案。

它运行得非常好,非常感谢!@Peter Lyons:也非常感谢你! - Liw

3

您不需要使用mvim别名来执行open命令,您可以使用大多数MacVim Snaphots捆绑的mvim launcher script。将该mvim添加到您的路径后,运行mvim newfile,将会像gvim一样在新的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接