【问题标题】:How to create a suffix alias for files without an extension?如何为没有扩展名的文件创建后缀别名?
【发布时间】:2021-07-03 17:16:23
【问题描述】:

我刚刚发现 zsh suffix aliases 允许将程序与 shell 中的扩展相关联:

alias -s {md,txt}=nano

除了没有扩展名的文件之外,有没有办法做这样的事情?

我试过了:

alias -s {}=nano

但如果我随后尝试使用它,我会收到 command not found 错误:

> alias -s {}=nano
> touch file_without_extension
> file_without_extension
zsh: command not found: file_without_extension

【问题讨论】:

  • 根据文档,似乎需要扩展。

标签: macos zsh oh-my-zsh


【解决方案1】:

后缀别名需要文件扩展名。不过,您可以使用 command_not_found_handler 函数来解决这个问题:

# Run this from a zsh prompt or put it in a file and source it
command_not_found_handler() {
    # If just the name of an existing file is given, with no extra arguments
    # open it in nano. Otherwise, print a message to stderr and return an error.
    if [[ $# -eq 1 && -f $1 ]]; then
        nano "$1"
    else
        print -u2 -f "command not found: %s\n" "$1"
        return 127
    fi
}
# Then with the function loaded:
$ file_without_extension # Opens in nano
$ file_that_doesnt_exist
command not found: file_that_doesnt_exist
$ file_without_extension blah
command not found: file_without_extension

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2023-02-02
    • 2016-12-24
    • 2013-11-03
    • 2013-05-07
    • 1970-01-01
    相关资源
    最近更新 更多