Pregunta

Can I configure my prompt to show a new line after each command?

To give you an example. In the following screenshot I did run cat .zshrc. I want to have a new line between the last output line of the command, . ~/.zsh_aliases, and ~ $.

enter image description here

¿Fue útil?

Solución

Edit ~/.zshrc and add the line precmd() { print "" }. This will simply print an empty line before the PROMPT is rendered.

Otros consejos

Another way is by just setting a custom prompt in ~/.zshrc that includes a newline character. For example:

autoload -Uz promptinit
promptinit

PROMPT="
%n@%m:%~ $ "

A variant of some existing solutions, which I find neater than using a magic variable and conditional:

precmd() {
    precmd() {
        echo
    }
}

This puts a blank line before every prompt except the first one: all the first precmd invocation does is replace precmd with a function that calls echo.

The accepted answer (authored by @abid-h-mujtaba) always prints a newline, even on the first load of the shell. I submitted an edit that was, for whatever reason, not accepted.

This is what I use in my personal dotfiles (see "Window Configuration" in zshrc):

function precmd() {
    # Print a newline before the prompt, unless it's the
    # first prompt in the process.
    if [ -z "$NEW_LINE_BEFORE_PROMPT" ]; then
        NEW_LINE_BEFORE_PROMPT=1
    elif [ "$NEW_LINE_BEFORE_PROMPT" -eq 1 ]; then
        echo "\n"
    fi
}

Following works:

export PS1='
Other text - blah$'

I know this is a bit old, but I found a way, even if it's not very clean, I just wanted to share it:

function precmd {
    if [[ "$NEW_LINE" = true ]] then
        if [[ "${ADD_NEW_LINE}" = true ]] then
            PROMPT=$'\n'"${PROMPT}"
            ADD_NEW_LINE=false
        fi
    else
        PROMPT="${PROMPT}"
        NEW_LINE=true
        ADD_NEW_LINE=true
    fi
}

Hope it helps

user926352

The accepted answer (authored by @abid-h-mujtaba) always prints a newline, even on the first load of the shell. I submitted an edit that was, for whatever reason, not accepted. This is what I use in my personal dotfiles (see "Window Configuration" in zshrc): function precmd() { # Print a newline before the prompt, unless it's the # first prompt in the process. if [ -z "$NEW_LINE_BEFORE_PROMPT" ]; then NEW_LINE_BEFORE_PROMPT=1 elif [ "$NEW_LINE_BEFORE_PROMPT" -eq 1 ]; then echo "\n" fi }

I used this answer, but didn't want the new line after a clear. So I added:

alias clear="unset NEW_LINE_BEFORE_PROMPT && clear"

Just a drop in replacement for clear that also unsets the new line variable.

ATTENTION: Edited the suggestion above, because I was assigning a function to clear that did the unset and then clear. That made it recursive, so I eventually hit the limit of nested functions. Oops :). You can just do this instead though and it's much simpler and safer. Sorry if someone found this before the edit.

Using zsh with oh-my-zsh, git support and ZSH Powerlevel9k Theme on Ubuntu 18.04, installed like described here: https://linuxhint.com/install_zsh_shell_ubuntu_1804/

To get the prompt at a new line open:

/usr/share/powerlevel9k/powerlevel9k.zsh-theme

Look for the function left_prompt_end()

The function looks like this in orig:

# End the left prompt, closes the final segment.
left_prompt_end() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n "%k%F{$CURRENT_BG}$(print_icon 'LEFT_SEGMENT_SEPARATOR')"
  else
    echo -n "%k"
  fi
  echo -n "%f$(print_icon 'LEFT_SEGMENT_END_SEPARATOR')"
  CURRENT_BG=''
}

Just add one new-line command. The function should now look like here:

# End the left prompt, closes the final segment.
left_prompt_end() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n "%k%F{$CURRENT_BG}$(print_icon 'LEFT_SEGMENT_SEPARATOR')\n"
  else
    echo -n "%k"
  fi
  echo -n "%f$(print_icon 'LEFT_SEGMENT_END_SEPARATOR')"
  CURRENT_BG=''
}

Following line was changed from:

echo -n "%k%F{$CURRENT_BG}$(print_icon 'LEFT_SEGMENT_SEPARATOR')"

To:

echo -n "%k%F{$CURRENT_BG}$(print_icon 'LEFT_SEGMENT_SEPARATOR')\n"

Prompt on new line : enter image description here

To print a newline only between the prompts:

precmd() $funcstack[1]() echo

or if you (want to) use multiple precmd hooks:

autoload -Uz add-zsh-hook
_precmd_newline_between_prompts() $funcstack[1]() echo
add-zsh-hook precmd _precmd_newline_between_prompts

Explanation:

With zsh the braces for simple functions are optional, so if it is not clear the first method is equivalent to this one:

precmd() {
  $funcstack[1]() {
    echo
  }
}

$funcstack[1] is special zsh parameter that contains the name of the caller/outer function.

On the first call (before the first prompt) the inner method $funcstack[1]() echo won't run yet, but will redefine the outer method, so it will only print a newline on further calls (before additional prompts).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top