Code navigation with Ctags instead of LSP
Published on
Recently I’ve got more opportunity to explore and modify larger codebases, due to being in a lab focused on industrial collaborations. Some of these codebases are well documented with tests and examples, some are mediocre, and some are straight out bad old code dating back to the early 2000s. To gain knowledge about these codebases, tools are required. I used to rely on LSP (Language Server Protocol) for this kind of task, but a prerequisite for spinning up the server itself is to get the software at hand to successfully build (or at least to successfully configure), which is sometimes time-consuming for those dated or under-documented software.
As a result, I rediscovered 1 Ctags and Emacs’ support for it via the built-in xref library. When running xref-find-definitions
by M-.
(i.e. goto) or xref-find-references
by M-?
without LSP enabled, Emacs prompts for the path to a TAGS
file. The wiki page EmacsWiki: Tags File suggests that this is a file format for indexing source code features such as identifiers.
There seems to be multiple programs capable of consuming source code files and producing indexes in the TAGS
format, including ctags, etags, and universal ctags which happens to be in the Nixpkgs collection. By default it produces a format that Emacs refuses though, so we have to pass -e
to make it produce an Emacs-compatible TAGS
file under the working directory.
# Add universal-ctags to PATH
nix shell nixpkgs\#universal-ctags
# Find .java files recursively,
# and run ctags -e a.java b.java c.java ... at once.
fd --extension java -X ctags -e {}
Once set up, the navigating experience is on par with LSP. The best part is that it’s language-agnostic, which allows navigating codebases without build system setups.
It was only after using ctags for a while that I recalled using ctags with Vim several years ago before LSP took off.↩︎