You're looking for the push-line
command, which is bound to Ctrl+Q and Esc Q by default. This command clears the input and lets you enter another command; once you've executed or canceled this command, the previous command is brought back for editing.
With a multiline command , push-line
only clears the current line, and keeps the other lines in the input buffer. There is another command push-input
which clears the whole input buffer, not bound to any key by default.
I use a wrapper around this command. I type Ctrl+Z to “suspend” the command I'm typing and type another command. After I've run that command (or aborted it with Ctrl+C), the suspended command comes back for edition (even remembering the cursor position). As an additional convenience, if the command line is empty when I type Ctrl+Z, the key invokes the bg
built-in instead (so a double Ctrl+Z sends a running command directly to the background).
fancy-ctrl-z () {
emulate -LR zsh
if [[ $#BUFFER -eq 0 ]]; then
bg
zle redisplay
else
zle push-input
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
A low-tech method that works in any shell is to add a #
at the beginning of the current line and press Enter.