fish-shell icon indicating copy to clipboard operation
fish-shell copied to clipboard

Allow combining multiple commands from history

Open a-m-s opened this issue 2 years ago • 2 comments

I frequently find myself reusing sequences of commands over and over, with maybe only minor edits. In bash I'd probably use the Ctrl-O feature to rapidly repeat history, but that's not a fish thing. Instead I either do "up-up-up-enter", repeatedly, or I get all my commands and stick them together with &&.

$ a
$ b
$ c
$ a && b && c

It would be nice if fish had some way to assist me with this. Right now I can use "up" to select the first command, but then I have to cut and paste with the mouse to join the others.

Perhaps something like a && <shift-up> would allow me to add a command from history to the end of the line? This would be a bit like alt-up, but for whole commands.

The same trick would be handy for ;, &, ||, and, or, not, etc. (maybe pipe?) Perhaps it could just work without any context?

a-m-s avatar Apr 25 '23 15:04 a-m-s

I built this feature myself, based on the fzf.fish plugin.

https://github.com/PatrickF1/fzf.fish

function fzf_search_history_insert --description 'Search command history, starting with an empty query. Insert an old command at the cursor. Adapted from _fzf_search_history'
    # history merge incorporates history changes from other fish sessions
    builtin history merge

    set command_with_ts (
        # Reference https://devhints.io/strftime to understand strftime format symbols
        builtin history --null --show-time="%m-%d %H:%M:%S │ " |
        _fzf_wrapper --read0 \
            --tiebreak=index \
            # preview current command using fish_ident in a window at the bottom 3 lines tall
            --preview="echo -- {4..} | fish_indent --ansi" \
            --preview-window="bottom:3:wrap" \
            $fzf_history_opts |
        string collect
    )

    if test $status -eq 0
        set command_selected (string split --max 1 " │ " $command_with_ts)[2]
        commandline --insert -- $command_selected
    end

    commandline --function repaint

end

And then bind it to something you like:

bind \er fzf_search_history_insert

bagohart avatar Apr 27 '23 19:04 bagohart

This is a pretty handy little script! Thanks for sharing. 👍

a-m-s avatar Apr 28 '23 08:04 a-m-s

232483d89a4c6d0d046cdf8f5d1acec204b53a12 implements something for this - ctrl-r only operates on the current line, so you can use it to search through your history and combine lines.

faho avatar May 09 '24 17:05 faho