Sean Mackesey
Sean Mackesey

Reputation: 10959

Inserting a newline in a multiline zsh command pulled from history

Sometimes I use multiline commands in zsh:

❯ echo \
> a \
> multiline \
> command

When editing the command after pulling it from a history search, I can change the content of individual lines. However, I can't figure out how to insert another line:

# I want to insert another line after "multiline"...
❯ echo \
> a \
> multiline \  # but hitting <return> here just runs the command, even though there's a backslash at the end of the line
> command

How can I insert a newline in the middle of a multiline command pulled from history?

Upvotes: 48

Views: 27830

Answers (7)

Agrover112
Agrover112

Reputation: 645

CTRL + Enter (Return) for Windows/WSL CTRL +X CTRL+E for Mac

Edited as per the comment below

Upvotes: 0

Alter Lagos
Alter Lagos

Reputation: 12550

Just to note, if you want to comment in a multiline command, you could use:

❯ echo `#first comment` \
 a `#second comment` \
 multiline \
 command

Upvotes: 2

Kamaraju Kusumanchi
Kamaraju Kusumanchi

Reputation: 1964

You can use ESC-Return.

FWIW, I tested it on Debian Jessie, zsh 5.0.7 and it works there.

Upvotes: 59

John Gliksberg
John Gliksberg

Reputation: 122

If using bindkey -v mode, you can also use the default o/O commands from vicmd mode to add a newline line and enter insert mode in it, respectively above or below the current line.

Upvotes: 4

Adaephon
Adaephon

Reputation: 18429

You can use self-insert-unmeta to bind Alt+Return to insert a literal newline without accepting the command:

bindkey '^[^M' self-insert-unmeta

To use your example: Hitting Alt+Return at the cursor position (#)

% echo \
a \
multiline \#
command

will get you this:

% echo \
a \
multiline \
#
command

This works not only when editing history, but also when typing commands. So you can prepare several commands in a script like fashion and accept them with a single Return.

For example pressing Alt+Return instead of # in this example:

% echo command 1#
echo command 2#
echo command 3

will do the same thing as the command echo command 1; echo command 2; echo command 3 and produce this output:

command 1
command 2
command 3

Upvotes: 44

chepner
chepner

Reputation: 532268

(A summary of answers from https://unix.stackexchange.com/questions/6620/how-to-edit-command-line-in-full-screen-editor-in-zsh)

zsh comes with a function that can be used to open the current command line in your favorite editor. Add the following lines to your .zshrc:

autoload -z edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line

The first line loads the function. The second line creates a new widget for the Z shell line editor (zle) from the function of the same name. The third line binds the widget to Control-X Control-E. If you use the vi bindings rather than emacs key bindings, use something like

bindkey -M vicmd v edit-command-line

instead (which binds the widget to v in vicmd mode).

Upvotes: 7

Stephen
Stephen

Reputation: 4249

Sounds like an appropriate place to use a shell script file instead no?

#!/bin/zsh
my
commands
here
I can even add a new line at a later time.

Upvotes: -2

Related Questions