首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MacVim:命令行使用` `alias mvim=“macvim -a macvim"`创建新文件

MacVim:命令行使用` `alias mvim=“macvim -a macvim"`创建新文件
EN

Stack Overflow用户
提问于 2011-09-18 08:31:56
回答 2查看 3.5K关注 0票数 6

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

然而,MacVim不是这样工作的-即mvim newfilename (alias mvim="open -a macvim")将导致一个错误:newfilename does not exist

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-18 17:00:05

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

代码语言:javascript
复制
mvim () {
    local f
    for f; do
        test -e "$f" || touch "$f"
    done
    open -a macvim "$@"
}

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

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

票数 7
EN

Stack Overflow用户

发布于 2012-04-26 23:44:07

您不需要为open命令指定mvim别名,而是可以使用大多数MacVim快照附带的mvim launcher script。在将mvim添加到您的路径后,然后运行mvim newfile,现在将在一个新的MacVim窗口中打开一个新的文件缓冲区,就像gvim一样。

链接到上面的MacVim mvim脚本:

代码语言:javascript
复制
#!/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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7458760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档