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

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?

I've tried:

alias -s {}=nano

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


Solution 1:[1]

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

Solution 2:[2]

base on the above answer:

with vim conceal:

enter image description here

command_not_found_handler() {
    if [[ $# -eq 1 && -f $1 ]]; then
        echo "leo test________________"
        vim "$1"
    else
        print -u2 -f "command not found: %s\n" "$1"
              # -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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shawn
Solution 2 Good Pen