ZSH: how to ZLE widgets directly?

You can execute widget by the widget execute-named-cmd, which is bound to ESC-x (emacs bindings) or : (vi bindings):

execute-named-cmd (ESC-x) (:) (unbound)

Read the name of an editor command and execute it.

This opens up a mini-buffer below the command line, where you can start zle widgets. (Autocompletion is available!):

$ [ESC-x]
execute: set-[TAB]
set-local-history  set-mark-command

To query the state of zle (including local history), use the variable $ZLE_STATE (only accessible inside widget functions):

ZLE_STATE (scalar)

Contains a set of space-separated words that describe the current zle state.

Currently, the states shown are the insert mode as set by the overwrite-mode or vi-replace widgets and whether history commands will visit imported entries as controlled by the set-local-history widget. The string contains insert if characters to be inserted on the command line move existing characters to the right or overwrite if characters to be inserted overwrite existing characters. It contains localhistory if only local history commands will be visited or globalhistory if imported history commands will also be visited.

The substrings are sorted in alphabetical order so that if you want to test for two specific substrings in a future-proof way, you can do match by doing:

if [[ $ZLE_STATE == *globalhistory*insert* ]]; then ...; fi

All quotes from man zshzle.


If you're using tmux you can call the bound key with send-keys.

Examples:

# Current pane
$ tmux send-keys C-r

# Some targeted pane
$ tmux send-keys -t SESSION_NAME:WINDOW_NUMBER.PANE_NUMBER C-z