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 ~ $
.
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 ~ $
.
Edit ~/.zshrc
and add the line precmd() { print "" }
. This will simply print an empty line before the PROMPT is rendered.
precmd() { print "" }
in precmd() { … }
. See my answer to that effect with a little explanation. –
Hollington 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
.
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 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:%~ $ "
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
}
echo "\n"
seemed to be printing two blank lines for me, but echo ""
worked well. –
Emelia Following works:
export PS1='
Other text - blah$'
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.
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
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).
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"
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'
© 2022 - 2024 — McMap. All rights reserved.