Skip to content

Instantly share code, notes, and snippets.

@joepestro
Created June 7, 2011 19:56
Show Gist options
  • Save joepestro/1013011 to your computer and use it in GitHub Desktop.
Save joepestro/1013011 to your computer and use it in GitHub Desktop.
Function for .vimrc to automatically cd to the root of current tab's NERDTree
function! ChangeDirectoryToNERDTreeDir()
" fallback to parent directory
let s:path = expand("%:p:h")
" if NERDTree is present in this tab, cd to it:
if exists("g:NERDTreeDirNode.GetRootForTab().path._strForCd()")
let s:path = g:NERDTreeDirNode.GetRootForTab().path._strForCd()
endif
" don't do anything if we're restoring a session
if exists("g:SessionLoad") && g:SessionLoad == 1
return
else
exe "cd" s:path
" echo s:path
endif
endfunction
au WinEnter * call ChangeDirectoryToNERDTreeDir()
@joepestro
Copy link
Author

Whoops! Not sure how that was working for me, but I updated to use s:path now.

Also, I was getting errors when restoring from a session so there's an additional check for that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment