Getting started with ctags + Vim on MacOS
A quickstart guide will show you how to setup and run ctags on MacOS with Vim. You’ll see how ctags lets you quickly hop from an instance of a function to its definition, and back again.
Installation
If you’re on Mac then I would suggest downloading an upgraded version with homebrew:
brew install ctags
Afterwards, you may need to set the alias to the new version by running:
alias ctags="`brew --prefix`/bin/ctags"
alias ctags >> ~/.bashrc
The second line saves the newly mapped ctags alias so you don’t need to rerun the first line every session. For me it added ctags=/usr/local/bin/ctags
to the .bashrc file.
I then added set tags=tags
to my .vimrc file with:
echo "set tags=tags" >> ~/.vimrc
This will tell vim to look for the ctags index file in the source directory.
Using it
Navigate to the source directory and type:
ctags -R .
Now you should see a newly created file named tags
containing the index.
While still in the source directory, open vim up by typing:
vim
Then navigate to a file and open it. This file should contain an instance of a function that’s defined somewhere else in the source directory.
Move your cursor to some function instance and type:
Ctrl + ]
This should bring you to the functions definition inside vim. You can go back to the instance by typing:
Ctrl + T
Instead of Ctrl + ], you can use this trick to open the function definition in a slit:
Ctrl + W Ctrl + ]
Then you can navigate between the panes by doing
Ctrl + W Ctrl + (arrow key)
Thanks for reading and enjoy using ctags 👏
If you want to chat, you can get in touch with me here on Twitter