如何为没有扩展名的文件创建后缀别名? [英] How to create a suffix alias for files without an extension?

查看:49
本文介绍了如何为没有扩展名的文件创建后缀别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚找到了关于 zsh 后缀别名 允许将程序与 shell 中的扩展相关联:

I just found about zsh suffix aliases that allow one to associate a program with an extension in the shell:

alias -s {md,txt}=nano

对于没有扩展名的文件,有没有办法做这样的事情?

Is there a way to do something like this but for file that do not have an extension?

我试过了:

alias -s {}=nano

但是如果我尝试使用它,我会收到一个 command not found 错误:

But if I then try to use it, I get a command not found error:

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

推荐答案

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

Suffix aliases require a filename extension. You can use a command_not_found_handler function to work around that, though:

# 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

这篇关于如何为没有扩展名的文件创建后缀别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