2

Question:

I would like bash to print a blank line before the next prompt, but only if there has been output to the terminal.

Partial solution:

  • This answer proposes changing the bash custom prompt to PS1="\n$PS1". This results in bash producing a blank line before every terminal prompt.

  • This answer improves upon this slightly by preventing a newline before the first prompt. This is accomplished via the PROMPT_COMMAND bash variable and a function that sets a variable the first time it is called.

  • This question tries to accomplish something similar (a new line before output instead of after output), but the answers provided only produce newlines for every prompt.

Current behavior:

[user@host ~]$ ls
Desktop  Downloads  Dropbox  Music 

[user@host ~]$ vim 

[user@host ~]$ ssh login

[user@login ~]$ uptime
 08:01:11 up 50 days, 15:15, 15 users,  load average: 0.00, 0.00, 0.03

[user@login ~]$ vim file

[user@login ~]$ cd dir/foo

[user@login foo]$ mkdir bar

[user@login bar]$ cd bar

[user@login bar]$ vim file2

[user@login bar]$ ls
file2

[user@login bar]$

Desired behavior:

[user@host ~]$ ls
Desktop  Downloads  Dropbox  Music 

[user@host ~]$ vim 
[user@host ~]$ ssh login
[user@login ~]$ uptime
 08:01:11 up 50 days, 15:15, 15 users,  load average: 0.00, 0.00, 0.03

[user@login ~]$ vim file
[user@login ~]$ cd dir/foo
[user@login foo]$ mkdir bar
[user@login bar]$ cd bar
[user@login bar]$ vim file2
[user@login bar]$ ls
file2

[user@login bar]$

Motivation:

A new line improves readability, but it is only necessary when there is something to read.

jmlarson
  • 837
  • 8
  • 31
  • 2
    This should be a shell feature, implementing it with external tools doesn't sound like a good idea – oguz ismail Oct 30 '19 at 13:37
  • 2
    It's an interesting question. But I can't think of any reasonable mechanism by which the shell could even know whether the previous command produced any output. – Nate Eldredge Oct 30 '19 at 13:52
  • I agree with @oguz. Do you think that using `PROMPT_COMMAND` and functions constitutes using "external tools"? – jmlarson Oct 30 '19 at 13:53
  • No, they are internal features but I don't think you can do this using them – oguz ismail Oct 30 '19 at 13:57
  • Maybe your `PROMPT_COMMAND` could store and compare the vertical cursor position to guess whether there was an output. However, this method would also print newlines after silent multiline commands. Also, I have no idea how scrolling would affect this method. – Socowi Oct 30 '19 at 14:16
  • pipe all outputs through awk like this somehow? `| awk '{ print $0"\n" }'` I don't know if it's possible to generically modify stdout with something like that – Shardj Oct 30 '19 at 15:41
  • 1
    @Shardj: Even if you could, it would break basically all applications that interact with the user, if they are expecting for stdout to be a tty and it is a pipe instead. – Nate Eldredge Oct 30 '19 at 17:25
  • 1
    Your motivation is improving readability. An alternative is a colored prompt. I use `PS1="\[\033[1;34m\]\u@docker:\[\033[0m\]\w\n$\[\033[0m\] "` on a dark blue blackground (and `colorscheme pablo` in `.vimrc`). – Walter A Nov 01 '19 at 09:42
  • Adding color to the prompt does help with readability. – jmlarson Nov 01 '19 at 12:49

0 Answers0