GDB Colour Filter

A problem I face regularly with GDB is the backtrace. The stack trace lists out the function frames currently on the stack and it is a wall of text. it is hard to discern the function address, function name, function parameters, source file path and line number.

Normal GDB backtrace

This is precisely the problem that GDB Colour Filter solves. Clone its repo and source its Python file inside your ~/.gdbinit and you are set. Backtraces are now printed with distinctly different colors and formatting for all the components of a function frame. I especially find it useful to pick out the function name and the source file and line number.

GDB backtrace with GDB Colour Filter

There is only one slight problem: to display the components of a function frame at a consistent column this breaks down a frame into two lines. So your backtrace lines are doubled and might fill up the display when you try this.

How to colorize gcc output using colorgcc

GCC 4.9 and later versions have built-in support for color in its output. For more info about using this, see this post.

If you are using older versions of GCC, then an option to colorize gcc output is colorgcc.

  • It can be installed easily:
$ sudo apt install colorgcc

This installs a Perl script at /usr/bin/colorgcc.

  • Create symbolic links in a directory that is in your PATH for each of the compilers you use. For example:
$ ln -s /usr/bin/c++ /home/joe/color-c++
  • Now you can invoke Make like this:
$ make CXX=color-c++

Note that the above trick will not work if your Makefile was generated by CMake.

  • To ask CMake to use colorgcc, set the CMAKE_CXX_COMPILER to the path of the symbolic link:
set(CMAKE_CXX_COMPILER /home/joe/color-c++)

Tried with: ColorGCC 1.3.2 and Ubuntu 16.04

How to view man pages in color in Fish

Man pages in color
Man pages in color

When you open a man page you are confronted with a wall of white-on-black text. You may have noticed that man pages have some formatting information. This can be used to render the formatted elements in various colors. This can make reading and searching for information in a man page a pleasant affair.

To render man pages with colors in the Fish shell, add these lines to ~/.config/fish/config.fish:

set -x LESS_TERMCAP_mb (printf "33[01;31m")  
set -x LESS_TERMCAP_md (printf "33[01;31m")  
set -x LESS_TERMCAP_me (printf "33[0m")  
set -x LESS_TERMCAP_se (printf "33[0m")  
set -x LESS_TERMCAP_so (printf "33[01;44;33m")  
set -x LESS_TERMCAP_ue (printf "33[0m")  
set -x LESS_TERMCAP_us (printf "33[01;32m")

Tried with: Fish 2.2b1 and Ubuntu 14.04

How to draw scatter plot using Matplotlib

Scatter plot drawn using Matplotlib
Scatter plot drawn using Matplotlib

Scatter plots are useful to show data points that lie in 2D. Drawing a scatter plot in Matplotlib is easy using the scatter function.

  • Assuming your data points are available as two NumPy arrays of shape (N, 1), drawing a scatter plot is straightforward:
import matplotlib.pyplot as mplot

# x_vals is NumPy array of shape (N, 1)
# y_vals is NumPy array of shape (N, 1)
mplot.scatter(x_vals, y_vals)
  • By default, markers that are filled discs, that is of type o, are drawn. This can changed to any of the other markers available in Matplotlib using the marker input argument. The different markers of Matplotlib are shown here.

  • The size of the marker is 20 by default. It can be changed by setting the s input argument.

  • The edge or border of the markers are drawn in black by default. If you do not want the edges to be drawn, then set the edgecolors input argument to none (a string, not the None value).

  • The color that is filled inside the marker is called the face color. It can be set by using the facecolors input argument. For setting the RGB values of N points, pass a NumPy array of shape (N, 3) where each color value lies in (0, 1).

  • If a dense set of data points is being drawn, multiple markers could obscure each other. This situation can be improved by adding some transparency to the markers. This can be set using the alpha input argument.

  • An example that uses all the above customizations to draw the figure shown above:

import matplotlib.pyplot as mplot

# x_vals is NumPy array of shape (N, 1)
# y_vals is NumPy array of shape (N, 1)
# c_arr  is NumPy array of shape (N, 3)

mplot.scatter(x_vals, y_vals, s=2, marker=".", facecolors=c_arr, edgecolors="none", alpha=0.5)

Tried with: Matplotlib 1.3.1 and Ubuntu 14.04

How to plot points in color in Gnuplot

Points plotted with color in Gnuplot
Points plotted with color in Gnuplot

I had some 2D data in a file in the following format:

6.37026 2.42441 0
4.16001 6.55597 2
3.54946 7.15539 2
4.35229 3.8448 1
7.12706 1.2563 3
1.98478 8.68739 0
7.67213 2.22039 0
3.85744 7.3549 1

Each data point is on a separate line. The first and second columns were the X and Y coordinates of the data. The third column represents that which of the 4 classes (0-3) that points belonged to.

To understand the data better, I wanted to visualize it by drawing a 2D plot with a point for each data point. To distinguish the points belonging to different classes, I wanted points of each class to be drawn with a distinct color.

Once your data is in this format, achieving the above is easy in Gnuplot:

plot("sales_data.txt") with points palette

Tried with: Gnuplot 5.0 and Ubuntu 14.04

How to set TERM to xterm-256color

20150318_terminator_xterm256color

Problem

GNOME Terminal, Terminator and other terminal programs in X that are based on VTE set the TERM environment variable to xterm. Since they support 256 colors, TERM should actually be set to xterm-256color.

Why is this important? Vim and other terminal programs decide how many colors to support based on TERM. So, if Vim sees xterm it reverts to using 8 colors, instead of 256 colors. That is, it sets its t_Co variable to 8 instead of 256.

