Comments on From XON/XOFF to Forward Incremental Search

Post Comment

Loomx said:

As an alternative, use ctrl+f for "forward-inc-search"; it's easier to remember (mnemonic) and means you can still use ctrl+s or ctrl+q for flow control as normal. For example, in Bash:

bind '"\C-f": forward-search-history'
14 Aug 2022 07:33 GMT (#1 of 6 comments)

Peter said:

# bash: a small cantrip to disable ixon before readline and
# turning it on again afterwards during command execution
# (assuming trap DEBUG not in use)
#
# inspired by and to extend the article on ixon on
# susam.net/blog/from-xon-xoff-to-forward-incremental-search.html

unset _IXON
# before readline, issue stty -ixon to permit use of ^s for
# incremental search, then reenable it for the next command
PROMPT_COMMAND="$PROMPT_COMMAND; : _IXONOFF"
function _TRAP_DEBUG {
   if [ "$BASH_COMMAND" = ": _IXONOFF" ]; then
      # about to enter readline, thus turn off ixon
      stty -ixon
      # and reenable it for the command from readline
      _IXON=1
   elif [ "$_IXON" = "1" ]; then
      # readline finished, we are about to run its input as command
      stty ixon
      unset _IXON
   fi
}
trap _TRAP_DEBUG DEBUG

echo "test it by ^s/^q the output during the run of the next command"
echo "(use ^c to end)"
find / -xdev 2>/dev/null

echo test ^s incremental search during readline:
: ...

: enjoy,
: Peter
14 Aug 2022 16:47 GMT (#2 of 6 comments)

Peter said:

With respect to the cantrip, the below line in ~/.inputrc might be preferable, as it remaps (forward) i-search to ctrl+^ for all programs using GNU-readline.

"\C-^": forward-search-history

And if you edit ~/.inputrc, consider adding a mapping for kill-region, in my case using an en-US keymap, its esc ctrl+@, and permits to move/copy/erase character regions together with ctrl+y and ctrl+x ctrl+x.

"\e\C-@": kill-region

'nough said,
Peter

14 Aug 2022 17:03 GMT (#3 of 6 comments)

Susam Pal said:

Thank you, Peter, for your comments. It took a few changes to get your Bash script to work properly in my shell. Since I do not have PROMPT_COMMAND set in my shell, the evaluation of the following prompt command

PROMPT_COMMAND="$PROMPT_COMMAND; : _IXONOFF"

was causing this error:

bash: PROMPT_COMMAND: line 0: syntax error near unexpected token `;'
bash: PROMPT_COMMAND: line 0: `; : _IXONOFF'

Here is how I modified your script to check if PROMPT_COMMAND is set before expanding it. If it is not set we use the null command : before the semicolon separator.

unset _IXON
PROMPT_COMMAND="${PROMPT_COMMAND:-:}; : _IXONOFF"
function _TRAP_DEBUG {
   if [ "$BASH_COMMAND" = ": _IXONOFF" ]; then
      stty -ixon
      _IXON=1
   elif [ "$_IXON" = "1" ]; then
      stty ixon
      unset _IXON
   fi
}
trap _TRAP_DEBUG DEBUG

To test this, we can add the above script to ~/.bashrc, then run something like ping localhost and confirm that ctrl+s pauses the output, whereas in the shell ctrl+s performs forward incremental search.

25 Sep 2022 10:52 GMT (#4 of 6 comments)

KDK said:

This was a very interesting read! I have personally solved this by using fuzzy finder, which gives a menu/search box with a selection instead of the simplistic string matching of ctrl+r.

If you are a heavy terminal user, I would recommend looking into https://wiki.archlinux.org/title/fzf. I see the built-in search being especially useful on remote/shared machines, where you do not necessarily want to install stuff.

The XON/XOFF feature actually seems quite useful for terminals without scrollback. Or for when you don't have a mouse. Hopefully I'll remember it. Thank you very much for your article!

14 Jan 2023 23:30 GMT (#5 of 6 comments)

Orev said:

This is a great explanation, especially the part describing how control codes are mapped to their corresponding letters. I always felt there was probably some logic to it, but the idea never fully materialized into something I thought to investigate. Learning about how to disable XON/OFF is more like a bonus.

08 May 2023 00:34 GMT (#6 of 6 comments)
Post Comment