LinuxDojo

How to Make Your Bash Terminal Look Good with Go & Google Fonts

Making your Bash terminal visually appealing involves two main aspects: choosing the right font for your terminal emulator and customizing your Bash prompt (PS1) and colors. While Google Fonts are primarily for web, many excellent monospaced fonts (ideal for terminals) are available for download from Google Fonts. Go can be used to create highly customized and performant dynamic Bash prompts.

This guide will walk you through setting up a great font and give you an introduction to building a custom prompt with Go.

Estimated Time: 30-60 minutes (depending on Go familiarity)
You will find examples tailored for **Arch Linux, Ubuntu/Debian, and FreeBSD** systems throughout this guide.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Choosing & Installing a Terminal Font (Google Fonts Angle)

While Google Fonts themselves are designed for web use, many excellent monospaced fonts (where all characters have the same width, crucial for terminals) are hosted on Google Fonts and are available for download and system-wide installation. These often include "ligatures" which can make code look cleaner.

Recommended Fonts from Google Fonts (or similar popular choices):

For this guide, let's assume you're interested in Fira Code.

1.1 Download the Font

1.2 Install the Font System-Wide

On Arch Linux:

The easiest way is usually through yay or pacman if an otf-fira-code or ttf-fira-code package exists.

Install Fira Code on Arch Linux
# Using yay (recommended):
yay -S otf-fira-code # Or ttf-fira-code, depending on the package name

# Manual Installation:
mkdir -p ~/.local/share/fonts
cp ~/Downloads/FiraCode-*.ttf ~/.local/share/fonts/ # Adjust path
fc-cache -fv
On Ubuntu/Debian:

Fira Code is typically available in the official repositories:

Install Fira Code on Ubuntu/Debian
sudo apt update
sudo apt install fonts-firacode

For manual installation (if not available via `apt` or if you prefer):

Manual Font Installation on Ubuntu/Debian
mkdir -p ~/.local/share/fonts
cp ~/Downloads/FiraCode-*.ttf ~/.local/share/fonts/ # Adjust path
fc-cache -fv
On FreeBSD:
Install Fira Code on FreeBSD
# Using pkg (recommended):
sudo pkg install fira-code

# Manual Installation:
mkdir -p ~/.fonts
cp ~/Downloads/FiraCode-*.ttf ~/.fonts/ # Adjust path
fc-cache -fv

1.3 Set the Font in Your Terminal Emulator

This step varies greatly depending on your terminal emulator. Here are common ways:

Restart your terminal emulator after setting the font. You should immediately see the new font applied.

Step 2: Customizing Your Bash Prompt with Go (Advanced)

Creating a dynamic Bash prompt using a compiled Go program offers excellent performance and allows for complex logic that might be cumbersome in pure Bash. We'll create a very simple Go program that outputs a colored prompt string.

2.1 Create Your Go Prompt Program

Create a new directory for your Go project (e.g., ~/go-prompt):

Create Go Project Directory & File
mkdir ~/go-prompt
cd ~/go-prompt
nano main.go

Paste the following Go code into main.go:

main.go code
package main

import (
    "fmt"
    "os"
    "os/user"
    "strings"
)

// ANSI escape codes for colors
const (
    Reset  = "\033[0m"
    Bold   = "\033[1m"
    Cyan   = "\033[36m"
    Green  = "\033[32m"
    Yellow = "\033[33m"
    Blue   = "\033[34m"
    Red    = "\033[31m"
)

func main() {
    // Get current user and hostname
    u, err := user.Current()
    if err != nil {
        fmt.Print(Red + "error" + Reset)
        return
    }
    hostname, err := os.Hostname()
    if err != nil {
        fmt.Print(Red + "error" + Reset)
        return
    }

    // Get current working directory, shorten if in home dir
    cwd, err := os.Getwd()
    if err != nil {
        fmt.Print(Red + "error" + Reset)
        return
    }

    homeDir := u.HomeDir
    if strings.HasPrefix(cwd, homeDir) {
        cwd = strings.Replace(cwd, homeDir, "~", 1)
    }

    // Determine prompt symbol based on user (root vs. non-root)
    promptSymbol := "$"
    if u.Uid == "0" { // Root user
        promptSymbol = "#"
    }

    // Construct the prompt string
    // Example: [user@hostname /path/to/cwd] $
    prompt := fmt.Sprintf("[%s%s%s@%s%s %s%s%s]%s %s",
        Green, u.Username, Reset, // Username in Green
        Blue, hostname, Reset,    // Hostname in Blue
        Cyan, cwd, Reset,         // Current directory in Cyan
        Yellow, promptSymbol, Reset, // Prompt symbol in Yellow
    )

    fmt.Print(prompt + " ")
}

Save and exit nano (Ctrl+X, Y, Enter).

2.2 Compile Your Go Program

Compile the program. This will create a standalone executable.

Compile Go Program
cd ~/go-prompt
go build -o myprompt # Creates an executable named 'myprompt'

You can test it by running ./myprompt. It should output your new prompt string.

2.3 Integrate into Your Bash Configuration

Now, you need to tell Bash to use your myprompt program. Edit your Bash configuration file:

Open ~/.bashrc
nano ~/.bashrc

Add the following lines at the end of your ~/.bashrc file:

Add to ~/.bashrc
# Custom Go Prompt
# Ensure the path to your Go prompt executable is correct
export PATH="$HOME/go-prompt:$PATH" # Add go-prompt dir to PATH
PROMPT_COMMAND='PS1=$(myprompt)'

Explanation:

Save and exit nano.

2.4 Apply Changes

For the changes to take effect in your current terminal session, you need to source your ~/.bashrc:

Source ~/.bashrc
source ~/.bashrc

You should immediately see your new Go-powered prompt! If there are issues, double-check the path to myprompt and any error messages.

Here's an example of what your Go-powered Bash terminal might look like:

Screenshot of a customized Bash terminal with Go-powered prompt and Fira Code font.

Step 3: Color Schemes and Terminal Themes

While your font and prompt are a big part, a good color scheme makes a huge difference.

Step 4: Other Enhancements

Conclusion

By following these steps, you've selected a visually appealing monospaced font from the Google Fonts ecosystem, installed it on your Linux (Arch, Ubuntu/Debian) and FreeBSD systems, and configured your terminal emulator to use it. You've also learned how to leverage Go to build a custom, performant Bash prompt, taking your terminal customization to the next level. Enjoy your good-looking Bash terminal!