2

I found this very useful tip in UsingTheTerminal to use the arrow keys to search through the command history:

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

How can I change it in order to use Ctrl+P and Ctrl+N instead of the arrow keys?

  • You could also use $ history [numberOfCommandsToReturn] if you just don't want to use the arrow keys. – LittleByBlue Jul 31 '14 at 20:06
  • 2
    Use bind command with selected option. For example for Crtl+N use bind '"\C-n": history-search-backward' – Nykakin Jul 31 '14 at 20:38
  • That worked. Do I have to copy it into my bashrc to make it permanent? – Michael Jul 31 '14 at 21:00
  • Yes, it needs to be placed in .bashrc or it will affect only current session. – Nykakin Jul 31 '14 at 21:29
2

Repeating what I said in comment section to not leave question hanging without answer, in order to bind one of Bash readline commands you can use bind command. For example, Crtl+N shortcut can be used with:

$ bind '"\C-n": history-search-backward'

This will be limited to current session and needs to be placed inside .bashrc to have it established in every one.

Expanding my answer, you can also modify ~/.inputrc file, as mentioned in link provided in question. There are two ways to do this. First syntax for same shortcut as above is:

"\C-n": history-search-backward

And the second one is:

Control-n: history-search-backward

This second type of syntax can be used with bind as well:

$ bind 'Control-n: history-search-backward'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.