VaTo
VaTo

Reputation: 3078

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!

Upvotes: 2

Views: 187

Answers (2)

Kurtis Rader
Kurtis Rader

Reputation: 7459

I have the following function to turn comment/uncomment the current statement:

function toggle-comment-cmd-buffer --description 'Comment/Uncomment the current or every line'
    set -l cmdlines (commandline -b)
    if test "$cmdlines" = ""
        return
    end
    set -l cmdlines (printf '%s\n' '#'$cmdlines | string replace -r '^##' '')
    commandline -r $cmdlines
    string match -q '#*' $cmdlines[1]; and commandline -f execute
end

I bind it thusly: bind \e\# toggle-comment-cmd-buffer. This way I can quickly comment and put the current statement in my command history in order to do something else. Then I can recall the comment and press [alt-#] again to remove the comment characters and continue modifying the command.

I set this up in my personal fish config because I had gotten used to doing something similar in ksh93.

Upvotes: 1

faho
faho

Reputation: 15924

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.

Upvotes: 1

Related Questions