How to implement magic-enter functionality in ble.sh? #490
-
OMZ has a pretty simple Zsh plugin called magic-enter. Magic-Enter detects when you have no command typed and it executes a default one instead. I'm so used to just hitting enter to run I read #420 thinking it would be pretty close and I could figure it out, or I could override |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I haven't checked how the mentioned If you want to replace just Enter, # blerc
function ble/widget/blerc/magic-enter {
if [[ ! $_ble_edit_str ]]; then
ble/widget/execute-command 'git status -sb .'
else
ble/widget/accept-single-line-or-newline # this is the default binding of C-m/RET
fi
}
ble-bind -f C-m blerc/magic-enter # for traditional keyboard protocol
ble-bind -f RET blerc/magic-enter # for advanced keyboard protocol If you want to hook into more places that executes the user's command, # blerc
ble/function#advice around ble/widget/default/accept-line '
if [[ ! $_ble_edit_str ]]; then
ble/widget/execute-command "it status -sb ."
else
ble/function#advice/do
fi
' If you want to automatically input # blerc
function ble/widget/blerc/magic-enter {
if [[ ! $_ble_edit_str ]]; then
ble/widget/insert-string 'git status -sb .'
ble/widget/accept-line
else
ble/widget/accept-single-line-or-newline
fi
}
ble-bind -f 'RET' 'blerc/magic-enter'
ble-bind -f 'C-m' 'blerc/magic-enter' |
Beta Was this translation helpful? Give feedback.
-
This worked perfectly! Here's what I ended up with. This is a fully compatible ble.sh implementation of magic-enter: # Default commands
: ${MAGIC_ENTER_GIT_COMMAND:="git status -sb ."} # run when in a git repository
: ${MAGIC_ENTER_OTHER_COMMAND:="ls ."} # run anywhere else
# This can be changed to handle all manner of magic-commands.
function ble/widget/blerc/magic-enter-command {
# In a git directory, run a git command. Otherwise, run other command.
local magic_cmd
if command git rev-parse --is-inside-work-tree &>/dev/null; then
magic_cmd="$MAGIC_ENTER_GIT_COMMAND"
else
magic_cmd="$MAGIC_ENTER_OTHER_COMMAND"
fi
# Run the command magically.
ble/widget/execute-command "$magic_cmd"
# Or, run the command more explicitly.
# ble/widget/insert-string "$magic_cmd"
# ble/widget/accept-line
}
function ble/widget/blerc/magic-enter {
# If no command is given, run magic-enter-command. Otherwise, run the
# ble.sh default binding for C-m/RET.
if [[ ! $_ble_edit_str ]]; then
ble/widget/blerc/magic-enter-command
else
ble/widget/accept-single-line-or-newline
fi
}
ble-bind -f C-m blerc/magic-enter # for traditional keyboard protocol
ble-bind -f RET blerc/magic-enter # for advanced keyboard protocol Thank you so much!! One quick follow up question - I don't understand what you meant by the middle example: "if you want to hook into more places that executes the user's command...". Any clarification/tips on how to learn more about |
Beta Was this translation helpful? Give feedback.
I haven't checked how the mentioned
magic-enter
precisely behaves, but here are examples:If you want to replace just Enter,
If you want to hook into more places that executes the user's command,