Insert a new line in a multi-line zsh command extracted from history

Sometimes I use multi-line commands in zsh :

 echo \ > a \ > multiline \ > command 

When editing a command after it is searched from a history search, I can change the contents of individual lines. However, I cannot figure out how to insert another row:

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

How can I insert a new line in the middle of a multi-line command extracted from history?

+6
source share
6 answers

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

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

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

 % echo \ a \ multiline \# command 

You will get the following:

 % echo \ a \ multiline \ # command 

This works not only when editing history, but also when typing commands. This way you can prepare several script-style commands 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 as echo command 1; echo command 2; echo command 3 echo command 1; echo command 2; echo command 3 echo command 1; echo command 2; echo command 3 , and will produce this output:

 command 1 command 2 command 3 
+14
source

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

zsh comes with a feature that can be used to open the current command line in your favorite editor. Add the following lines to .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 ( zle ) shell line editor from a function with the same name. The third line binds the widget to Control - X Control - E. If you use vi bindings, not emacs key bindings, use something like

 bindkey -M vicmd v edit-command-line 

(binds the widget to v in vicmd mode).

+6
source

You can use ESC - Return .

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

+6
source

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

+4
source

Sounds like a good place to use a shell script file instead?

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

Just note that if you want to comment on a multi-line command, you can use:

 echo `#first comment` \ > a `#second comment` \ > multiline \ > command 
0
source

Source: https://habr.com/ru/post/976833/


All Articles