30

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

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
orschiro
  • 19,847
  • 19
  • 64
  • 95

10 Answers10

48

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

Abid H. Mujtaba
  • 1,890
  • 1
  • 24
  • 20
  • 9
    This works, the issue is that it prints a new line before each prompt, even when you start up the shell! Any way around this?? – Sethen May 18 '15 at 20:57
  • 2
    @Sethen I know this is now over two years old, but yours is a common question. I've submitted an edit to this answer (which is still a top search result) which addresses this. –  Jun 27 '17 at 19:33
  • @Ben your edit hasn't been accepted, can you please share your solution. – Vinay Nagaraj Apr 27 '18 at 05:52
  • 1
    A technique to skip the empty line on the first prompt is to wrap `precmd() { print "" }` in `precmd() { … }`. See my answer to that effect with a little explanation. – Chris Morgan Jan 03 '20 at 10:35
18

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • Perfect answer! Any chance an exception could be made for a 'clear' when a newline at top is not required? – Yashank Dec 30 '20 at 01:30
  • I don’t think so generally. You could alias `clear` to something that would redefine `precmd` in this way, but at that point a magic variable and conditional would probably be cleaner too. You’d probably need to change Ctrl-L behaviour in zle too, somehow, so that it would do this twiddling too. I’m not sure how that’s done. – Chris Morgan Jan 03 '21 at 18:57
13

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:%~ $ "
Exeleration-G
  • 1,360
  • 16
  • 29
7

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
}
5

Following works:

export PS1='
Other text - blah$'

pr-pal
  • 3,248
  • 26
  • 18
  • This answer is helpful for those folks looking to put a newline **within** the prompt string. My interest is to put a newline right before the $ character. This leaves the prompt text one line and the actual $ prompt on a new line. – Sixto Saez Jun 25 '20 at 17:06
3

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.

2

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

1

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

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
Mogens
  • 548
  • 1
  • 3
  • 10
1

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).

agostonbarna
  • 356
  • 3
  • 4
0

A lot of the answers above do work but with some caveats, like an annoying newline at startup and newline when terminal gets clear

This solution fixes all of the issues ( Hopefully )

In your .zshrc, add this

precmd() {
    precmd() {
        echo
    }
}

This will echo a new line after every command but not at startup

But we still have another problem - when the terminal gets cleared a newline is echoed. To fix that alias 'clear' to :-

alias clear="precmd() {precmd() {echo }} && clear"

For me 'Ctrl + L' clears the screen without a newline but if anyone is having issues just add this to your .zshrc

bindkey -s '^L' 'clear^M'
Panda
  • 1
  • 1