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:
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 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.
Write the following set of commands in the .vimrc file and save it.
In the above image, ‘/home/kbuzdar/pythoncode/’ is the folder path where the tags file will be stored.
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.
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.
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.
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.
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:
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:
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.