All Case Studies Design Development Interviews Our Way Project Management

Win VIM! hacks

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:

  • 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!

What do you need?

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:

ctags -R

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:

{identifier}{filename}{ex command, which can transport us to the identifier}.

Ctags can generate tags for over 40 languages, and if you only know regular expressions, you can easily add more by yourself.

What does it provide?

If you have a tags file generated, Vim will let you use it out-of-the-box.

Give these a try:

 

[ tagstack example here]

If there are multiple matches, Vim will jump to the first one by default.

 

Note that:

  • Jumping around alternatives does not change the tagstack.
  • You can also use :ta /pattern.

You might want to consult

:h tagsrch.txt

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:

 

Let the tag plugins come forth

#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:

 

[ CtrlPBufTag example here]

#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

 

Not only project files

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:)

 

Reindexing

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:

Personally, I have two tags files for:

  • project files - reindexed only after git pull if there were any changes
  • gem files - reindexed only after bundle install

Extending tags

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 regex-your\_language.

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

Summing it all up

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:

  • Have method definitions at your fingertips, no matter — libraries or project specific,
  • Browse code effortlessly

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:

  • have some issues with module scopes which have ::
  • when using it in conjunction with ruby, it does not tag everything you would expect it to (e.g. constants and attributes.

Choosing another solution based on parsers such as ripper tags, which are written specifically for Ruby source code parsing should have no issues like the ones above.

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

Why it is impossible to hire quality engineers in London and how to deal with it
Scaling SaaS
READ ALSO FROM Dev
Read also
Need a successful project?
Estimate project or contact us