ctags command in Linux system is used for the with the classic editors. It allows quick access across the files (For example quickly seeing definition of a function). A user can run tags or ctags inside a directory to create a simple index of the source files while working on. Tags-capable editors like vi/vim can then refer to these tags index file to allow you to follow references. Basically, this command generates the tag files for source code. It is also used to create a cross reference file which lists and contain the information about the various source objects found in a set of human-readable language files.
Syntax:
Options:
- –help: It will print the general syntax of the command along with the various options that can be used with the ctags command as well as gives a brief description about each option.
- ctags -a: This option used to append the tags to an existing tag file. Equivalent to –append. [Ignored with -e]
- ctags -B: This option used for backward searching patterns (e.g. ?regexp?). [Ignored with -e]
- ctags -e: This option used for output a tag file for use with Emacs. If this program is being executed by the name etags, this option is already enabled by default.
- ctags -F: This option used for searching patterns (e.g. /regexp/)(default). [Ignored with -e]
- ctags -i: This option is similar to the –c-types option and is retained for all the compatibility with earlier versions.
- ctags -n: This option is Equivalent to –excmd=number.
- ctags -N: This option is Equivalent to –excmd=pattern.
- ctags -o: This option is Equivalent to -f tagfile.
- ctags -p: This option is Used path as the default directory for each supplied source file, unless the source file is already specified as an absolute path.
- ctags -R: This option is Equivalent to –recurse=yes.
- ctags -u: This option is Equivalent to –sort=no (i.e. “unsorted”).
- ctags -V: This option Enables the verbose mode. This prints out a brief message describing that what action is being taken for each of the file considered by ctags.
If you want to try the latest universal-ctags without building it yourself. Daily builds are available at the ctags-win32 project. Go to the releases page to download zip packages. See Homebrew Tap for Universal Ctags. Go to ctags-snap and clone the ctags-snap repo. Then, follow instructions to build the snap package of ctags. On your Mac, click the Finder icon in the Dock to open a Finder window. Do any of the following: Search for a tag: Enter the tag color or name in the search field, then select the tag from the suggestions. Select a tag in the sidebar: To see everything with a certain tag, click the tag in the Finder sidebar. To change the items you see in the sidebar, choose Finder Preferences, click Tags.
- Nov 04, 2017 Prerequisites. Mac OSX (for this tutorial, can be installed on other operating systems) Homebrew; Install universal-ctags. First, we’ll install universal-ctags for our OS.
- I believe ctags defaults to sort=yes so these edits are not necessary. I'm wondering if the edits are useful. JohnBeckett 23:32, May 28, 2010 (UTC) The autocompletion does.NOT. work for boost shared pointers. So for example, if there is a section of code that looks like.
- Compatibility: Windows, Linux, Mac OS, IOS, Android, UNIX, AmigaOS, and MorphOS. Top plugins and features: Scripts that allow you to perform almost all of the programming tasks in Python. Ctags for better tag navigation. Built-in basic word completion. Pros: It has a keyboard interface to improve efficiency.
ctags with Vim:
- cd to the folder of your choice where your file is located:
Example:Run ctags recursively over the entire folder of your choice to generate the tags file
- Now run this command:
- To search for a specific tag and open the output in Vim to its definition, run the following command in your shell:
Example:As a result this screen pops up with the matching result:
-
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please Improve this article if you find anything incorrect by clicking on the 'Improve Article' button below.
Today I learned
Ctags is not always the best solution to this today. There are Language Server implementations for some modern languages that would offer these features and more. Also, as of 2019, there are other ctags alternatives to consider today, such as Universal Ctags.
Ctags lets you navigate code fast, and is perhaps the single most useful productivity boosting tool in any developer's arsenal. If you're not using Ctags yet, let's get you started.
What's it for?
Ctags indexes a project's tags, or names of its classes and methods. Coupled with some integration with your editor (shown: Vim's unite-tag), it will give you two interesting features to help you traverse code base:
Ctags For Windows
Installing Ctags
Exuberant Ctags
In OS X, use Homebrew to install Exuberant Ctags. This is a more useful version of ctags than the one that ships with Xcode.
Ctags options
Let's make ctags ignore some common directories. Save this file as
~/.ctags
.
Generate ctags for a project
Go to your project's path, and run ctags to generate a
tags
file in your project. This is the index of all tags in your project that your editor will use.
Ignore all ctags files
It's safe to make all projects ignore all files called
tags
. I recommend setting up a global git ignore list.
Vim setup
Auto-update ctags files
Use vim-autotag to automatically update
tags
files. This will only work on projects that have already had ctags -R
performed before.
Jumping to tags
Use
:tag
to go to the definition of a certain tag. Usually, you will want to use this to jump to a certain Class or Method. Yes, this supports tab completion!
From the command line
Use
vim -t
to open vim to a certain tag.
Navigating through multiple definition
If you used
:tag
on a tag that's got multiple definitions, use these commands to sift through them all.
Shortcut | Definition |
---|---|
:tn |
Move to next definition (:tnext ) |
:tp |
Move to previous definition (:tprevious ) |
:ts |
List all definitions (:tselect ) |
Key shortcuts
You can also place your cursor on some text and press
^]
to jump to that tag.
Shortcut | Definition |
---|---|
^] |
Jump to definition |
^t |
Jump back from definition |
^W } |
Preview definition |
g] |
See all definitions |
Unite integration
If you're using unite.vim, you can use unite-tag to browse tags. You can also check out my plugin, vim-fastunite, which offers a pre-packaged distribution of Unite.vim.
Futher reading
- Browsing programs with tags (vim.wikia.com)
Ctags For Mac Download
You have just read Navigate code like a pro with Ctags, written on April 22, 2015. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).