prompt

All posts tagged prompt by Linux Bash
  • Posted on

    How to Customise Your Bash Prompt for Better Productivity

    The Bash prompt is the text that appears in your terminal before you type a command. By default, it displays minimal information, such as your username and current directory. Customizing your Bash prompt can enhance productivity by providing quick access to important information and making your terminal visually appealing.


    What is the Bash Prompt?

    The Bash prompt is controlled by the PS1 variable, which defines its appearance. For example:

    PS1="\u@\h:\w\$ "
    
    • \u: Username.
    • \h: Hostname.
    • \w: Current working directory.
    • \$: Displays $ for normal users and # for the root user.

    Why Customise Your Bash Prompt?

    1. Enhanced Information: Display details like the current Git branch, exit status of the last command, or time.
    2. Improved Visuals: Use colors and formatting to make the prompt easier to read.
    3. Increased Productivity: Quickly identify useful information without typing additional commands.

    Steps to Customise Your Bash Prompt

    1. Temporary Customization

    You can modify your prompt temporarily by setting the PS1 variable:

    PS1="\[\e[32m\]\u@\h:\w\$\[\e[0m\] "
    

    This changes the prompt to green text with the username, hostname, and working directory.

    2. Permanent Customisation

    To make changes permanent:

    1. Edit the .bashrc file in your home directory: bash vi ~/.bashrc
    2. Add your custom PS1 line at the end of the file.
    3. Save and reload the file: bash source ~/.bashrc

    Common Customizations

    Add Colors

    Use ANSI escape codes for colors:

    • \[\e[31m\]: Red

    • \[\e[32m\]: Green

    • \[\e[34m\]: Blue

    • \[\e[0m\]: Reset to default color.

    Example:

    PS1="\[\e[34m\]\u@\h:\[\e[32m\]\w\$\[\e[0m\] "
    

    This makes the username and hostname blue and the working directory green.

    Include Time

    Display the current time:

    PS1="\[\e[33m\]\t \[\e[34m\]\u@\h:\w\$ \[\e[0m\]"
    
    • \t: Time in HH:MM:SS format.

    • \@: Time in 12-hour AM/PM format.

    Show Git Branch

    Display the current Git branch when inside a repository:

    PS1="\[\e[32m\]\u@\h:\[\e[34m\]\w\[\e[31m\]\$(__git_ps1)\[\e[0m\] \$ "
    
    • Ensure you have Git installed and the git-prompt.sh script sourced in your .bashrc file.

    Add Command Exit Status

    Show the exit status of the last command:

    PS1="\[\e[31m\]\$? \[\e[34m\]\u@\h:\w\$ \[\e[0m\]"
    
    • \$?: Exit status of the last command.

    Advanced Customisations with Tools

    Starship

    Starship is a modern, highly customizable prompt written in Rust. Install it and add this line to your .bashrc:

    eval "$(starship init bash)"
    

    Best Practices for Customizing Your Prompt

    1. Keep it Simple: Avoid cluttering the prompt with too much information.
    2. Use Colors Sparingly: Highlight only the most critical details.
    3. Test Changes: Test new prompts before making them permanent.
    4. Backup Your .bashrc: Keep a backup before making extensive changes.

    Example Custom Prompt

    Here’s a full-featured example:

    PS1="\[\e[32m\]\u@\h \[\e[36m\]\w \[\e[33m\]\t \[\e[31m\]$(git_branch)\[\e[0m\]\$ "
    
    • Green username and hostname.
    • Cyan working directory.
    • Yellow current time.
    • Red Git branch (requires Git integration).

    Customizing your Bash prompt is a great way to make your terminal more functional and visually appealing. Experiment with different configurations and find the one that works best for you!