Vim and Large Projects


Watch out emacs, we have an over-achiever here

[linkstandalone]

Going through a large code project can be pretty daunting, and I've got some specific tips how to navigate. I'm gonna cover five main things: (1) file navigation (nerdtree), (2) quickly finding files (ctrlp) in a directory and sub-directories (and sibling directories as well, I think), (3) search for text across files in the directory (and child directories) (vimgrep + vim-unimpaired) (basic functionality built-in), (4) utilize Vim's tabs (built-in), and (5) using read-only mode (built-in).


The first thing is to use Vim. VSCodium (a FOSS program, not the Microsoft thing) is a pretty solid program, and it has Vim capabilities you can enable. However, straight Vim (actually, I've been messing around with neovim lately) is just solid and snappy. This isn't a Vim tutorial post though, so I will assume you know Vim here (Vim is HIGHLY HIGHLY worth learning - even if you don't code. LaTeX + Vim is a killer combo). (I recommend Vim over VSCodium because I'm a command-line junkie. It consumes low system resources, it's extremely uncluttered (VSCodium is full of stuff I frankly find unnecessary), etcetera etcetera. To me it's great because I can go into Vim to make a quick change, but if it turns out I need to dig in, I have the whole machinery available. With VSCodium, it's just a whole thing. But in terms of getting down into the weeds into a big code project, one is basically about as good as the other).


First, we'll need some extensions (largely from Ben Awad's 'How to Configure Vim like VSCode' video). Watch that video, and then come back, no sense in repeating what's been said (although I don't personally care about the vim-colorschemes plugin, the vim-devicons plugin, enough to try and make it work, although I can see the benefits). Only thing I WILL say is that you don't need tmux for this.


One small thing - I can't get the location toggle feature to work (where ^n toggles nerdtree AND shows your position in the directory). I've got a clumsy workaround for now - ^m shows you your location in the directory, although you still need to press ^n to toggle off the menu.


Now if you're new to Vim (or VSCodium), you might be wondering what the purpose of all of that stuff is (if it was all crystal clear why to use that stuff, probably this post isn't for you lol). First, nerdtree allows you to navigate the folder system, and see where you're at. This is helpful in orienting yourself in the directory organization.


First up, is ctrlp (or ^p). Often times, a file/script will call another file/script in the project. For example, in Java, you might be in CrazyTrain.java, which contains the class CrazyTrain that implements the interface class Train, in the file Train.java. Well, you might be interested in looking at the file Train.java. To do so, you'll simply press ^p, and type Train.java - the file should appear, at least be one of the options. So control+p is super helpful for jumping around a project quickly and efficiently, without breaking flow.


Alright, next is vim-gitgutter. This is a nice extension inspired by Sublime (I believe), which shows next to each line if you've either (A) changed the line, (B) subtracted a line, or (C) added the line. This is handy for version control stuff - ie the business of git. Git is a fantastic tool to help you manage changes to your project.


Now we're gonna leave the land of Ben Awad's video. First, it's time for the vim-unimpaired plugin. Now suppose you're in a Java class, SuperUltraCrazyTrain.java, which you know is the great great great great descendent of a family of classes. Now suppose that in some other script in the project, the method gofaster(int x) of SuperUltraCrazyTrain.java is called. Now you're not sure which class in particular in the family tree of Train.java that this particular method is actually written in. One approach could be to ^p to SuperUltraCrazyTrain.java, search, if it's not there, ^p its parent class, search, and repeat. But this is a bit tedious.


This is where vimgrep comes in. Vimgrep is a built in functionality to Vim which you use by typing (given the example of looking for gofaster): ':vimgrep /gofaster/ **'. Th ** basically allows you to search through all of the branch directories of the directory your in (so everything you want to look in), and the '/.../' allows you to enter search terms, even RegEx I believe. The problem is, navigating between the matches is annoying. I forget the key combination, that's how annoying it is.


To alleviate this problem, use vim-unimpaired! This allows you to simply enter the hotkey sequence `]q` (type in sequence, not at same time) to go forward to the next result, and `[q` to go back to the previous result. This might seem small, but it's HUGE. In some C++ projects I've looked at, where different things (objects?) are defined in scripts with names that don't obviously connect with the thing, so just searching through the filenames (ie ^p) isn't much help. But had I had vimgrep + vim-unimpaired, I'd be sane.


(If you use VSCodium, I think there is a similar functionality. Although I don't know much about it or how it works, I'm pretty sure it's there.)


The next big thing is not even a plugin: it's built into Vim! If you type `:tabnew`, it will open up a new tab in Vim. If you type `gt` (in sequence, not simultaneous), it will go to the next tab (notice how `g` takes you to places. For example, `gg` takes you to the beginning of a file, and `G` takes you to the end of a file). If you type `gT`, it will take you to the previous tab. If you want to close a tab, either type `ZZ` (which saves and closed the vim buffer(?)), or type `:tabclose`.


So that's the rundown on reading through a large project. Half of the battle is dealing with that. What Vim is absolutely great for is letting you 'get in the zone' with whatever you're working with. It's so easy to digest a large project with this setup, I needed to share.


One last tip, if you run vim with the option `-M`, you'll be in read only mode. This is great if you want to make sure you don't accidentally change anything. Everything in this post can be done in only read only mode, and it makes for fast and secure reading, without worrying you'll accidentally change anything. In my .bashrc file, I have added the alias `alias vimm='vim -M'` so I can enter read-only vim mode just by typing `vimm filename`, and it's great.