Пользовательский виджет ZSH не работает, как я думаю, должен

480
Brian Myers

Я пытаюсь определить функцию, чтобы я мог запустить Midnight Commander нажатием Alt-, но эта функция просто не работает.

Я нашел и изменил функцию на другом сайте и изменил ее так:

function _midnight { zle kill-whole-line zle -U "mc" zle accept-line } zle -N _midnight bindkey '\e,' _midnight 

И это то, что я думаю, я говорю это сделать:

define _midnight as { erase everything on the line insert "mc" on the command line execute as a shell command } create _midnight as a custom widget bind alt-comma to the widget 

На самом деле он просто отправляет возврат каретки, а затем вставляет mc на следующей строке, не отправляя его.

Причина, по которой я использую это вместо того, bindkey -s '\e,' '^Umc^Mсостоит в том, что я в конечном итоге хотел бы найти способ запустить Midnight Commander без чего-либо, появляющегося в командной строке.

2

1 ответ на вопрос

2
Adaephon

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.

Мне нужно многому научиться. Я полностью пропустил ту часть, где nothign фактически должно быть записано в подсказку. Второе решение так элегантно. Brian Myers 9 лет назад 0

Похожие вопросы