使用Command Not Found Magic中command-not-found
指定的Ubuntu 挂钩。当前用于建议安装软件包。请参阅应在系统上安装哪个。/usr/share/doc/command-not-found/README
更好的是,因为它不依赖于command-not-found
软件包,所以(重新)实现内置的Bash command_not_found_handle
来创建xdg-open
if $1
是现有文件,并将所有其他情况委托给先前的实现。
# Save the existing code for the handler as prev_command_not_found_handle.
# Bit of a hack, as we need to work around bash's lack of lexical closure,
# and cover the case when it is not defined at all.
eval "prev_$(declare -f command_not_found_handle)" >& /dev/null \
|| prev_command_not_found_handle () {
echo "$1: command not found" 1>&2
return 127
}
# Define the new implementation, delegating to prev_handler.
command_not_found_handle () {
if [ -f "$1" ]; then
xdg-open "$1"
else
prev_command_not_found_handle "$@"
fi
}
好问题,漂亮的功能。
再想一想:除非您同时扩展bash_completion
处理程序,否则您可能不会像您想的那样喜欢该功能。想象一下要打开file-with-a-long-name.txt
,然后设置
alias o='xdg-open'
将使(大约)四次按键就足够了:
o f<Tab><Enter>
键入完整的文件名需要花费乏味的26-并且排除了不可避免的输入错误的退格。
command_not_found_handle()
功能/etc/bash.bashrc
以检查并xdg打开同一目录中的文件吗?