14

I'm in the process of customizing my terminal.

Currently, I have the following:

before

What I want is a newline after the output of every command, like this:

after

The only way I have been able to accomplish something close to this is by adding a newline at the beginning of my PS1. This works, but it annoys the hell out of me that the first time I open up a bash prompt, there is a newline above the very first line. Is there any other way to accomplish this?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
bitpshr
  • 1,033
  • 2
  • 9
  • 21

3 Answers3

14

One approach using printf:

$ printf '%s\n' * $'\n'

or better (for every command):

$ PROMPT_COMMAND="echo"
$ ls

From man bash :

If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    I like this method, but this is interpreted before `PS1` and still prints a newline above the very first line of the bash prompt, essentially equivalent to a newline at the beginning of `PS1`. I may be doing something wrong... – bitpshr Oct 26 '13 at 17:48
  • 1
    I'm really not sure how this is different from putting a newline at the beginning of PS1, which what OP asked not to do. (Except PROMPT_COMMAND is slightly more expensive.) – kojiro Oct 26 '13 at 18:08
  • 1
    I once [posted an answer](http://stackoverflow.com/a/14860632/1126841) showing how to define `PRMOPT_COMMAND` in such a way that it redefines itself the first time it is called. You could adapt that so that `echo` is *not* called the very first time it is used in the shell, but is for each subsequent use. – chepner Oct 26 '13 at 19:27
  • Is there a way to get this to work after every line of *output* instead of every command? – Cole May 20 '20 at 07:42
  • 1
    `PROMPT_COMMAND="export PROMPT_COMMAND=echo"` seems to work – qwazix Feb 12 '21 at 15:19
  • For anyone who tries to get this to work in macOS: neither of these worked in zsh on macOS. – jcollum Mar 07 '22 at 16:35
5

Combining @GillesQuenot's answer with @chepner's comment this seems to work and it's quite simple:

PROMPT_COMMAND="export PROMPT_COMMAND=echo"
qwazix
  • 906
  • 11
  • 17
1

Adding up @GillesQuenot's answer + @chepner's comment with a neat hack for the clear command:

# Print newline AFTER executing a command
PROMPT_COMMAND="export PROMPT_COMMAND=echo"
alias clear="unset PROMPT_COMMAND; clear; PROMPT_COMMAND='export PROMPT_COMMAND=echo'"
Safal Piya
  • 11
  • 1
  • 4