17

In Linux, I had managed to set up the terminal in such a way that if I type part of a command and press the arrow key 'up', it would auto-complete with the last command that started with what I had typed.

I tried to do the same on MacOS following the instructions on this MacWorld article, but it doesn't seem to work.

Does anyone know how to set this up on a Mac?

4
  • 1
    Related: Command-line completion from command history
    – klanomath
    Commented Jun 22, 2017 at 9:25
  • 2
    It's better to use ctrl+r to search your command history, that way you don't have to even remember the command name. It has sped up my terminal usage by many times. Highly recommended. Plus, I recommend the Z Shell and: ohmyz.sh Commented Jun 22, 2017 at 10:05
  • In the end I installed Fish shell, which autocompletes from the history as you type, so you only have to press the right arrow to complete the command.
    – Kurt Peek
    Commented Jun 23, 2017 at 11:07
  • When asking terminal or shell questions, please provide your shell type (Bash, Zsh, Fish) since they behave differently.
    – hc_dev
    Commented Apr 15, 2020 at 10:21

4 Answers 4

27

You may use reverse-search. Just press ctrl+R and start typing your command, and then just press Enter.

1
  • 3
    I fail to see how this is not infinitely worse that the Page-Up / Page-Down solution in Linux.
    – Maxi Mus
    Commented May 31, 2022 at 10:22
7

Bind key to history-search

For searching lines starting with the typed, as you read in Macworld article, you can add the keybindings, for example:

  • Up key bound to history-search-backward
  • Down key bound to history-search-forward

To add them permanent edit your ~/.inputrc like this

"\e[A": history-search-backward
"\e[B": history-search-forward 

Same can be achieved using the bind command in your ~/.bashrc by pasting above lines within single-quotes (') as argument:

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

Note: for both settings to take effect you have to restart terminal !

Alternative: use built-in history search shortcuts

You can use Reverse History Search in Bash as explained in the question Searching your command history on macOS terminal.

Following shortcuts allow to search parts of commands in your Bash command history:

  • Ctrl + r backwards
  • Ctrl + s forwards

Tag typed commands for easy search later

Wouldn't above reverse-search be useful if you can set easy to remember keywords?

To leverage partial history search you can also append commands with comments, e.g. typing a command for later use:

ls -l *.sh # list scripts

The added comment (starting with #) allows to search for list scripts since the comment is recorded in history.

1
5

You may refer to auto suggestion.

And for zsh user, zsh-autosuggestions will be very helpful.

4
  • Thanks for the inclusion of the zsh-autosuggestions, that was what I was looking for. Commented Aug 22, 2021 at 19:32
  • Finally, I turn to Fish shell. It's really worth a try!
    – yue huang
    Commented Aug 30, 2021 at 3:39
  • Will EVERY bash script run flawlessly on Fish shell? Commented Jan 12, 2023 at 20:36
  • For ZSH style suggestions you can use ble.sh, see superuser.com/a/1644033/322610 Commented Jan 12, 2023 at 20:37
2

In order to get up/down arrow history search behavior to work on MacOS Monterey, I had to put the bindings in my .zshrc file.

I used this flavor of history-search behavior:

bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward

It will match command strings exactly, even after a space is typed. See this answer for more discussion.

2
  • What is the "this" you are referring to in the first sentence? If your answer doesn't stand on its own but is primarily an extension of an existing one, an edit of that answer might be more useful.
    – nohillside
    Commented Apr 4, 2023 at 15:09
  • 1
    Thanks, I've edited my answer to clarify that "this" refers to the behavior being sought in the original question.
    – Stew
    Commented Apr 4, 2023 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .