How to search and replace word under cursor in Vim

Search and replace in Vim is done using this command: :%s/old/new/.

If you start using Vim a lot, you start seeing a problem with this: you have to type the old string that you want to search and be replaced. If this string is long and complicated, then it is a pain. It would be wonderful if you could magically get the word currently under cursor into this search and replace command.

In Vim, you can do this! Here you go:

  • Navigate cursor to any letter of the word.

  • Press *. This highlights the word and also selects it.

  • Now type the command :%s//new/. Notice that you are not providing any search string. Vim automatically picks up the last searched word for that! :-)

Tried with: Vim 7.4 and Ubuntu 14.04

About these ads

Warning options of GCC

The GCC and G++ compilers have many warning options that can be useful to improve the quality of your code. All the warning options are listed and described here.

Since the list of warnings is very long, GCC provides a few warning options that are a shortcut for enabling a larger bunch of warnings. These warning options are -Wall, -Wextra and -Wpedantic.

The warnings enabled by -Wall and -Wextra is listed along with their description. However, this is not provided for -Wpedantic. I went through the individual warning descriptions and found the warning options enabled by -Wpedantic:

Warning options enabled by -Wpedantic:


It can very useful to know which of the warning options are not enabled by -Wall, -Wextra and -Wpedantic. These are the options that might be useful for debugging or improving particular problems with your code. Again, I went through the individual warning descriptions and here they are:

Warning options not included by -Wall, -Wextra and -Wpedantic:


Note that I have not included the warning options that disable certain types of warning messages.

Tried with: GCC 5.0.0 online documentation

How to create a confusion matrix plot using Matplotlib

Confusion matrix plot generated using Matplotlib

Confusion matrix plot generated using Matplotlib

Confusion matrix is an excellent method to illustrate the results of multi-class classification. It takes a single function call in Matplotlib to generate a colorful confusion matrix plot. However, you have to first have your results in the form of a confusion matrix.

Let me illustrate with an example. Assume, you have 4 classes: A, B, C and D. Your classifier does great on A, C and D with fully accurate results. However, for results that should be class B, it classifies them as A 10% of the time and as C 20% of the time. You should be able to extract such classification results from your classifier easily.

You just need to put these results in a 2D float Numpy array in the form of a confusion matrix. In this type of matrix, typically the true classes are listed on the Y axis, top to bottom. The predicted classes are listed on X axis, from left to right. For our example, the confusion matrix would look like this:

[[100 0  0   0  ]
 [10  70 20  0  ]
 [0   0  100 0  ]
 [0   0  0   100]]

Optionally, you can also normalize the results to 1.0:

[[1.0 0   0   0  ]
 [0.1 0.7 0.2 0  ]
 [0   0   1.0 0  ]
 [0   0   0   1.0]]

Once you have this as a 2D float Numpy array, just pass it to the matshow method of Matplotlib to generate the confusion matrix plot. To get a temperature scale of the colors used in the plot, call the colorbar method:

import matplotlib.pyplot as plt

# Assume m is 2D Numpy array with these values
# [[1.0 0   0   0  ]
#  [0.1 0.7 0.2 0  ]
#  [0   0   1.0 0  ]
#  [0   0   0   1.0]]


To add X-axis, Y-axis labels, and other modifications, use the typical calls you use in Matplotlib for other types of plots.

Tried with: Python 2.7.6 and Ubuntu 14.04

How to convert type of items in Numpy array

You may sometimes receive a Numpy array from external sources where the data type of the items are string or some other non-numeral format. Converting the data type of a Numpy array can be done using the astype method. This method on the array creates a copy of the array where every item has been converted to the destination type.

For example, assume smat is a Numpy array of strings that contain floats. To convert it to Numpy array of floats:

>>> smat
[[ '1.2', '3.4', '5.6']
 [ '7.8', '8.9', '9.0']]
>>> fmat = smat.astype(numpy.float)
>>> fmat
[[ 1.2 3.4 5.6 ]
 [ 7.8 8.9 9.0 ]]

Tried with: Python 2.7.6

Channel order in OpenCV

Sometimes, you may need to access the memory of a cv::Mat or an IplImage directly. If the image contains M channels, then it becomes important to know how the pixels of these M channels are stored in the image memory. According to OpenCV documentation here:

  • The M bytes of every pixel are stored together.
  • For 3 channel image, the RGB channels are stored in BGR order. There is no particular reason for this, OpenCV just happens to store it in this order. It may be due to influence from Windows, where most internal APIs seem to deal with this channel order.

How to print to file from Vim

Printing from Vim is not like any other application since it has both console (Vim) and desktop (GVim) versions running off the same backend. On Linux, it does not show a Print Dialog, where I can pick the printer and other print options like number of pages per side, orientation and duplex. So, I print to a PDF file, open it in a PDF viewer and print from there.

To print to a PDF file from Vim:

:hardcopy > out.pdf

To print to a PS file from Vim:

:hardcopy >

Tried with: Vim 7.4 and Ubuntu 14.04


Redshift is an alternative to F.Lux for Linux. To install the commandline tool and its status icon:

$ sudo add-apt-repository ppa:jonls/redshift-ppa
$ sudo apt-get update
$ sudo apt-get install redshift redshift-gtk

Just run redshift at the commandline. Using the geoclue framework it figures out the latitude-longitude of your location and adjusts the color temperature of your display.

If you prefer having a status icon, then run Redshift from the Dash. Clicking the status icon gives you the options of having Redshift start whenever you login and to temporarily turn it off.

Tried with: Redshift 1.8 and Ubuntu 14.04