The reason this does not work is because zle -U "mc"
pushes "mc" onto the input stack, it does not replace the current command buffer.
What your widget actually does is:
- empty line
- put "mc" on the input stack
- accept the empty line
After the line gets accepted, zsh pulls "mc" from the input stack and puts in the now current buffer. That is why it seems that the widget only prints "mc" without doing anything else.
The intended result could be achieved with
function _midnight { BUFFER="mc" zle accept-line }
But you could also do just:
function _midnight { mc zle reset-prompt }
The main difference being that the first solution emulates what you would be doing, e.g. typing the command and accepting it (this includes mc
being written to the command history). While the second one just starts mc
. zle reset-prompt
is optional, but mc
may leave your cursor at odd positions when exiting.