Navigation in Vim. Part 2—plug-ins
Vim is powerful of its own. Extensibility via plug-ins make it a Swiss Army knife. Don’t take it for granted, read on! This is the second part of articles about navigation in Vim. The first part, Navigation in Vim. Part one—vanilla features focused on built-in features and navigation techniques.
In this part I am focusing on enhancing the editor via plug-ins or external tools. Please keep in mind that the following list of plug-ins is what I have found especially useful, as I realize that every Vim user has his own favourite tailor-made toolset or preferable workflow. Still, I hope that even more experienced users of Vim will find something useful in this article.
In this part I will cover
- NERD tree vs. vinegar.vim file explorer
- Fuzzy search (as a way of finding files)
- Project-wide search (grep, ack, ag)
The third part will focus on
- fugitive.vim (as a Git tree explorer)
I would like to point out that extensibility via plug-ins is the second feature that makes Vim so popular. First, you get bare editor that is not equipped with all possible features that might stand in your way without the possibility to switch them off. However, with Vim solid foundation, it is only a matter of time when you start noticing patterns or things you do repeatedly. Questions such as How could I do X quicker? will occur to you naturally and as a result, you will surely be more proficient.
Do not take it for granted. Give Vim a try!
NERD tree vs. vinegar.vim file explorer
As I wrote in the first part, no editor would be complete without a file explorer. Vim by default equips you with
:e in short) explorer. To be honest, I did not find it useful and replaced it first with NERD tree that was later followed by vinegar.vim.
Project drawer vs. split explorer
Drew Neil in Oil and vinegar—split windows and the project drawer pointed out that NERD tree is a project drawer while vinegar.vim is a split explorer. Sounds strange? In short, NERD tree resembles a traditional file tree that lists files in a sidebar. Think of RubyMine, Atom or any other editor you use or might have used in the past.
Vinegar.vim, on the other hand, lets you list files in the current (split) window you are working in. If you splitted a window into four panes and used a sidebar with a tree of files, you would be confused where to open the selected file.
vinegar.vim pros and cons
- Split windows-friendly
- Opens in a directory the current file belongs to
- The way it works can confuse newcomers
NERD tree pros and cons
- A good starting point for new Vim users
- A bit more difficult than vinegar.vim
- Vim allows you to explore files in more than one way
- Try NERD tree, if you prefer opening files in a sidebar
- Vinegar.vim opens files in the currently active window (pane)
Fuzzy search is not a plug-in per se, but rather a way to find files quickly and it tolerates typos (noise). It seems to be the best file explorer companion and most often it is the quickest way to find files whose exact location you do not know.
Let’s say that you have a file
welcome_fudges.md. Normally, using a file tree, you would have to type (and know!) the whole path:
:e welcome_fudges.md. Fuzzy search, on the other hand, moves searching to the next level, letting you type the part of the path. When you type one of the terms below
- fudgesmd, or even
You will find the
welcome_fudges.md file! Notice that you do not have to type characters such as underscore, dot, or dash, which also speeds up the whole searching process. I have been using fuzzy search from Vim-CtrlSpace for almost 1.5 years now. The engine has been rewritten to Go language and can deal with projects as big as 100,000 files.
As I am also a project contributor, I can be biased, so do not take it for granted—try by yourself! If you need a simpler solution, try ctrlp.vim, popular Vim finder.
Fuzzy search cons?
One thing comes to my mind. If you have a lot of files that have the same name but stored in different folders, it might be difficult to find the correct one. In Ember.js for examples, each component may consist of the component template and its logic (
Now, having lots of such components may hinder the ultimate power of fuzziness… by displaying you too much noise. If you search for component (or as a part of the term) as in the example below, you may get the following results
Fuzzy search, depending on the implementation, may or may not highlight all occurrences of the component term, which would not help much. In those cases, you would be more successful if you knew the approximate parent folder name at least.
- Fuzzy search is a fast searching technique
- It tolerates typos
- It accepts part of the path as an input
- When you have many similarly named files fuzzy, search might not be the best solution
Project-wide search (grep, ack, ag)
In most editors you can press ⌘ ⇧ F to find files within the whole project or a given directory. By default Vim comes with a build-in grep tool, the old Unix fellow which can either be used as a stand-alone tool or as a part of Vim. The stand-alone mode may not be very convincing for newcomers so forget about it for a while.
$ grep -n kaizen _posts/* ➜ _posts/2016-01-07-recent-changes-on-blog.md:6: - kaizen $ vim _posts/2015-12-19-haxorz-unconference.md +6
The inside Vim buddy is a little more approachable, still leaving a lot to desire in terms of jumping to the next or previous item.
:grep kaizen _posts/*
This jump can be done by using the built-in quickfix list commands:
:cn) for next occurrence,
:cp) for previous occurrence
Do not fear! With unimpaired.vim which I will describe in the third part, you do not have to remember those cryptic names and can use very handy shortcuts. For some people, especially programmers, grep is not enough and fails especially short in a larger codebase. Because of Vim’s inherent flexibility nature, you can replace the default grep with tools such as ack or ag.
Project-wide search with Ag
Having the choice: ag or ack, which one should I use? Both of them are supposed to be faster than grep. I will not argue that ag I have chosen from the very beginning is the ack killer. Try both of them and use whichever fits you better.
- Vim comes with built-in grep search tool
- Default grep can be replaced with tools like ack or ag
- ack and ag is more source code-friendly
In Vim you can jump to n-th word, end of word, or even a Word. Even if the line is 80-character long, counting how many words you have to jump to get to the destination is… not very practical. You can, of course press / and enter the search term, but leave these cases for EasyMotion.
What I use most is
- Jumping to given word in line with \\\\ w
- Jumping up and down with \\\\ k and \\\\ j
Recently a new version 2.0 has been released which introduced a feature I had long been waiting for. Now with
" Custom mapping; By default <Leader> is backslash key map <Leader><leader>w <Plug>(easymotion-wl)
the targets are limited to the line the cursor is on, while previously the plug-in also marked targets in the following lines. The new option is not only faster (performance), but it also lets you ignore numerous distractions. You can see it in the examples below!
EasyMotion is more advanced than what you have seen so far. Visit the Project repository on GitHub which provides more examples.
Why the heck do I need so many tools as I already got lost among multitude ways to go back and forth? — Frustrated Newbie
As you may remember from the previous part, Vim lets you write at the speed of thought as Drew Neil explained in Practical Vim. Being proficient at something means that you do not have to spend much time on that since habits come naturally. Being equipped with various options available in Vim actually empowers you. One day, you may use Easymotion more often and replace it with some built-in feature the other day. All of that without giving it a forethought.
It is that gut feeling that lets you choose whatever you feel like.
Special thanks for dergachev the author of Screengif that made the gif demos in this article look way better than last time. :)
- Navigation in Vim. Part one—vanilla features (Selleo blog)
- Navigation in Vim. Part one—vanilla features (ryrych.pl)
- Practical Vim
- Oil and vinegar—split windows and the project drawer
- ack project page
- ag project page
- Vim-CtrlSpace project page
- ctrlp.vim project page
- EasyMotion repository on GitHub
- NERD tree repository on GitHub
- unimpaired.vim repository on GitHub
- vinegar.vim repository on GitHub
- Vimium—a Google Chrome extension
- Vim Resources on Vim-CtrlSpace
- ag vs ack performance boost for programmers