Wrong solution 1

One common solution to this problem is to add set t_Co=256 to your .vimrc.

This is a crude solution because you are overriding the capability of Vim to automatically detect the terminal type and set colors based on that. For example, if you copy your .vimrc to another computer and run Vim under the virtual terminal (VT) which supports only 8 colors, your .vimrc is now setting t_Co erroneously to 256!

Wrong solution 2

Another common solution is to set TERM=xterm-256color in .bashrc or similar shell initialization script. For example, it can be set based on the value of COLORTERM which is set to gnome-terminal by GNOME Terminal.

However, this is a wrong solution because you are setting a terminal capability in a shell!

Also, think what happens if you now run screen or tmux inside the shell. It will set its own terminal type in TERM and overrides your setting.

Correct solution

We first need to identify that this problem lies in VTE and the GNOME-based terminals that use it. This is a well known problem and distributions are mulling over fixing it. In the meanwhile, we can at least make sure that TERM is set to xterm-256color before a shell is invoked.

For example, in Terminator I do this by right-click -> Preferences -> Profiles -> Command -> Custom command and adding a env TERM=xterm-256color fish to set it before calling the Fish shell. You can add similar custom commands for Bash or in GNOME Terminal.

Reference: TERM Strings

Tried with: Terminator 0.97 and Ubuntu 14.04

How to check colors supported by terminal

Problem

On the surface it seems like a simple question: how many colors are supported by a terminal you are using? This is important when configuring or debugging color problems in terminal programs like Vim.

Solution 1

The answer should be simple: just query the terminfo database. The easiest way to query the terminfo database to find this out is:

$ tput colors

The problem? The answer reported by this program is wrong! Under a GNOME Terminal which I can see rendering 256 colors, this reports 8 as the answer!

Why is it reporting the wrong answer? It seems to be misled by the TERM environment variable. For reasons which only the Ubuntu/GNOME Gods can answer, the TERM for GNOME Terminal is set to xterm instead of xterm-256color. If you set this manually to xterm-256color, then tput reports 256 correctly. But then what is the point of that?

Solution 2

Left: terminalcolors.py reporting 8 colors for xterm Right: For xterm-256color
Left: terminalcolors.py reporting 8 colors for xterm Right: For xterm-256color

Another solution is to run the terminalcolors.py Python script from here. This beautifully renders the colors in the shell. However, it also falls for the TERM. Only if it is set to xterm-256color can I see 256 colors rendered. Else only the first 8 colors are shown and the rest are in grey.

A similar output and behavior is seen by running msgcat --color=test.

Solution 3

xterm-color-count reporting colors correctly (well almost)
xterm-color-count reporting colors correctly (well almost)

The final solution that worked for me was xterm-color-count. This Bash script actually tries all the XTerm Control Sequences and examines their responses to report the colors. This script was the only one which reported back 256 correctly for me for xterm.

Tried with: GNOME Terminal 3.6.2 and Ubuntu 14.04

How to test color setup in Vim

Color test of Vim in GNOME Terminal
Color test of Vim in GNOME Terminal

You can quickly test the color setup of Vim by using a Vimscript file that ships along with it. This can be useful to determine if all the default background and foreground combinations in Vim are being rendered correctly for your Vim setup and terminal.

To run this test, type :runtime syntax/colortest.vim. You should be able to see text rendered in various background-foreground combinations, like shown above. There are 46 pairs of background-foreground colors that are displayed on 46 lines (lines 8 to 53).

If you can see most of these color combinations correctly and read the text displayed in color, then your color setup should be fine. Else you need to investigate what setting in your terminal or in your Vim is rendering colors incorrectly.

Tried with: Vim 7.4, GNOME Terminal 3.6.2 and Ubuntu 14.04

GNOME Color Chooser

GNOME Color Chooser
GNOME Color Chooser

GNOME Color Chooser is a tool that makes it easy to change the colors of GTK UI elements. Since GNOME is based on GTK, this affects the foreground, background and other colors of the UI elements of many applications and the desktop. The settings applied here might override the colors applied by your desktop theme.

Installing it is easy:

$ sudo apt install gnome-color-chooser

It can be invoked from the Dash as GNOME Color Chooser or from the shell as gnome-color-chooser. The changes are applied on the desktop and applications as soon as they are set. No need to logout of your desktop session.

Curious to know what this tool is changing? I found that this tool writes a ~/.gtkrc-2.0 file. Internally, that just points to a ~/.gtkrc-2.0-gnome-color-chooser where this tool writes all its settings.

Some of the changes I used it for:

  • The Ubuntu Ambiance theme uses a black background color for Tooltips. This looks horrible especially when they appear in applications like Eclipse. The foreground and background color of Tooltips can be changed in this tool at Specific -> Tooltips.

Tried with: GNOME Color Chooser 0.2.5 and Ubuntu 14.04

How to change font and colors of Tooltips

20150130_tweak_tooltips

The font, font size, foreground color and background color of Tooltips in Ubuntu can be changed. This can be important since Tooltips are used by applications in Eclipse.

  • To change the font and the font size: Open GNOME Tweak Tool, go to Fonts section and set the font and font size for the Interface element.

  • To change the foreground and background colors: Open GNOME Color Chooser, go to Specific -> Tooltips and set the colors you want.

Restart your application to see the effect of these changes.

Tried with: GNOME Tweak Tool 3.10.1-2, GNOME Color Chooser 0.2.5 and Ubuntu 14.04