Vim and NERD Tree: always share the same tree across all tabs
After years associating vi/Vim with the idea of some archaic mumbo-jumbo, I started becoming a heavy Vim user about one year ago, mostly influenced by my ACM-ICPC teammates and fellow competitors. And I couldn’t be happier with it; actually, it is because of it that I try to stick to the keyboard and ditch the mouse/trackpad for as long as I can (now I’m also a Vimium user to the core). Since then, I’ve been getting deeper and deeper into its intricacies, and found some pretty useful plugins along the way.
NERD Tree is one of those essential plugins that help you stay on Vim unless you have a really good reason not to. It opens a new window displaying a file system tree, allowing you to open files quickly, create bookmarks and some other nifty things. This catonmat post describes it in more detail, and suggests that you map the :NERDTreeToggle command (which makes the tree window appear or disappear) to one of your function keys.
This is surely useful, but if you’re like me, you open a Vim instance for one of your projects and open a tab for each of your project’s files you’re currently dealing with. Problem is, invoking :NERDTreeToggle by itself generates a new independent tree for each tab. Not really practical, but there’s a way to solve that. NERD Tree has a :NERDTreeMirror command, which makes the current tab share the same tree with another tab (and, consequently, any change done to one of the trees will be reflected upon the other). But we don’t want to type it down every time we open a new tab, do we? The following snippet maps a combination of the two mentioned commands to the F2 key, making sure that all trees opened in all tabs are actually the same. Drop it in your .vimrc file:
" guarantees that the NERDTrees for all tabs will be one and the same map <F2> :NERDTreeToggle \| :silent NERDTreeMirror<CR>
Since any tree opened after the first will only have one choice for mirroring (because all previous trees mirror the first one), :NERDTreeMirror will never ask you to choose which tree to mirror. :NERDTreeMirror is executed through :silent because otherwise it would alert "NERDTree: No trees to mirror" whenever you toggled the first opened tree or a tree that was already mirroring the first one.
As far as I can tell, the current version of NERD Tree, 4.1.0, doesn’t provide a more straightforward way to do this; maybe future versions will present an alternative. Or maybe I should fork it… =)
The obligatory generic first post
So, this is where this blog starts. As a hacker and a student, what I mostly want to do here is to share my professional experiences and other useful information I happen to come across. And, as a normal person (and yes, surprisingly enough, there’s always a normal person behind a hacker), I’ll probably feel compelled to write about more mundane stuff every now and then.
Oh yes, the title of this blog is a homage to a song by one of my most favorite bands of all time, Idlewild. If you’re into good rock music, you should check them out. ASAP. (And also British Sea Power, while we’re at it)
Now, just sit back and relax. I hope you enjoy the ride.