NERDTree on Startup

If you use vim and you have haven't seen the excellent NERDTree plugin, you should really check it out.

I wanted NERDTree to automatically load if I start vim without arguments, so I added the following to my .vimrc:

function! StartUp()                                                                                                                                                                                         
    if 0 == argc()
        NERDTree
    end
endfunction

autocmd VimEnter * call StartUp()

Very handy. I expect I might be adding more to my StartUp function in the future.

As a side note, it installs via a Rakefile, but if you don't have Rake available, I hacked the Rakefile to make this installable via bog-standard Ruby:

require 'fileutils'                                                                                                                                                                                         
require 'find'
require 'pathname'

IGNORE = [/\.gitignore$/, /Rakefile$/]

files = `git ls-files`.split("\n")
files.reject! { |f| IGNORE.any? { |re| f.match(re) } }

vimfiles = if ENV['VIMFILES']
             ENV['VIMFILES']
           elsif RUBY_PLATFORM =~ /(win|w)32$/
             File.expand_path("~/vimfiles")
           else
             File.expand_path("~/.vim")
           end
files.each do |file|
  target_file = File.join(vimfiles, file)
  FileUtils.mkdir_p File.dirname(target_file)
  FileUtils.cp file, target_file

  puts "Installed #{file} to #{target_file}"
end

Update: fixed my thinko after Aristotle pointed it out.

5 Comments

execute 'NERDTree'”? Doesn’t just “NERDTree” work?

Nice startup function. I added it to my .vimrc. But what about reading from "stdin", e.g.:

date | vi -

If you want 'not' to open "NERDTree" if you read from STDIN for example in calling vim as:


date | vim - 

. modify your function in "vimrc" as follows:

function! StartUp()     
    if !exists("s:std_in") && 0 == argc()
        NERDTree
    end
endfunction

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * call StartUp()

Here it defines a variable only if it calls as STDIN. "NERDTree" is called only if this variable is not exist. before calling "NERDTREE".

Leave a comment

About Ovid

user-pic Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.