Browsing through the code base

When I worked as Windows developer, my main tool of choice was obviously Visual Studio. Now I use vim and at first there was a problem: Browsing through code base.

How can I browse through my code base? In Visual Studio I just needed to click on an identifier to see the file or class where it was defined. The first days doing Ruby and Padrino where a bit frustrating for me, because I did not understand where certain methods or classes came from. It needed many manual work to find answers to my questions.

ctags

Then I was pointed to ctags. This solves the issue that I was not able to get quickly to a method or class definition.

On most Unix systems this should be preinstalled, otherwise have a look at your package manager repository.

How does it work?

Ok, at first, it creates a tags file of your code base. Mostly I create this at the top level directory:

$ ctags -R .

This creates a tags file for all classes and methods for the current directory recursively. That’s it. Now, in vim you can type:

:tag <identifier>

(Note, that the file will be openend in place, so if you want the current file to stay open, I recommend to split the window first Ctrl-W v for vertical split or Ctrl-W s for horizontal split).

Also check, if vim’s working directory is set to the directory containing the tags file. Check it via:

:pwd

and in case you need to switch to another directory, just

:cd <path>

(This also works in MacVim)

Find usages of string

The other thing which is very comfortable in an IDE is doing search in the complete project. For example, if you want to see all places where a constant is used, you just open the search window and it will show all occurrences. This was the other thing which annoyed me when I switched to terminal and vim. But after some searches on google, I found a way to do this without an IDE.

$ egrep -inr <search_string> <path>

This searches for the search pattern in the given path and lists all occurrences with line number.

  • i – Performs case insensitive search
  • n – Prints the line numbers
  • r – Search recursively