Up-arrow previous command completion in Bash

I got addicted to the up-arrow auto-completion of the previous commands that is available in the Julia REPL. Is there a simple way to reproduce that in Bash?

5 Likes

zsh maybe?

6 Likes

Had never heard of it before, but I installed it here and, with default configuration files, it does not have that. Also I am not sure if I am willing to change the shell completely. A solution withing bash would be better.

I too got addicted to that feature in the Julia REPL. This is what I added to my zsh config to enable it:

autoload -U history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end

# Search entered text and move cursor to end (this matches the Julia
# REPL behavior re: cursor position)
bindkey '^[OA' history-beginning-search-backward-end
bindkey '^[OB' history-beginning-search-forward-end
# alternately, search entered text and leave cursor at current position
bindkey '^[OA' history-beginning-search-backward
bindkey '^[OB' history-beginning-search-forward

5 Likes

fish does this by default.

As a rule: OhMyZsh can do anything fish can in zsh
though i don’t know particularly how.

4 Likes

If on Mac OS, use iTerm2

2 Likes

In regular bash, you should be able to add the following to your ~/.inputrc file:

## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
8 Likes

Almost there, using your answer I found that adding this to the ~/.bashrc file works:

# ~/.bashrc
if [[ $- == *i* ]]
then
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
fi

From here. (I never had an ~/.inputrc file, so I was willing to modify the ~/.bashrc).

3 Likes

That’s what I do, but it doesn’t have the killer addition of preserving your spot in history after grabbing a line of code. E.g., in Julia, you can type:

julia> f() = 1
f (generic function with 1 method)

julia> g() = 2
g (generic function with 1 method)

julia> f

Then to get f() = 1 on the line, to execute it, then the magic is that at the next julia> prompt, will grab g() = 2.

Do zsh or fish do that? Is there magic to bring that into bash?

5 Likes

Fish does not do that.
Down on a new prompt does nothing on fish right now.
I have added this to an issue there
https://github.com/fish-shell/fish-shell/issues/7813

1 Like

I like this functionality, but I’ve had a problem for a long time where entries seem to disappear from the history (in the Julia REPL). It’s quite hard to reproduce, it’s just that at random intervals, if I navigate up and down with the arrows and press enter, in some unclear pattern, suddenly the latest entry becomes inaccessible. Then I have to select, copy and paste it, even though it is still visible in the REPL window.

I second fish. Almost no configuration needed and if their syntax is in the way, then just call bash. For the backward search, install fzf for fish (like I said, almost no configuration needed :stuck_out_tongue_closed_eyes:)

The REPL history actually stores your edits to it if you don’t execute it. This is handy in case you accidentally press up or down while editing because you don’t lose your work if you go back to it, but I agree it’s caught me out in the past, too.

The Julia REPL is the ZF1 of REPLs.

5 Likes