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 4 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 4 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 4 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 4 comments)
Post Comment