📜 ⬆️ ⬇️

Navigation in major projects for Vim and Emacs

From publications about Vim and Emacs, it seems that very few people use human code navigation in them. Or use tools like ack and ag instead. Meanwhile, for navigation in these editors there are effective tools that have been around for more than a decade. These tools are Ctags and Gnu global — they are represented in all major Linux server distributions, so navigation will work even if development is done on a remote server.

I will write mostly about Emacs, because I use it, but I will give links to the appropriate plugins for Vim. The main thing is to bring the principle of operation and the basic capabilities of the tools and discourage you from the excessive use of ack, ag and grep.


Ctags and Gnu global work roughly the same way - you feed them a folder with the project sources, and index files appear in it called tags, TAGS, GTAGS. All the symbols found in the project are stored in these files: classes, methods, functions, variables, etc. and their position in the source file. Those. compact information is sufficient to enter the class name in the editor and jump to the place of its declaration. Or get a list of files where the class with the same name is declared.
')

Ctags


Ctags is a utility that scans all the files of projects every time, without analyzing whether they have changed or not. For projects with several thousand files, it works quite quickly - up to 10 seconds. You can create separate index files for libraries and for a project, or for libraries, backend and frontend, and update only the index you need.

Emacs uses Ctags index files of the same format, but Vim and Sublime Text Editor do another. Therefore, if you generate indexes for later use in Emacs, do not forget to add the option -e .

This utility is quite old, and there are two ... not even three types. They differ little in essence, but differ greatly in the quality of the isolation of symbols. The most primitive of them is ctags bundled with Emacs, the so-called etags. It is worth using if there is nothing else - it supports many languages, but I liked only support for perl, C, Java. The command is called etags or ctags .

The second option is exuberrant ctags. The version that is supported by your distribution, and will support a couple of years, is outdated. But it supports PHP and JS parsing much better. The command is called ctags-exuberant .

The most perfect option is universal ctags. This version supports external parsers like coffeetags and generally has the best parsers for other languages. It is advisable to use this particular utility, even if it is necessary to assemble it manually.

So, you have decided which version you will use, which means you can go to the project folder and run it in the terminal: ctags -e - if you use Emacs, or just ctags , if this is a different editor.

Emacs Usage

Mx visit-tags-table and select the TAGS file. Then move the cursor to the method name and press M-. then enter. If you understand that you jumped to the wrong symbol, then press Cu M-. - so you will go to the next definition of this symbol, but in a different file. I advise you to install helm for convenient selection from the list of tags, although it will become convenient only after you get used to helm in all other places of Emacs ...

If you use Emacs and write in PHP, look here .

Use in Vim



Gnu global


Gnu Global was created to work with code bases of projects of several gigabytes in size at a time when IDEs requiring several gigabytes of work have not yet been invented. It works much like ctags, moreover, it can even use ctags as a backend for parsing - this is very convenient, because in the native way Gnu Global doesn’t support many languages. However, there is one important difference - Gnu Global does not assume that your editor will load the index file, but uses a special global utility to search for characters, which the editor calls when searching for a character.

Also, the index file is optimized so that it can be supplemented without rewriting from scratch. Due to this, if a single file has changed in the project, only he will be subjected to parsing, and not the entire project, which dramatically affects the update rate. This is convenient if you have a code of hundreds of megabytes, you are constantly switching branches or pulling the latest changes.

To create an index file, go to the project folder and invoke gtags . Wait ... But in general the speed is good - the source code for the JIdea Community Edition, which is about 150 MB of pure Java code, is indexed in less than a minute. Subsequent indexing should be run with gtags -i so that only changed files are indexed.

Emacs Usage

It is best to install the ggtags plugin, you can take it here or in elpa, melpa or marmalade. Then open one of the project files and click Mx ggtags-mode , then Mx ggtags-visit-project-root and select the folder where the tags are located. Everything further in the project files if you hit M-. , then the transition to the definition of the character that is currently under the cursor will work. You want to enter the symbol name manually - Cu M-. . If a symbol has more than one definition, then press Mn and Mp to switch between definitions. To update the tag file, click Mx ggtags-update-tags from one of the project files.

Despite the fact that ggtags and progectile both attempt to work with projects and indexing them, they do not conflict.

Use in Vim



Enjoy jumping on files.

Source: https://habr.com/ru/post/265635/


All Articles