• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Add a Blank line at the Beginning of Command Line Prompt

Add a Blank line at the Beginning of Command Line Prompt

Last updated on October 26, 2020 by Sal Ferrarello

Running the following line from the command will add a blank line before your command line prompt.

PS1=$'\n'"$PS1"

Make this Change Permanent

To make this change permanent add this line to your .bashrc (or .zshrc if you’re using Z shell).

Example

Before the Change

sal@salcode:~/wp-cli (master)$ ls
CONTRIBUTING.md         features
LICENSE                 php
README.md               phpcs.xml.dist
VERSION                 phpunit.xml.dist
bin                     templates
ci                      tests
composer.json           utils
composer.lock           vendor
sal@salcode:~/wp-cli (master)$ ls

Command Line Prompt Example.

After the Change

sal@salcode:~/wp-cli (master)$ PS1=$'\n'"$PS1"

sal@salcode:~/wp-cli (master)$ ls
CONTRIBUTING.md         features
LICENSE                 php
README.md               phpcs.xml.dist
VERSION                 phpunit.xml.dist
bin                     templates
ci                      tests
composer.json           utils
composer.lock           vendor

sal@salcode:~/wp-cli (master)$ ls

Command Line Prompt With Leading Blank Line Example.

zsh specific solution

Thanks to my co-worker Amor Kumar for pointing out this zsh specific solution that eliminates the empty line at the top of a new shell.

Add the following to ~/.zshrc

precmd() {
    precmd() {
        echo
    }
}

In zsh, precmd is a hook “Executed before each prompt.”

This clever piece of code defines a function called precmd(). When the function is run the first time, it (re)defines the function precmd() to echo a blank line. During this first run, it does NOT output anything.

In other words, the first time precmd() runs (at the top of a new shell), it does NOT output anything but it does redefine itself so on all subsequent runs it outputs a blank line.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Solution Tagged With: Bash, command line, zsh

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in