Do you find yourself: - ack’ing (or better yet grep’ing) your source code to find a method? - using gem open and similar? - browsing online documentation and checking method arguments? If you are doing anything listed above, you don’t have to anymore!
Do you find yourself:
If you are doing anything listed above, you don’t have to anymore!
To have the minimal amount of fun, let’s start with installing http://ctags.sourceforge.net/. If you’re running OSX it is available through homebrew:
brew install ctags
Once you have it installed, go to the directory of your project and simply run:
This will generate an index file for language objects found in all recognized project source files. Usually, it is located in the current directory, called
tags and consists of lines sorted by
identifier in a format similar to:
Ctags can generate tags for over 40 languages, and if you only know regular expressions, you can easily add more by yourself.
If you have a
tags file generated, Vim will let you use it out-of-the-box.
Give these a try:
If there are multiple matches, Vim will jump to the first one by default.
You might want to consult
to discover more useful split-and-jump commands, or how to view alternatives in a quickfix window.
If you have multiple global tag files or have renamed the tag file generated by ctags with the
-f option, you can specify a comma separated list of file names which will be searched for when looking for a tag:
#1 Searching For A Tag
If you’re not familiar already with CtrlP, it’s a fuzzy full path file finder and one that I highly recommend.
If you’re already using CtrlP you should check out tag specials at:
#2 Insert Mode Tag Completion
Fuzzy search code completion which can be found at https://github.com/Valloric/YouCompleteMe will save you some typing. Besides the keywords it detects itself, it can also complete known tag names for you.
For those of you who have relatively small tags files, do the following to have the plugin identifier completer collect tag file entries:
Add this line to your .vimrc:
let g:ycm\_collect\_identifiers\_from\_tags\_files = 1
Have the tags generated with `—fields=+l’
Note that I don’t actually use this myself, since my tags files are enormous, and having it enabled makes Vim uncomfortably slower. Nevertheless, if your tag file is small, it can come in handy.
#3 Displaying Tags
These two will show your tags in a window and additionally, let you peek at the method signature.
Taglist a source code browser
Tagbar which I’m currently using and prefer a bit more over Taglist
[ tags browsers example here] Taglist on the left, TagBar on the right
Generating tags for only project files can be a little annoying. For example, doing
CTRL+] only to find that the jump cannot be done because the keyword is not included in the tags file is a nightmare. For me personally, the main utility of the index is having all of the library methods available for browsing by a single key binding.
As I primarily work with ruby and rails, I have a tags file generated for all of the gems which are currently bundled in the project (e.g. in bash:)
Regenerating an index time and time again can be quite tiresome, so if you don’t want to do that (and save yourself some time) there are a few routes you can take:
ctags -aand append any new tags that have been added to the file you are currently editing.
Personally, I have two tags files for:
git pullif there were any changes
I work with CoffeeScript a lot lately, but alas, ctags does not support coffee. However, as I mentioned before, you can extend the number of languages ctags recognizes by defining the new language with
lang-def, lang-map and
There are quite a few regexes written for coffee, e.g.
which you can place in your ~/.ctags configuration file. If you don’t want to play with it, you can check out a another gem solution here: https://github.com/lukaszkorecki/CoffeeTags
As you can see, the possibilities are many, and you can pick and choose the setup and flavour which you like most.
It will surely let you:
and have it all with automatic generation, which you can set and forget, makes it worth the extra effort. Once you have it, you won’t look back.
…on the flip side, unfortunately ctags:
Please remember that ctags does not provide any kind of method reference access. I may talk about that later on another ‘vim hacks’ post.
Join the discussion at reddit r/vim