1

Say I had the following commands typed into Bash during a session:

nano file1.txt
nano file2.txt
netcat
ls
pwd

I know I can move through these by hitting the up arrow and then hit enter to run the command selected, but if I want to get back to the "nano" commands I have to sc

Is there a key combination where I can start typing the command, then hit some keys, and only commands that begin with what has already been written show up to cycle through?

1
  • 1
    Oh-my-bash may be useful for you. It has a builtin plugin that does exactly what you want. Install it and it should work out of the box.
    – Parker
    Jul 26, 2022 at 18:12

2 Answers 2

2

The correct way of searching using what is already on your command line is to move your cursor to the beginning of the line with CTRL + A, call the reverse history with CTRL + R, paste the current line into the search with CTRL + Y, and then using the CTRL + R again to search in reverse.

2
  • Or if you already know before starting to type the line that that's what you're going to do anyway, simply hit Ctrl-R on the empty line already and go ahead.
    – DonHolgo
    Jul 26, 2022 at 18:32
  • 1
    Thanks! I was hoping it was something a bit simpler like one or two keystrokes but that will do Jul 26, 2022 at 18:40
0

You can use bang notation

!nan<Enter>
!?ano<Enter>

These would, respectively, find and execute the most recent command that begins with the string specified, or contains the string.

You can also append :p to print the command in case you aren't sure, it ends up as the previous command on your history, so just hit up and enter to select it.

You must log in to answer this question.

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