Vim and Ctags
While working on SuperTuxKart, I tend to use vim. Wonderful as it may be when you get into a good flow with your favourite editor, default vim is lacking in some ends. One of those is more easily navigating through bigger source files and projects.
To solve this, just install Ctags using your favourite method. For example, in Arch Linux it is a simple
pacman -S ctags
. This program goes through your source files and parses out
functions, macros, … which it then puts into a file called tags
. Vim can
use this tags file to allow you to navigate through your project more easily.
I will show by example, using it on SuperTuxKart’s src/
folder, which contains
all the C++ files. Start out by generating the tags
file.
ctags -R ./src/
Now we have a file ./tags
that has all the info about our ./src/
folder’s
files. However, vim has to still find the tags file. If we open
./src/karts/kart.hpp
, then we want it to still use our generated tags file a
few directories up. For this, we add the following lines to our ~/.vimrc
" Searches for tags file in current folder and works its way up to
" root looking for one.
set tags=./tags;/
Lines in .vimrc
starting with a "
are comments, be sure to keep them in so
future you will still be sure what the line does.
Now all that is done, we will also want to actually use some of the features it provides. Here is a non-exhaustive list of handy keyboard shortcuts
- CTRL+] while the cursor is on a function, will bring you to the definition of that function.
- CTRL+T will take you back to where the function was called.
- CTRL+\ opens the definition in a new tab.
- ALT+] opens the definition in a vertical split in the same window.
We are not done yet! Moving around like that is a start, but sometimes you want
an overview of the functions in the file you are looking at. Fear not, you
are not alone and the functionality is a plugin away. Simply download
taglist.vim and unzip
the zip file you get into ~/.vim/
. Now when using vim, you can open a left
pane list of functions, macros, … of the file you are working with. Simply
type :TlistOpen
to open it up.
No doubt there is much more that can be done with the mentioned plugin and ctags functionality, but this post was deliberately made to just give a quick intro to future me, as well as the random reader who happens onto my blog.