Search code examples
shellfish

Is there any way to come back to a ready-to-enter command in fish shell by just pressing a combination of keys?


Some times that I have a command ready to press enter but that command I have changed it in some way and it's a long command, then I remember that I have to open a text file (e.g. to get some information that I will use in the command). So what most of the times I do, is to cancel that command (Ctrl+C) and then open the text file get the information I need and then retype the command again with the pasted value from the text file. This is not very efficient for me specially if the server doesn't have any kind of GUI and I can't copy the previous command so I don't lose it.

So my question is, Is there any kind of combination keys that I could use to save a command ready to enter so I don't lose it and I don't have to type it all over again?

Thanks!


Solution

  • This is currently not possible out of the box.

    The easiest way to do it is probably to

    • Change the cancel binding to stash the commandline

    • Add a binding to recall the stashed commandline

    It would work something like this:

    The function to recall:

    function recall_commandline
        if set -q stashed_commandline
            commandline -r -- $stashed_commandline
        end
    end
    

    Add to __fish_cancel_commandline (use funced __fish_cancel_commandline. Once you are happy do funcsave __fish_cancel_commandline):

    set -g stashed_commandline $cmd
    # right before:
    commandline ""
    

    Add to fish_user_key_bindings

    bind \cr recall_commandline
    

    This will allow you to press Ctrl+r to recall the last cancelled commandline. Expanding it to multiple is non-trivial (since "commandlines" can have multiple lines), as is adding the commandlines to history so that they can be recalled with the normal bindings.