16

Say I am editing a single line, with my cursor at the indicated position:

$ abc ꕯ def ghi

I would now like to split into two lines and continue editing (like hitting ENTER would do in a text editor):

$ abc
$ ꕯ def ghi

Is there a way to do this in ZSH?

1
  • Should abc be executed or do you want to insert a \n character at the cursor position?
    – mpy
    Feb 19, 2014 at 23:30

3 Answers 3

19

In ZLE's emacs mode:

… the Control+V character (ASCII SYN) is bound to the quoted-insert widget. So just enter Control+V then Control+J (ASCII LF).

In ZLE's vi mode:

… the Control+Q (ASCII DC1) and Control+V characters are bound to the vi-quoted-insert widget. So again, in insert mode, just enter Control+V, then Control+J.

Remember:

Newlines in the middle of command lines separate commands. (In the terminology of the zsh manual: both newline and ; terminate a list.) Quote the newline if you don't want that.

3
  • I was able to bind the emacs mode to Shift + Enter to automatically insert a quoted insert. However since I switched to the vim mode, it doesn't work. Is there a way to make it work in vim mode? Mar 6, 2017 at 9:57
  • 1
    In emacs mode, you can also insert a newline with M-Return.
    – SilverWolf
    Sep 7, 2017 at 20:37
  • I suspect you mean to escape the newline (with a backslash) if you don't want that newline to separate commands. A quoted newline (e.g. $'\n') is merely a string with a single NL char in it which is going to be sent as an argument to the command.
    – Steven Lu
    Sep 21, 2023 at 4:32
6

Use ⌥↩︎ (Option / Alt+ Return), or ⎋, ↩︎ (Esc then Return).

This will only work if you've called bindkey -e previously or somewhere in your .zshrc.

2
  • PS: This also works in OS X! If you press ⌥↩︎ in, say, the Messages box, you'll get a new line. Doesn't work in SE comment boxes, though. (:
    – SilverWolf
    Sep 7, 2017 at 20:36
  • Thank you so much, this is the answer that I was looking for. Usually line-break is done using shift + enter but seems like linux does stuff differently. Thanks a lot for the concise and straightforward answer!
    – RVFET
    Nov 13, 2023 at 9:08
2

Here is a "vimmer" way in ZLE vi-mode that does not involve ctrl chords.

if your line looks like follows, and you are in normal mode: $ abc ꕯ def ghi

type this: DoESCp

total key presses: 4.

explanation:

`D` cuts from cursor to the end of the line and places cut string in register
`o`opens a new line places the cursor in it, and switches to insert mode
`<esc>` switches to normal pode
`p` paste register content.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .