11

When using the command line, often it gets very cluttered. Making it inconvenient to examine past commands and their outputs for example.

I would like to have a newline added each time before the command prompt is shown. Like so:

<clutter>
<blank line>
name@machine:~$

I use the bash shell. How can this be achieved?

Slothworks
  • 445
  • 1
  • 4
  • 17
  • 1
    I prefer this: http://lifehacker.com/5840450/add-a-handy-separator-between-commands-in-your-terminal-on-mac-os-x-and-linux – Maerlyn Aug 11 '15 at 09:16

3 Answers3

23

One way to achieve this is by modifying the .bashrc file. Simply place the following at the end of the .bashrc file.

PS1="\n$PS1"

To explain how this works, PS1 is the variable containing what should be displayed as the prompt. All this is saying is "set PS1 to the previous contents of PS1, with a newline character prepended". Putting it in .bashrc on most distros just makes bash run it every time you open an interactive shell (but not a login shell - see Difference between Login Shell and Non-Login Shell?).

Slothworks
  • 445
  • 1
  • 4
  • 17
  • 2
    To explain how this works, PS1 is the variable containing what should be displayed as the prompt. All this is saying is "set PS1 to the previous contents of PS1, with a newline character prepended". – Muzer Aug 11 '15 at 15:20
  • @Muzer Maybe your comment could be an edit – rpax Aug 11 '15 at 20:21
  • @rpax good idea, submitted. Added a bit about .bashrc itself, too. – Muzer Aug 12 '15 at 08:37
  • Satisfyingly, executing `PS1="\n$PS1"` in a terminal has instant effect :) – mwfearnley Nov 18 '15 at 16:22
  • Works totally fine, other than after entering `clear` (or pressing `Ctrl+l`) the first line shown is an empty line. For some it might be a small detail, but I'd much prefer the first line to be the prompt. – Robert Kusznier Aug 13 '19 at 01:54
6

You can use PROMPT_COMMAND:

PROMPT_COMMAND="printf '\n';$PROMPT_COMMAND"

or:

PROMPT_COMMAND="echo;$PROMPT_COMMAND"
cuonglm
  • 150,973
  • 38
  • 327
  • 406
  • 2
    Should the first one be `PROMPT_COMMAND="printf '\n';$PROMPT_COMMAND"`? In its present form it dint seem to do anything when I placed it in the `.bashrc` file. – Slothworks Aug 11 '15 at 07:19
  • @Slothworks: Yes, of course, my mis-typing, fixed it. – cuonglm Aug 11 '15 at 07:26
  • More than one way to do it indeed :) Thank you for your answer. – Slothworks Aug 11 '15 at 07:35
  • 2
    Slothworks's alternative is better, because yours executes an extra command after every command that is run at the command line. `printf` and `echo` are very lightweight, of course, but it still doesn't make sense to use `PROMPT_COMMAND` when `PS1` will do the job. – Mikkel Aug 11 '15 at 18:00
  • Note, if you wanted to put something in the command that changed every time you ran the prompt, then you want PROMPT_COMMAND and not PS1. This is commonly used with git to display which branch of the repo is active. Google for PROMPT_COMMAND suggestions to see what can be done. – Walter Aug 11 '15 at 21:04
  • A relevant search result: http://stackoverflow.com/q/3058325/3262406 – Slothworks Aug 12 '15 at 05:08
  • For me this works better than adding `'\n'` in front of `$PS1`, because when I press `Ctrl+l` (or enter `clear`), the first line in the terminal is the prompt, while after modifying `$PS1` the first line is empty line. – Robert Kusznier Aug 13 '19 at 01:52
4

Alternative: leave a line in the PS1= prompt of your .bashrc. Here's literally how I set up .bashrc on every linux machine I have:

PS1=' 
serg@ubuntu [$(pwd)]
================================
$ '

As you see in my example above, username is hardcoded into the prompt. You can of course use escape sequences that bash or another shell (e.g. ksh) provides, but a little more neutral option would be to do use parameter substitution with commands like whoami (report your username) and hostname(obviously, reports hostname). For instance:

PS1='
$(whoami)@$(hostname):$(pwd)
$ '

For more fun stuff with the prompt, .bashrc, and parameter expansions, check out my answers here:

How to check battery status using terminal?

How can I get my terminals / shells to have custom designs in it? and

How to show a running clock in terminal before the command prompt?

Sergiy Kolodyazhnyy
  • 16,187
  • 11
  • 53
  • 104