Thursday, September 1, 2016

Three Minutes Daily Vim Tip: Ctags

When you are dealing with a large code base, you definitely want to jump back and forth within the code. Let's assume that you are looking at OpenCV library source code. To find functions and jump back and forth between functions and files in Vim, one can use ctags:

First, install ctags
$ sudo apt-get install ctags

Next, create the tags for the source directory
$ cd ~/opencv-3.1.0
$ ctags -R

Finally, use Vim to search for a specific tag
$ vim -t Stitcher

The above command looks for a function, class, or variable named that contains the word Stitcher. There should only be one return, and Vim will open up stitching.hpp file and point to the class Stitcher.

To view the list of previously found tag, run the following within Vim
:ts

To search for tag blend, run
:ts blend

To go to the next or previous tag result in the list, run
:tn
:tp


Now this is where the fun begins! To jump to the tag currently pointed by the cursor, press
<ctrl> + ]

To go back to the caller tag, press
<ctrl> + t

To go back to the previous position, press
<ctrl> + o

Happy Vimming!

No comments:

Post a Comment