11

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
    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 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
    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
    Apr 15, 2020 at 10:21

3 Answers 3

19

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

2
  • 2
    Straight forward and very handy, thanks
    – itsmnthn
    Apr 11, 2021 at 7:37
  • I fail to see how this is not infinitely worse that the Page-Up / Page-Down solution in Linux.
    – Maxi Mus
    May 31 at 10:22
4

You may refer to auto suggestion.

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

2
  • Thanks for the inclusion of the zsh-autosuggestions, that was what I was looking for. Aug 22, 2021 at 19:32
  • Finally, I turn to Fish shell. It's really worth a try!
    – yue huang
    Aug 30, 2021 at 3:39
3

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

You must log in to answer this question.

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