zsh new line prompt after each command
Asked Answered
R

10

32

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

Russophobe answered 11/12, 2013 at 7:24 Comment(0)
O
51

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

Oospore answered 16/12, 2013 at 16:23 Comment(4)
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??Aldridge
@Aldridge 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.Protestantism
@Ben your edit hasn't been accepted, can you please share your solution.Foppish
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.Hollington
H
25

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.

Hollington answered 3/1, 2020 at 10:32 Comment(2)
Perfect answer! Any chance an exception could be made for a 'clear' when a newline at top is not required?Joline
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.Hollington
T
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:%~ $ "
Trollop answered 1/6, 2017 at 8:9 Comment(1)
This is how I ended up doing mine to avoid new lines before each new prompt.Aldridge
P
8

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
}
Protestantism answered 30/4, 2018 at 15:48 Comment(3)
echo "\n" seemed to be printing two blank lines for me, but echo "" worked well.Emelia
Thank you very much for this answerImprovised
Works for 'reset', 'clear', and between commands, however it doesn't add a newline after CTRL+l.Inbeing
M
6

Following works:

export PS1='
Other text - blah$'

Mistreat answered 20/4, 2019 at 16:57 Comment(1)
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.Sisson
B
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.

Brasca answered 27/4, 2021 at 9:59 Comment(1)
I did the same but with reset too.Devito
T
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

Tilefish answered 28/2, 2017 at 12:52 Comment(0)
T
2

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

Thant answered 21/3, 2020 at 16:9 Comment(0)
S
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

Singapore answered 16/6, 2019 at 23:34 Comment(1)
There is an extra space on the new line. Can you please tell how to remove that extra line?Fennessy
A
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'
Alcott answered 8/5, 2023 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.