vim - 如何阻止Vim创建/打开新文件?

标签 vim vi

当我们给不存在的文件命名时,Vim会创建新文件。这对我来说是不可取的,因为有时我输入了错误的文件名,并且无意打开文件然后将其关闭。

有没有一种方法可以阻止Vim打开新文件?例如,当我执行vi file1时,应该说File doesn't exist并停留在bash终端上(不打开vi窗口)

最佳答案

您可以将此函数添加到您的.bashrc(或等效文件)中。在调用vim之前,它会检查其命令行参数是否存在。如果您确实要创建一个新文件,则可以传递--new覆盖检查。

vim() {
    local args=("$@")
    local new=0

    # Check for `--new'.
    for ((i = 0; i < ${#args[@]}; ++i)); do
        if [[ ${args[$i]} = --new ]]; then
            new=1
            unset args[$i]   # Don't pass `--new' to vim.
        fi
    done

    if ! (( new )); then
        for file in "${args[@]}"; do
            [[ $file = -* ]] && continue   # Ignore options.

            if ! [[ -e $file ]]; then
                printf '%s: cannot access %s: No such file or directory\n' "$FUNCNAME" "$file" >&2
                return 1
            fi
        done
    fi

    # Use `command' to invoke the vim binary rather than this function.
    command "$FUNCNAME" "${args[@]}"
}

关于vim - 如何阻止Vim创建/打开新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073807/

相关文章:

vim - 如何使用与默认 .vimrc 不同的 .vimrc

go - vim-go 找不到需要的库

shell - 如何设置 vimrc 标签以使用 shell 环境变量

vim - 在命令模式下向前/向后移动一个字?

vim - 如何在 VIM 中向 snipMate 添加新片段

Vim:选择文件后保持位置列表窗口打开

vim - vim 中的全局片段

python - 在 Vim 中重新缩进 Python 代码

regex - 使用 vim 正则表达式对数字进行通信

regex - Vim 从位置删除字符