kdb
kdb

Reputation: 4426

Consistent PS2 prompt in zsh when editing multiline input?

When using PS1='>>> ' and PS2='... ', entering a multi line command looks like

>>> for file in *.txt; do
...     echo "-- $file"
...     cat "$file"
... done
(Here goes the output)

When editing the command however, PS2 is ignored and I am seeing.

>>> for file in *.txt; do
    echo "-- $file"
    cat "$file"
done
(Here goes the output)

Note how the output and the command are no longer clearly separated.

Is it possible to make this more consistent, i.e. for multi line editing to respect PS2?

Upvotes: 2

Views: 702

Answers (1)

Marlon Richert
Marlon Richert

Reputation: 7040

Note how the output and the command are no longer clearly separated.

If your concern is to clearly separate command and output, just put something like this in your .zshrc file:

autoload -Uz add-zsh-hook
add-zsh-hook preexec separate
separate () {
  # If command has more than 1 line, add empty line before output.
  [[ $3 == *$'\n'* ]] && 
      print
}

Note: The 3rd arg to each preexec hook function contains the full text that will be executed. See https://zsh.sourceforge.io/Doc/Release/Functions.html#index-preexec


Alternatively, you can just always print something after each command. For example:

autoload -Uz add-zsh-hook
add-zsh-hook preexec separate
separate () {
  print 'vvv'
}

Upvotes: 2

Related Questions