如何为没有扩展名的文件创建后缀别名?

How to create a suffix alias for files without an extension?

我刚刚发现 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

后缀别名需要文件扩展名。不过,您可以使用 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  ]]; then
        nano ""
    else
        print -u2 -f "command not found: %s\n" ""
        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

根据以上答案:

和vim隐藏:

command_not_found_handler() {
    if [[ $# -eq 1 && -f  ]]; then
        echo "leo test________________"
        vim ""
    else
        print -u2 -f "command not found: %s\n" ""
              # -u n   Print the arguments to file descriptor n.
                 # 2 : stderr
        return 127  # return an error.
    fi
    echo "aaaaaaaaaa  leo test________________"
}

# 名字不能改
#

# (Nearly) All about this from  man zshmisc (modified by me for easier understanding) 
#     If a command name contains no ✌/✌
#         the shell attempts to locate it:
            # If there exists a shell function by that name,
    #                 the function is invoked as described in the section `Func‐ tions'.
    #         If there exists a shell ✌builtin✌ by that name,
#                     the builtin is invoked.
    #
#     contains  ✌/✌
#         the shell searches for a ✌directory✌  containing an executable file
#                   searche  ✌by that name✌.
                  searches # in each element of $path

    #
    #     if the search is successful
        #     if execution ✌fails✌ because  the file is not in ✌executable format✌,
        #     and the file is ✌not a directory✌,
           #     it is assumed to be a shell script.
           #     /bin/sh is ✌spawned✌ to execute it.

            #     If the program is   a file beginning with `#!',
                #     the remainder of the first line specifies an interpreter for the program.
                #     The shell will execute the specified interpreter,
                #     which do not handle this executable format in the ✌kernel✌ ,
                #     on  ✌operating systems✌
    #
    #         else (the search fails)
        #         the shell prints an error message and returns a nonzero exit status.
    #
    #     If no external command is found
#         but a function ✌command_not_found_handler✌ exists
    #         the shell executes this function with all command line arguments.
    #         The return status of the function becomes the status of the command.
    #
    #         If the function wishes to mimic the behaviour of the shell when the command is not found,
    #             it should print the message `command not found: cmd'
    #             to standard error and return status 127.
    #
    #         Note that the handler is executed in a subshell forked to execute an external command,
    #         hence changes to directories,
                    #         shell parame‐ ters,
    #         have no effect on the main shel
    #

# vim:ft=zsh