Search code examples
bashzshfish

In shell, is there a way to run a default command when you just press enter without entering anything


I am thinking it would be cool to have some things run when you just press enter on command line without entering anything. It is kind of unusual thing to do so I am not sure where to look and how to even approach it.

Do you have any idea how I could do something like that? Maybe run something in prompt... but only when there is nothing entered.

Hope someone has an idea either yes or know. I personally use fish, but I would be curious in any of the mainstream shells, bash, zsh.

Thank you!

P.S. Alternatively, is there a way to make a facility like ctrl-R where you enter search string. Can you add a hook to ctrl-something?


Solution

  • In fish, you can do this by setting a custom binding for return, which modifies the command line if it's empty:

    function replace_command
        string length --quiet (commandline)
        or commandline "echo hello world"
        commandline -f execute
    end
    
    bind \n replace_command
    bind \r replace_command
    

    This will run echo hello world if you press return with an empty command line.

    (Look up fish_user_key_bindings if you aren't sure where the bind statements should go).