当我们给不存在的文件命名时,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/