Ctags is a tool used for navigating source code indexing methods, classes, identifiers, and variables. Ctags stores the index of programming code in a separate tags file. In the tags file, each line contains a single tag. You can obtain a lot of details from this index. Ctags supports 41 programming code languages and make it easier to search for methods or function blocks in large projects, especially when you do not know the working of code lines. For example, sometimes, you might not know how the particular method to be called when programming. With the ctags tool, you can immediately jump to the method definition.

This article will show you how to install ctags on your Ubuntu 20.04 Linux system and how to integrate Vim with ctags for source code navigation.

Installing ctags on Ubuntu 20.04

Ctags is not installed by default on Ubuntu 20.04. Use the following command to install ctags on Ubuntu 20.04:

Integrating Vim with ctags vim

Select the programming source code project folder to determine whether ctags is functioning properly. This article will use a few examples to test how ctags functions with Python projects.

Navigate to the specified folder where you want to implement ctags. Use the following command to move to the Python folder and list files.

Integrating Vim with ctags vim

Integrating ctags with Vim

All the information related to ctags is stored in a tags file. So, you will need to set the folder path in ~/.vimrc file before using ctags.

Open the ~/.vimrc file in vim editor with root privileges by using the terminal.

Integrating Vim with ctags vim

Write the following set of commands in the .vimrc file and save it.

:syntax on

set tags =$HOME/home/kbuzdar/pythoncode/

Integrating Vim with ctags vim

In the above image, ‘/home/kbuzdar/pythoncode/’ is the folder path where the tags file will be stored.

Integrating Vim with ctags vim

Working with ctags

Navigate to the specified folder through the terminal and use the following command to create ctags of the programming source code files that exist in this folder.

Integrating Vim with ctags vim

After executing the above command, you will observe that a file named ‘tags’ has been created in this folder.

Open the tags file on the vim editor. The tags file contains all the tags details of the programming code files present in the current folder.

Integrating Vim with ctags vim

Find Tags Using Search Pattern

You can search tag by using a search pattern in Vim. To do so, open the python code file named even_odd.py in Vim. Next, search the ‘if’ tag by typing ‘:/if’ as follows:

The following output will be displayed when you hit the Enter key. The ‘if’ tag will be highlighted if it exists in the Python source code file.

Integrating Vim with ctags vim

Search Tags Using tag Command

You can use the tag command in Vim to search for a tag in the file that exists in the tags file. Use the following command to search for a tag in a file:

For example, in the following output, the tag ‘num’ exists in the tags file.

Integrating Vim with ctags vim

If a file contains the same tag many times in a file, the ctag command allows you to move to the next tag of the same type in the tag list. Use the following command to move the cursor to the next tag of the same type:

The ctag command also allows you to move to the previous tag of the same type in the tag list. Use the following command to move the cursor to the previous tag of the same type:

You can also move to the last and the first tag of the same type of tag searched in the list. To move to the last tag of the same type as the searched tag, enter the following command:

To move to the first tag of the same type as the searched tag, enter the following command:

Integrating Vim with ctags vim

To select a particular tag from a list of tags after opening the source code file, use the following command:

You can display the current tag information using the following command:

Integrating Vim with ctags vim

Ctags are useful for source code navigation within a file. Using the tags file, you can search the appropriate tag in the source code file. Keep in mind that if you change the source code file, then every time you make these changes, you will need to integrate and configure ctags with the changes. This is because the ctags file does not automatically update with the source code file.  To resolve this problem, you can use some useful auto plugins that keep your source code and tags file up to date.

About the author

Integrating Vim with ctags vim

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.