我知道xdg-open
会从终端打开用户首选应用程序中的文件,如下所示:
xdg-open filename
但是我想知道如何仅通过键入以下内容从默认目录中的当前目录中打开文件:
filename
其次是Enter,当然。而已。
使用Command Not Found Magic中command-not-found
指定的Ubuntu钩子。当前用于建议安装软件包。请参阅应在系统上安装哪个。/usr/share/doc/command-not-found/README
更好的是,因为它不依赖于command-not-found
软件包,所以(重新)实现内置的Bashcommand_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-并排除了不可避免的输入错误的退格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句