MacVim:通过使用`slias mvim ='open -a麦克维姆(MacVim)从命令行创建新文件。

发布于 2024-12-05 07:03:11 字数 356 浏览 1 评论 0原文

当我使用 vim newfilename 打开一个文件并且该文件没有退出时,vim 将创建一个名为 newfilename 的新文件。

然而,MacVim 不能以这种方式工作 --- 即 mvim newfilename (alias mvim="open -a macvim") 将导致错误: newfilename 不存在

有没有办法配置 MacVim 使得 mvim newfilename (alias mvim="open -a macvim") 将创建一个新文件并打开它?

When I use vim newfilename to open a file and this file does not exit, vim will create a new file with the name newfilename.

However, MacVim does not work in this way --- i.e. mvim newfilename (alias mvim="open -a macvim") will lead to an error: newfilename does not exist

Is there a way to configure MacVim such that mvim newfilename (alias mvim="open -a macvim") will create a new file and open it?

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

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

发布评论

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

评论(2

清欢 2024-12-12 07:03:11

我猜测错误消息来自 open,而不是来自 vim。您可以用函数替换别名;

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

如有必要,这将在打开文件之前创建空文件。

编辑 没有看到@Peter Lyons对此的评论;应该归功于他首先提出了这个解决方案。如果彼得想提交他的答案,我很乐意删除这个答案。

I'm guessing the error message comes from open, not from vim. You can replace your alias with a function;

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

This will create empty files if necessary before opening them.

edit Didn't see @Peter Lyons' comment about this; credit should go to him for first suggesting this solution. I'll be happy to remove this answer if Peter wants to submit his.

糖粟与秋泊 2024-12-12 07:03:11

您不需要打开命令的 mvim 别名,您可以使用 mvim 启动器脚本。将 mvim 添加到您的路径后,然后运行 ​​mvim newfile ,现在将在新的 MacVim 窗口中打开一个新文件缓冲区,就像 gvim 一样。

上面链接的 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

You don't need the mvim alias to the open command, you can instead use the mvim launcher script that comes bundled with most MacVim Snaphots. After adding that mvim to your path, then runing mvim newfile, will now open a newfile buffer in an new MacVim window just like gvim would.

The MacVim mvim script as linked to above:

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