Theme.sh is an interactive script that enables one to set the terminal theme. This script comes with about 400 themes in a single file. You can set the terminal theme directly or using fzf(command-line fuzzy finder) which offers a terminal menu for theme selection.

Theme.sh can be used on any terminal that supports OSC 4/11. These terminals include:

  • urxvt(non interactively unless the truecolor patch is applied)
  • kitty
  • alacrity
  • st
  • Terminal.app(osx)
  • iTerm2
  • libvte-based terminal like Terminator, GNOME Terminal, Xfce Terminal, Guake, Console, Mate Terminal, and many others.

Theme.sh offers other features that include:

  • It is portable (a single, 130k file with all 400 themes included)
  • Self modifying (can ingest kitty themes).
  • It is script friendly
  • It keeps history
  • It is interactive – requires fzf
  • Has both dark and light filters so you can decide when you want to burn your retinas.
  • Small, self contained and POSIX compliant

This guide offers a detailed demonstration of how to easily change Linux/macOS Terminal theme using theme.sh

Install Theme.sh on Your system

Installing theme.sh is a very simple task. It entails downloading the script and putting it somewhere in your path.

This can be achieved using the cURL commands below:

  • On Linux
sudo curl -Lo /usr/bin/theme.sh 'https://git.io/JM70M' && sudo chmod +x /usr/bin/theme.sh
  • On OSX
sudo curl -Lo /usr/local/bin/theme.sh 'https://git.io/JM70M' && sudo chmod +x /usr/local/bin/theme.sh

You may also need to install fzf(the general-purpose command-line fuzzy finder) for an interactive shell as below.

  • On Linux

Ensure that git is installed on your system before proceeding as below.

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

Futhermore fzf is available in Linux package managers and can be installed as below.

##On Debian/Ubuntu
sudo apt-get install fzf

##On Fedora
sudo dnf install fzf

##On Arch Linux
sudo pacman -S fzf

##On openSUSE
sudo zypper install fzf
  • On macOS

You can use Homebrew to install this package.

$ brew install fzf

# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install

Edit the Linux/macOS Terminal Theme using theme.sh

Once the script has been added to your patch, you can easily change your Linux/macOS Terminal as below.

List the available themes:

theme.sh -l

Sample Output:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh

List dark themes.

theme.sh --dark --list

Sample Output:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 1

Now switch to the desired theme.

theme.sh gruvbox-dark-medium

Sample Output:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 2

You can also use the interactive shell to set a theme.

theme.sh -i

Sample output:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 3

Once selected, press Enter to apply the theme.

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 4 1

Configure theme.sh

Using theme.sh without further configurations is not persistent, the set theme is automatically lost when the system is restarted or when a new terminal window is opened.

For the theme.sh changes to survive a system reboot, we need to edit the shell and add the lines as below.

  • For bash/Zsh

Open the file for editing.

$ vim ~/.bashrc
##OR##
$ vim ~/.zshrc

Now add the below lines at the end of the file

if command -v theme.sh > /dev/null; then
	[ -e ~/.theme_history ] && theme.sh "$(theme.sh -l|tail -n1)"

	# Optional

	#Binds C-o to the previously active theme.
	bind -x '"\C-o":"theme.sh $(theme.sh -l|tail -n2|head -n1)"'

	alias th='theme.sh -i'

	# Interactively load a light theme
	alias thl='theme.sh --light -i'

	# Interactively load a dark theme
	alias thd='theme.sh --dark -i'
fi

You may need to source the profile.

$ source ~/.bashrc
##OR##
$ source ~/.zshrc
  • For Fish.
vim ~/.config/fish/config.fish

AT the end of the file, add the lines.

if type -q theme.sh
	if test -e ~/.theme_history
	theme.sh (theme.sh -l|tail -n1)
	end

	# Optional
	# Bind C-o to the last theme.
	function last_theme
		theme.sh (theme.sh -l|tail -n2|head -n1)
	end

	bind \co last_theme

	alias th='theme.sh -i'

	# Interactively load a light theme
	alias thl='theme.sh --light -i'

	# Interactively load a dark theme
	alias thd='theme.sh --dark -i'
end

You can also configure Vim to play with the new themes in ~/.vimrc

colorscheme default
set notermguicolors
highlight Search ctermfg=0

sudo/su wrapper

You can also edit the sudo/su wrapper. For example, to set a red-alert theme when su/sudo is run, add the below lines to ~/.bashrc

su() {
	(
		INHIBIT_THEME_HIST=1 theme.sh red-alert
		trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
		env su "$@"
		theme.sh "$(theme.sh -l|tail -n1)"
	)
}

sudo() {
	(
		pid=$(exec sh -c 'echo "$PPID"')

		# If the command takes less than .2s, don't change the theme.
		# We could also just match on 'su' and ignore everything else,
		# but this also accomodates other long running commands
		# like 'sudo sleep 5s'. Modify to taste.

		(
				sleep .2s
				ps -p "$pid" > /dev/null && INHIBIT_THEME_HIST=1 theme.sh red-alert
		) &

		trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
		env sudo "$@"
		theme.sh "$(theme.sh -l|tail -n1)"
	)
}

Source the profile and try to run su/sudo commands. Your terminal will appear as below.

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 5

When running a sudo command, the terminal changes to the red-alert theme and back to the default theme once complete.

Theme.sh SSH Intergration.

For SSH integration.

$ vim ~/.ssh_themes
host1: zenburn
host2: red-alert
...

In the file, replace host1 and host2 with the desired hostnames/IP_addresses, you can also set more themes to be used side from zenburn and red-alert for the hosts.

Now add the lines to ~/.bashrc

ssh() {
	# A tiny ssh wrapper which extracts a theme from ~/.ssh_themes
	# and applies it for the duration of the current ssh command.
	# Each line in ~/.ssh_themes has the format:
	#     <hostname>: <theme>.

	# Restoration relies on the fact that you are using theme.sh to manage
	# the current theme.  (that is, you set the theme in your bashrc.)

	# This can probably be made more robust. It is just a small demo
	# of what is possible.


	touch ~/.ssh_themes

	host="$(echo "$@"|awk '{gsub(".*@","",$NF);print $NF}')"
	theme="$(awk -vhost="$host" -F': *' 'index($0, host":") == 1 {print $2}' < ~/.ssh_themes)"

	if [ -z "$theme" ]; then
		env ssh "$@"
		return
	fi

	INHIBIT_THEME_HIST=1 theme.sh "$theme"
	trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
	env ssh "$@"
	theme.sh "$(theme.sh -l|tail -n1)"
}

For example, trying SSH to my host 2 with the red-alert theme set:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 6

Adding themes to theme.sh.

If the theme.sh script is writable, then you can easily add themes to it using the –add flag. For example, to add a theme proceed as below.

curl -O 'https://raw.githubusercontent.com/dexpota/kitty-themes/master/themes/Solarized_Darcula.conf'
sudo theme.sh --add Solarized_Darcula.conf

Set the new theme:

theme.sh Solarized_Darcula

Execution output:

How To Easily Change LinuxmacOS Terminal Theme using theme.sh 7

Here, any theme with a similar name will be overwritten.

Conclusion.

That marks the end of this amazing guide. Now you can be able to play around with the terminal themes and make desired configurations with theme.sh. Did you find it amazing? Let us know your thoughts about this script in the comments section.

Related posts:

LEAVE A REPLY

Please enter your comment!
Please enter your name here