Getting shit done on Windows, part 2.2: Using NERDTree to level up your awesome

Getting shit done on Windows, part 2.2: Using NERDTree to level up your awesome

Aug 02
Getting shit done on Windows, part 2.2: Using NERDTree to level up your awesome

Right, since you’ve obviously read my previous post on being awesome with gVim, taken it all to heart and have installed gVim you’re now chomping at the bit to be even more awesome (unless you’re Scott, who clearly already possesses all the awesome a single human body can hold as far as Vim goes…). Being able to fling code around in Vim does make you awesome, I agree, but to round of the whole Vim experience there is something lacking.

Even with the menu bar, opening and creating files, creating directories, renaming files, these things are still a drag, especially on Windows with mixed case and spaces in directory names. Is this the best we can do? Typing file names with auto-complete?

Nay! There is more to life than the TAB key! There is NERDTree: it completes you (up to the point where you want auto complete for Python/Django, it won’t help for that at all).

What is NERDTree I hear you ask? Let me tell you! NERDTree is a plugin for Vim (and, obviously, gVim) that gives you a tree view of a directory structure in a split buffer to the left of your editable area. It allows you to, drum roll, visually browse for files, create files, open files, create directories, rename files and directories and otherwise enables profound awesome by providing an ncurses’esque interface, via Vim, to your file system.

It looks a bit like this:

Midget NERDTRee, shrunken for your perusal...

NERDTree: I must have it now!

Yes, you must. And you can get it either from here: http://www.vim.org/scripts/script.php?script_id=1658 or you can download it from me here: NERD_tree.zip

Inside the ZIP file you will find three directories. Copy them (or their content) as indicated. Stat! Of course, I am going to assume you installed the same version of Vim as I did and it installed to ‘Program Files’ but substitute ‘Program Files (x86)’ if you’re lucky enough to have a 64 bit OS. Which I do. So… I am not entirely sure why I am carrying on with the 32 bit explanation… Anyway:

  • The contents of directory ‘doc’ gets copied to: ‘C:\Program Files\Vim\vim73\doc’
  • The directory ‘nerdtree_plugin’ is copied to ‘C:\Program Files\Vim\vim73\’
  • The contents of directory ‘plugin’ is copied to ‘C:\Program Files (x86)\Vim\vim73\plugin\’

Exit Vim and start it up again, if all went well, it will auto load the plugin. But you can’t see it. More to follow. Soon. Of course, if it doesn’t load… dude, seriously, just follow the damn instructions. If the instructions are wrong… nah, couldn’t happen right? Right.

NERDTree: Let the awesome begin!

So, to show a lovely NERDTree, just type the command:

<esc>:NERDTree<enter>

Behold the magnificence of the NERDTree (if you don’t, the case, I bloody meant it when I said it matters)! The buffer containing the tree should have focus so the up and down arrows should work as expected.  Enter will expand a tree or open a file.  Browse around. Open a file. Feel the awesome.

After you open a file, the file buffer will have focus. To get back to the NERDTree press Ctrl+Tab (this will cycle through open buffers… so obviously this is also how you get back to the file buffer). Closing a file with :q! will work as expected, it will leave the NERDTree buffer open with focus so you can browse and open another file. Or not. The quit command (:q!) will work the same way as it does on a file in the NERDTree buffer: it will close it.

NERDTree: Enhance my awesome even moar!

So, that one guy who downloaded my settings file and (epic) colour scheme (that doesn’t work in terminal… oops…) has a little less to do since F2 is already mapped to toggling NERDTree on and off. For the rest of you (and by rest, I mean those of you who don’t already know how to do this (WTF are you reading this for anyway?)), we need to add a couple of things into the settings file. Thankfully, it’s pretty simple. What could possibly go wrong? Nothing. Nothing could go wrong.

Do this:

  • Run gVim
  • Type the command: <esc>:e ~/_vimrc<enter>

