Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active February 7, 2019 20:40
Show Gist options
  • Save ddrscott/63505a15f9cf9a1209aba33b3a12677e to your computer and use it in GitHub Desktop.
Save ddrscott/63505a15f9cf9a1209aba33b3a12677e to your computer and use it in GitHub Desktop.
Bash bind example
# Based on:
# - https://unix.stackexchange.com/questions/82630/put-text-in-the-bash-command-line-buffer/82716#82716
# - https://github.com/junegunn/fzf/blob/master/shell/key-bindings.bash
hack() {
if [ $((RANDOM % 10)) -eq 0 ]; then
# Run an interesting in the background:
(/full/path/to/command &)
fi
local selected="e"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}
# Only Bash 4 and higher supports READLINE_LINE
if [ $BASH_VERSINFO -gt 3 ]; then
bind -x '"e": hack'
fi
@ddrscott
Copy link
Author

ddrscott commented Feb 7, 2019

This should be a template to make e passthrough the hack() function without a new line, but for some reason, a new line is emitted which spoils the effect.

@ddrscott
Copy link
Author

ddrscott commented Feb 7, 2019

source hack.bash to enable the feature. I'd recommend trying it in a new shell. So e doesn't get messed up. Also, you can still exit the shell with Ctrl-D since e will be broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment