Exuberant ctags with vim

Exuberant ctags allow you to jump to definition of the method / object you have under your cursor. Here is how to set them up:

  1. Run sudo apt install exuberant-ctags
  2. Create .ctags file in your home directory

    This file is for storing configuration, mine looks like this:

     --exclude=.git
     --exclude=node_modules
     --exclude=log
     --exclude=vendor
     --exclude=tmp
     --exclude=legacy       <-- very nice for working with pytorch
  3. Navigate to a directory with code you are working on.

  4. This directory will be searched automatically. Create a file listing other directories you would like to index.

    In root of the fastai library I have a .srclist file containing the following line:

     /home/radek/workspace/pytorch
  5. Run ctags -R -L .srclist .
    • -R tells ctags to recursively look at directories within the current directory
    • -L tells ctags to look at the listed locations
  6. You are now ready to start using ctags in vim

    • ctrl+] takes you to the tag under cursor
    • ctrl+t takes you back

      For tags resolving to multiple source locations

    • :tnext or :tn takes you to the next location
    • :tprev or :tp takes you to the previous loation
    • :ts lists all source locations
  7. There are many ways to refresh the tags when code changes. Three popular choices:
    • rerunning the command manually
    • using vim-tags
    • adding a crontab entry, for example 15 * * * * ctags -R -L .srclist ~/workspace/fastai/fastai

Suggestions from Jeremy Howard

  • :tag is handy and supports tab completion
  • mapping ctrl-] to :ts is a good idea

    This is what you need to add to .vimrc for this:

      noremap <C-]> g<C-]>
    

    (gives you a list for multiple defs and jumps immediately for single def)