14

In the readline manual

# man readline

The following describes how to search backward and forward.

reverse-search-history (C-r)
Search backward starting at the current line and moving `up' through the history as necessary.  This is an incremental search.

forward-search-history (C-s)
Search forward starting at the current line and moving `down' through the  history  as  necessary.   This  is  an  incremental search.

When I hit ctrl+r and type "apt-get", I'm able to do a reverse-search-history by continually tapping ctrl+r. But then I tap ctrl+s and the terminal does not go forwards. Am I doing something incorrect?

Also what key does the Meta prefix "M-" represent?

7 Answers 7

17

The sequence C-s is taken from the terminal driver, as you can see from

stty -a | grep '\^S'

To free up the sequence for use by readline, set the stop terminal sequence to some other sequence, as for example

stty stop ^J

or remove it altogether with

stty stop undef

After that C-s would work in the given terminal.

Set it in ~/.bashrc to make it work in every terminal.

The M- sequence means the Alt key, as already noted.

1
  • ^J is a good choice since for stop, it's not used for anything else
    – hobs
    Apr 2, 2013 at 19:56
3

In addition to assigning a different TTY 'stop' char as suggested above, you can remove it entirely if you don't plan to use it (I tend not to), like this:

$ stty stop undef

Then readline can use control-s for forward search and you won't get any strange behavior due to a new stop char being added.

0
1

forward-search-history (C-s) doesn't work (though it is in the man page).

'M-' represents the Alt key modifier.

1
  • 2
    Not correct the C-s part, see my answer.
    – enzotib
    Sep 5, 2011 at 12:45
1

To switch forward when using reverse search (with Ctrl-R command):

  1. Open your .bashrc file:

    sudo gedit ~/.bashrc
    
  2. add this line (the letter "f" can be replaced with another not yet used by the system)

    bind '"\C-f": forward-search-history'
    
  3. Close your file and update your .bashrc (or open another terminal) with the command:

    source ~/.bashrc
    

    or

    . ~/.bashrc
    

Now you can move forward in the history with the shortcut Ctrl+f

Working on Ubuntu 18.04 LTS

0

Please do not use:

stty stop ^J

but

stty stop ^P

or anything else, as ^J makes big problems with ssh. (I do not know why anyway.) After I logged in Enter and backslash were not working. or I had to type:

stty sane 
export TERM=linux

before I wanted to type

ssh root@192...

so

stty stop ^P

solved both problems at the same time.

0

You can disable XON/XOFF flow control:

stty -ixon

in your ~/.profile or similar, then Ctrl+S will be recognized.

Example

0

Note that I had used

stty stop ^J
stty -ixoff

in .bashrc to enable forward search and disable the annoying "Output has been suspended by pressing Ctrl+S. Press Ctrl+Q to resume" message in Konsole when ctrl+s was pressed, but both commands individually broke the Konsole integration in Dolphin (the "F4" shortcut) in the way that I had to do ctrl+c two times to get it to even work, and the automatic "cd" commands where not being injected when graphically navigating.

So I now prefer:

stty stop undef
stty -ixon

It has the same effect, an all seems to work as expected ;)

You must log in to answer this question.

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