When editing multi-line commands, with escaped newlines, I cannot move up lines.

For example, suppose I enter echo \ one one line, then I press Enter, and then I want to edit the echo \ part of the command. Pressing Up doesn't move back to the first command line.

This works for long commands which wrap, but not with escaped newlines:

_physical_up_line()   { zle backward-char -n $COLUMNS }
_physical_down_line() { zle forward-char  -n $COLUMNS }
zle -N physical-up-line _physical_up_line
zle -N physical-down-line _physical_down_line
bindkey -M vicmd "R" physical-up-line
bindkey -M vicmd "N" physical-down-line

When you press Enter (accept-line command), the current line is parsed and scheduled for execution. If the line is syntactically incomplete (e.g. echo \ or for x in foo), it isn't executed, but it's already stored. You can see that zsh is in this state because it shows the PS2 prompt instead of the usual PS1.

As far as I know, there's no built-in way to edit such stored lines. It should be doable by storing the current line without executing it and recalling the previous history line for editing.

The easiest way to get at the previous line is to make sure that the current line is unfinished (e.g. type \ at the end), accept it (press Enter), then cancel it (press Ctrl+C). Then you can recall the whole stored command as a single history line as a single multi-line buffer by pressing Up.


Assuming you have the default emacs keybindings, try using Alt+Enter instead of plain Enter to schedule a line for execution.
For me when the lines are added this way I can easily move up and down those lines. e.g. When at a zsh terminal prompt:

% cat << EOF<ALT-ENTER>
first line<ALT-ENTER>
second line<ALT-ENTER>
   pressing <UP> moves up a line like you would expect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.