Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add entry to fish shell history?

Tags:

fish

In my fish function, I'm evaluating constructed commandline via eval (commandline), specifically - I'm looking for some file name from fzf, and then analyze if commandline was prepended with vim. If it was - instead of returning vim filename to press enter afterwards, I just evaluate it, as I showed before.

The thing is that if I evaluate it, instead of pressing enter manually, it is not going to history - e.g. I can not see it as previous command by pressing up.

I tried set -x history (commandline) $history after eval, but it shows me an error set: Tried to change the read-only variable “history”

Is there a way to manually add custom string (in my case commandline buffer) to history? thx.

like image 708
sandric Avatar asked Jun 12 '15 23:06

sandric


People also ask

How do I customize my fish shell prompt?

Prompt Tab The "prompt" tab displays the contents of the current fish shell prompt. It allows selection from 17 predefined prompts. To change the prompt, select one and press "Prompt Set!".

Where is fish shell history stored?

Customizing the name of the history file By default interactive commands are logged to $XDG_DATA_HOME/fish/fish_history (typically ~/. local/share/fish/fish_history ). You can set the fish_history variable to another name for the current shell session.

How do you put a path on fish shell?

It does this by adding the components either to $fish_user_paths or directly to $PATH (if the --path switch is given). It is (by default) safe to use fish_add_path in config. fish, or it can be used once, interactively, and the paths will stay in future because of universal variables.

How do I change my fish shell greeting?

When an interactive fish starts, it executes fish_greeting and displays its output. The default fish_greeting is a function that prints a variable of the same name ( $fish_greeting ), so you can also just change that if you just want to change the text. While you could also just put echo calls into config.


1 Answers

history --merge does not merge history in chronological order #2312. So even if we manually add the command to ~/.config/fish/fish_history we will no be able to simply press up to see it (although you will get to it eventually if you press up enough).

To get around this we can make a copy of fish_history then call builtin history --clear which will clear both fish's internal history and the history file. We then restore the history file from our copy, append our command and call history --merge to merge the history file with the now empty internal history.

function evalh
    eval $argv

    # backup history file
    cp ~/.config/fish/fish_history /tmp/fish_history.tmp

    # clear internal history and history file (using builtin means we don't get a prompt)
    builtin history --clear

    # restore history file
    cp /tmp/fish_history.tmp ~/.config/fish/fish_history

    # append our command 
    echo "- cmd:" $argv >> ~/.config/fish/fish_history
    echo "  when:" (date "+%s") >> ~/.config/fish/fish_history

    # merge history file with (empty) internal history
    history --merge
end
like image 172
Brett Y Avatar answered Nov 10 '22 09:11

Brett Y