By default, ctags is not installed on Ubuntu. Run the following command to install ctags on Ubuntu.
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.
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.
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.
Open the file, tags in the vim editor. The file contains all the tag information of the current folder.
Searching tag by pattern
You can search any tag name by using pattern in vim editor. Open a python file named leapyear.py in the vim editor. Search the tag ‘if’ by typing ‘:/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 leapyear.py file and press Enter key. This will highlight the variable, ‘year’.
Search tag by other tag commands
Open another python file named abs_num.py 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’.
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 abs_num.py 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 abs_num.py 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, abs_num.py 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. abs_num.py 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.
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.