Vim and Ctags

Ctags is a very useful tool to navigate any source code of the programming language. Identifiers, methods, classes, etc. from the source code are parsed by using ctags and saved the index in a tag file. Each tag is stored in each line. Ctags is supported by many programming languages. This tool helps the user to search any method or function block to find out how it works. It is very useful to search for any variable in the large project. How ctags can be installed and used with vim editor for navigating the source code of any programming language on Ubuntu are shown in this tutorial.

Install ctags

By default, ctags is not installed on Ubuntu. Run the following command to install ctags on Ubuntu.

$ sudo apt-get install ctags

Select Program Folder

You have to select any source code or programming project folder to check the uses of ctags. Python scripts are used in this tutorial to test the use of the ctags. Go to the folder where you want to apply the ctags. The following two commands are used to go to the python folder and check the file list.

$ cd code/python
$ ls

Configure Ctags

Ctags stores all information in tags file. It is necessary to set the folder location of tags file in .vimrc file before using this tool. Open the .vimrc file in vim editor with root permission and add the following set command that defines the location where tags file will be stored.

$ sudo vim ~/.vimrc
set tags+=$HOME/code/python/

Create tags

Run the commands ‘ctags -R *’ to create tags for all the files exist in the selected project folder. Next, ‘ls’ command is executed to check the tag file is created or not.

$ ctags -R *
$ ls

Open the file, tags in the vim editor. The file contains all the tag information of the current folder.

$ vim tags

Searching tag by pattern

You can search any tag name by using pattern in vim editor. Open a python file named in the vim editor. Search the tag ‘if’ by typing ‘:/if’.

$ vim
: /if

The following output will appear after pressing the enter key. ‘If’ tag will be highlighted if it exists in the source code.

Searching tag by tag command

Type ‘:tag tagname’ in the vim editor to search any tag in the file that exists in the tag file. Here, ‘year’ tag exists in tags file. Type the following ctags command to search the tag, ‘year ‘ in file and press Enter key. This will highlight the variable, ‘year’.

:tag year

Search tag by other tag commands

Open another python file named in the vim editor and search the tag, ‘num’ by using tag command. There are three entries of ‘num’ tag in the tag file because there are three statements in the source code with the variable, ‘num’.

$ vim
:tag num

Move to next tag

Ctags has a command to move the next tag in the list of the same type of tag. Type the following ctags command from vim editor to move the cursor in the next ‘num’ tag.


Move to the previous tag

ctags has also a command to move to the previous tag in the list of the same type of tag. Type the following ctags command from vim editor to move the cursor in the previous ‘num’ tag.


Move to the last tag

If the source code contains more than one same tag in the list then the following ctags command can be used to move to the last tag position in the tag list.  There are three tags of ‘num’ for file in the tag list. ‘:tlast’ will move the cursor in the third position of the ‘num’ tag.


Move to the first tag

ctags command can also be used to move the cursor in the first tag of the same tag list. The following command will move the cursor to the first position of ‘num’ tag in file.


Select tag from tag list

You can select a particular tag from the tag list after opening the file in the vim editor by using ctags command. Open any source code in vim editor and type ‘:tselect’ to find out the list of tag list of current source code. Here, the same file, is used to check this command.


The following output will appear after executing the above command. It shows that there are three entries of ‘num’ tag.  The user has to type any number from 1 to 3 and press Enter key to select any tag from the list.

Find a particular tag position

‘:tags’ command can be used to find out the information of the current tag. file contains a list of three ‘num’ tag. If the ‘num’ tag is searched and the cursor is under the first ‘num’ tag in the list then the following output will appear after running the following ctags command.

: tags

The output shows that ‘num’ tag is the first tag in the tag list.


Ctags helps to find out the particular portion of the source code easily if the tag entry exists in tags file and the user knows the appropriate tag name that he/she is searching for. There is a problem with using ctags. The source code of any file can be changed at any time. You need to configure ctags every time if any source code is changed because ctags can’t update tags file automatically. Autotag plugin can be used to solve this issue. This plugin keeps tags file up to date. But, you have to use vim with python to use this plugin. If you want to navigate any source code efficiently to search any part of the code then this tutorial will help to learn the use ctags to do that task.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.