I keep fairly long bash history and sometimes when I search for something with bash reverse search function(Ctrl+r) I end up at the beginning of search history and I still did not find what I needed and then I would like to move back to the end of the history file. One option is to use forward search function(Ctrl+s) which moves closer to the end of the history or execute #
, but is there also a keyboard shortcut to move directly to the end of bash history?
1 Answer
There is the readline function end-of-history
,
by default mapped to M->,
but if used during reverse searching it exits the reverse search prompt.
A possible workaround / trick
Start string search
Instead, to use reverse search, you could use history-search-backward
and history-search-forward
(default unmapped),
mapping them to up/down (very useful) in your .inputrc
:
# up-down arrow to search in history
"\e[A":history-search-backward
"\e[B":history-search-forward
So, after you have typed
you could reverse search pressing up
(instead of Ctrl+R),
and when you want to return to the end of the history you can use the end-of-history
function by pressing (M->)
This way "The search string must match at the beginning of a history line,"
so, if you search for ls -ltr
,
you have to type ls
and then press the up key.
Substring search
if you want "The search string may match anywhere in a history line,"
you have to use history-substring-search-forward
and history-substring-search-backward
:
# up-down arrow to search in history
"\e[A":history-substring-search-backward
"\e[B":history-substring-search-forward
This way, if you search for ls -ltr
, you can type ls
, but also ltr
,
before you press up.
Reference: Bash Reference Manual – Commands For History.
history
will show you all bash commands made..