3

I'm trying to define a function so that I can launch Midnight Commander by pressing Alt-, but the function just doesn't work.

I found and modified a function on another site and modified it so:

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

And this is what I think I'm telling it to do:

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

What it actually does is just send a carriage return and then insert mc on the next line, without sending it.

The reason I'm using this instead of bindkey -s '\e,' '^Umc^M is because I'd eventually like to find a way to run Midnight Commander without anything appearing on the command line.

1 Answers1

2

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.

Adaephon
  • 6,094