How to Use Colors in Warp/zsh

As a newcomer to using Warp, it’s important to note that some Bash-specific commands may not work as expected in the Warp terminal. However, you can still achieve colorized output in zsh by utilizing the tput command.

Pavol Kutaj

--

The tput command generates terminal-specific escape sequences for colors, making it compatible with various terminal emulators, including Warp. By using tput setaf and tput setab, you can set the foreground and background colors, respectively. The tput sgr0 command resets the colors to the default.

  • In zsh, you can use the tput command to apply colors in the Warp terminal.
  • tput setaf sets the foreground color, and tput setab sets the background color.
  • tput sgr0 resets the colors to the default.
  • The appearance of colors may vary depending on your terminal emulator and its configuration.
  • tput setaf color codes:
  • 0 or black
  • 1 or red
  • 2 or green
  • 3 or yellow
  • 4 or blue
  • 5 or magenta
  • 6 or cyan
  • 7 or white

Using the tput command allows you to generate terminal-specific escape sequences, ensuring compatibility with different terminal emulators, including Warp.

Example

Here’s an example function that uses fzf to select a file, prints its content, and displays its path in cyan:

function caf {
absolute_path=$(readlink -f "$(fzf)")
cat "$absolute_path"
echo "$(tput setaf 5)~~> ${absolute_path}$(tput sgr0)"
}

In this example, the caf function uses fzf to interactively select a file. The selected file's absolute path is obtained using readlink -f. The content of the file is then printed using cat, and the file's path is displayed in cyan using tput setaf and tput sgr0.

LINKS

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com