6

I am trying to map the reverse-search-history (Ctrl+R) command to a different command combination in iTerm but not sure how? Any help would be appreciated.

user1686342
  • 1,265
  • 1
  • 18
  • 24
  • 1
    It has nothing to do with iTerm, this is done by `bash` independently of the terminal emulator. Read the bash documentation on configuring `readline`: https://www.gnu.org/software/bash/manual/html_node/Bindable-Readline-Commands.html#Bindable-Readline-Commands – Barmar Aug 19 '16 at 00:00
  • Why can't iTerm map this key combination the same way it does with other commands? – user1686342 Aug 19 '16 at 00:02
  • The commands aren't bound by iTerm in the first place, they're bound by `bash` and `zsh`. You get the same bindings with `xterm`, `gnome-Terminal`, etc., don't you? – Barmar Aug 19 '16 at 00:05
  • 1
    Please ask for your question to be migrated to SuperUser.com, it's not a programming question. – Barmar Aug 19 '16 at 00:06
  • 1
    You could use BetterTouchTool or `osascript` to make a key combination alias. Also, google `bindkey` or `bind` commands. – theoden8 Aug 19 '16 at 00:20

1 Answers1

5

The person who mentioned readline is correct.

You can edit your bindings using the bind command (try help bind).

For this particular question, let's see what's bound to Control-R:

$ bind -P |grep C-r
re-read-init-file can be found on "\C-x\C-r".
reverse-search-history can be found on "\C-r".
revert-line can be found on "\M-\C-r".

Ok. One of those is just \C-r which means Control-R. Let's double-check:

$ bind -q reverse-search-history
reverse-search-history can be invoked via "\C-r".

man readline includes:

reverse-search-history (C-r) Search backward starting at the current line and moving `up' through the history as necessary. This is an incremental search.

That looks right. How do we change it? Let's assume you want to use ⌘-B instead. That's Meta-b (aka \M-b) in readline-speak.

Let's try it out:

$ bind '\M-b:reverse-search-history'
$ bind -q reverse-search-history
reverse-search-history can be invoked via "\C-r", "\M-b".

Pressing ⌘-b now triggers reverse search just like Control-R. Control-R is still bound though. We can fix that:

$ bind -r '\C-r'
$ bind -q reverse-search-history
reverse-search-history can be invoked via "\M-b".

This change will hold for the current shell session, but will vanish the next time a shell is invoked. To make the change persistent, do the following:

$ echo '"\M-b": reverse-search-history' >> ~/.inputrc

Now ~/.inputrc contains the desired binding. Any program that uses it for readline configuration (including your shell) will now use the binding you specified.

Eric
  • 1,431
  • 13
  • 14