17

In Terminal when we press Ctrl+R, we can search through the command history. However, if we do not find what we want, how can we go back to the directory prompt, and not have any of the search results displayed?

1
  • I guess one workaround is to Alt+P Backspace
    – PRATAP
    Nov 29, 2019 at 17:09

3 Answers 3

18

You can use the combination Ctrl+G.

Excerpt of man readline:

SEARCHING Readline provides commands for searching through the command history for lines containing a specified string. There are two search modes: incremental and non-incremental.

Incremental searches begin before the user has finished typing the search string. As each character of the search string is typed, readline displays the next entry from the history matching the string typed so far. An incremental search requires only as many characters as needed to find the desired history entry. To search backward in the history for a particular string, type C-r. Typing C-s searches forward through the history. The characters present in the value of the isearch-terminators variable are used to terminate an incremental search. If that variable has not been assigned a value the Escape and C-J characters will terminate an incremental search. C-G will abort an incremental search and restore the original line. When the search is terminated, the history entry containing the search string becomes the current line.

To find other matching entries in the history list, type C-s or C-r as appropriate. This will search backward or forward in the history for the next line matching the search string typed so far. Any other key sequence bound to a readline command will terminate the search and execute that command. For instance, a newline will terminate the search and accept the line, thereby executing the command from the history list. A movement command will terminate the search, make the last line found the current line, and begin editing.

Non-incremental searches read the entire search string before starting to search for matching history lines. The search string may be typed by the user or be part of the contents of the current line.

2
  • Thanks! I learned something today :)
    – popey
    Nov 29, 2019 at 21:01
  • Ctrl + C also aborts the search and returns the original line(For experience, dunno about documentation)
    – Tejas Kale
    Nov 30, 2019 at 12:27
3

You can use the Esc key or Ctrl+C. Both will get you out of the search command.
The difference is that Esc will keep the last searched command, but Ctrl+C will exit the command completely.

2

Exit from history search with ESC

1
  • 8
    "When the search is terminated, the history entry containing the search string becomes the current line." It means that if there's one result, although not the one is being looking for, pressing ESC will leave that result in the screen. That's not what OP wants. Nov 29, 2019 at 19:25

You must log in to answer this question.

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