How To Customize Your Terminal

The terminal has been developed over many years, so their wasn’t a single design vision that developed it.  You previously learned what to customize in your terminal. What you’ll learn today is how to customize your terminal to make it more consistent. The terminal application I’m using is iTerm2.

Create Your Theme Profile

This first step doesn’t require any code. Instead, within your terminal application, you’ll need to only create a new profile, and configure it. The main aspects you need to configure are:

  • Name your theme profile
  • Set your colors
  • Set your font

Beyond those, other feature may be available depending on your application.  In this step, you set the foundation of your command line UX.

About Your Profile Colors

There are 4 color palettes you’ll be working with today. The trick is to get them to all play well together. The colors you set in your iTerm profile, are your default colors. You’ll be over-riding them when you configure your prompt, vim color scheme, and ls colorized output. When you switch back, your profile colors will be restored. In your Vim theme, you’ll need to determine when to not set a color.

Setup Bash

There are multiple things you can do to configure bash to match your profile. We’ll start with setting the prompt colors, and LSCOLORS. We’ll use some bash scripting to make things modular. I highly recommend learning the ins and outs of bash. It will play an essential role to customize your terminal experience.

Create Your Default Function

Here’s a boilerplate bash function, without any functionality

function default_settings(){

    # Set ls colors


    # Set Primary Prompt

}

The contents of the LSCOLORS variable is a string, which represents pairs of file types that the ls commands uses to differentiate the output – directories, executables, et cetera. This string is represented as foreground/background pairs, with one pair per type.

To determine the available color values, review the man page for ls on your system. On mac the string ‘cxxxxxxxCxxxxxxxxxxxxx’ will display directories in a normal green font, executable files in a bold green font, and all the other file types using the default text and background colors.

function default_settings(){

    #
    # Define coloring of different file types for `ls` command 
    #
    LSCOLORS="cxxxxxxxCxxxxxxxxxxxxx"

    export LSCOLORS


    # Set Primary Prompt

}

As you can see, the value of the LSCOLORS is just a string, but manipulating the values is not intuitive. One way to simplify the editing of it, is to concatenate the LSCOLORS variable. In this example, the comments indicate which part of of the variable.

function default_settings(){

    #
    # Define coloring of different file types for `ls` command 
    #
    DEFAULT_FG_BG="xx"

    LSCOLORS=""

    # directory
    LSCOLORS+="cx"

    # symbolic link
    LSCOLORS+=$DEFAULT_FG_BG

    # socket
    LSCOLORS+=$DEFAULT_FG_BG

    # pipe
    LSCOLORS+=$DEFAULT_FG_BG

    # executable
    LSCOLORS+="Cx"

    # block special
    LSCOLORS+=$DEFAULT_FG_BG

    # character special
    LSCOLORS+=$DEFAULT_FG_BG

    # executable with setuid bit set
    LSCOLORS+=$DEFAULT_FG_BG

    # executable with setgid bit set
    LSCOLORS+=$DEFAULT_FG_BG

    # directory writable to others, with sticky bit
    LSCOLORS+=$DEFAULT_FG_BG

    # directory writable to others, without sticky bit
    LSCOLORS+=$DEFAULT_FG_BG

    export LSCOLORS


    #
    # Set Primary Prompt
    #

}

Using the same concatenation technique, you can create a more elaborate prompts. In this example, The PS1 variable below displays the whole prompt in yellow. the escape sequence at the end – [\e[0m] – restores the default color. Here’s some information about coloring the prompt.

#
# Set Primary Prompt
# 

PS1="\n\[\e[1;33m\]\u";
PS1+="@";
PS1+="\h ";
PS1+="\W\n";
PS1+="\$\[\e[0m\] "

export PS1

Somewhere after this function is defined, you’ll need to call the function. this is the minimum you need.

Create Your Theme Function

Now that you have a default_settings function, you’ll need to create one for your theme. For the sake of example, I’d have ‘yourtheme_settings’. The colors you use for your ls command and your prompt should match your theme profile.

Create Your Conditional

The iTerm2 application sets an environment variable automatically when a new window or tab is created. This variable is how we determine which profile settings to load. Now all we need to do is create an if/elif/else block.

if [[ 'yourtheme_settings' == $ITERM_PROFILE ]]
then
    yourtheme_settings
else
    default_settings
fi

Set up Vim Theme

So you’ve created the theme profile for your application, and configured your ls and prompt colors. Your next step is to create a vim theme. This is a big part of your command line experience when you edit files, so you want to spend a fair amount of time working on your vim color scheme.

Create Your Color Scheme (a.k.a. theme)

Vim looks for custom themes in your ~/.vim/colors directory. You should install your theme there. You’ll spend hours tweaking your vim theme colors. That’s the fun part, but something that’s out of scope for this article.

Alias your theme in Bash

in the bash function your created for your theme, you should add an alias to tell veim to use your theme. This will allow you to override the default theme in your .vimrc file.

alias vi='vi -c "colo yourtheme"'

Summary

That’s the heart of how to improve your command line user experience. The hardest part is creating the Vim theme. Ironically, that’s the part I can’t help you with here. While it’s a minimal example, it’s easy to adapt so that you can add features or settings to further customize your UX.

Sorry, but comments are closed. I hope you enjoyed the article