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?
Bash – move back to the end of bash history when using reverse search
bashcommand history
Best 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
andhistory-search-forward
(default unmapped), mapping them to up/down (very useful) in your.inputrc
: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 typels
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
andhistory-substring-search-backward
:This way, if you search for
ls -ltr
, you can typels
, but alsoltr
, before you press up.Reference: Bash Reference Manual – Commands For History.