You should now see a glorious Vim settings file. Before you continue, decide which function keys you want to map to where. As I said in my previous post, I map F2 to toggle NERDTree on and off, F4 to open NERDTree in my sandbox directory and F5 to open NERDTree in my Dropbox directory. Decide what you want and substitute the path you want for what I put below. If you don’t understand what I just said… go away. Now.

Add the following lines (assuming they’re not already there) to the bottom of your _vimrc file:

map <F2> <ESC>:NERDTreeToggle<RETURN>
map <F4> <ESC>:NERDTree c:\Sandbox<RETURN>
map <F5> <ESC>:NERDTree c:\Users\shawn\Dropbox<RETURN>

As you can probably guess, ‘map’ means… er… map… <F2> means… er… F2 oh fuckit. You get the point. Change the ‘c:\Sandbox’ and/or ‘c:\Users\shawn\Dropbox’ to whatever you want, or delete the line, or whatever.

Once you’ve done that, save the file and exit (you need to restart Vim… well you could… nah, you need to restart Vim):

<esc>:wq!<enter>

Fire good old gVim up again and you should be good to go! Give her a test drive: F2 should show NERDTree, F2 again should hide it. And the other function keys you mapped should open the directories you configured. If they don’t… just give up now. Delete gVim, it’s not for you. Seriously.

NERDTree: The tree of the future... maybe...

NERDTree: So is that all?

Nein! It is not all! NERDTree does a couple of other awesome things also. Let me tell you them. (All of the following assumes NERDTree is open and has focus).

Pressing ‘r’ will reload the directory tree below the item you’re on.  So if things have changed (new files, new directories) ‘r’ will reload or refresh the tree below the highlighted item and show you these changes.

Pressing ‘m’ will bring up the file management menu. This will allow you to create a file or directory, rename a file or directory or delete the highlighted file or directory.

To accelerate your awesome acquisition, here’s some recipes.

Create a file:

  • Focus NERDTree
  • Highlight the directory you want to create the file in
  • Press ‘m’
  • Press ‘a’
  • Type the name of the file (test.txt, do not end with a ‘/’)
  • Press enter

An empty file with that name will have been created and should be highlighted. Pressing enter again will open that file. Awesome? Yes. Very.

Deleting a file:

  • Focus NERDTree
  • Highlight the file you want to delete
  • Press ‘m’
  • Press ‘d’
  • Press ‘y’

Like a boss. The file would know who the boss was… if the file wasn’t DEAD. Ah… yea.

Create a directory:

  • Focus NERDTree
  • Highlight the directory you want to create the directory in
  • Press ‘m’
  • Press ‘a’
  • Type the name of the directory but DO end it with a ‘/’ (for example, type ‘test/’)
  • Press enter

Behold! A new directory sees the light of day!

NERDTree: …and in conclusion…

Isn’t NERDTree just the bees knees? The only correct and acceptable answer is ‘YES SIR’. Can you feel the aura of awesome that now surrounds you? That’s right: gVim, using it makes you awesome like a boss.

 

6 comments

  1. Yermek

    Hi! I followed your instructions and I am surrounded by the aura of awesomeness! 🙂

    I’ve got one problem tho.. NERDTree can’t open any file “..function 19_KeyMap_Invoke..32..31..19_activateFileNode..56..75..118..119..136:”
    line 1

    do you know what the problem could be?

  2. .com

    Great stuff – Saved to favorites, facebook liked, and a google plus 1!
    I don’t think the googleplus 1 went through some kind of error. But at least I tried, right? That’s what is important.
    Keep pumping out content like this and your site will be at the very top very fast!

  3. Abhilash Nair

    Thank you Shawn for putting up this document.

  4. Chris Kavanagh

    Awesome! Thank you for the installation instructions, and a great article.

Trackbacks/Pingbacks

  1. My Vim Key Maps | James Lin's Blog - [...] Finally I have decided to move over to vim, if you would like to do the same, please spend…

Leave a Reply