Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NERDTree command automatically change directory and root directory

Tags:

vim

nerdtree

Is it possible that whenever I select a directory and enter that directory from the NERDTree it should become the root directory? That is, every time I select a new directory the following two commands will be triggered: cd and :NERDTreeCWD?

Thanks!

like image 326
SebiSebi Avatar asked Jun 10 '17 12:06

SebiSebi


2 Answers

set in your vimrc: let g:NERDTreeChDirMode = 2

like image 94
Bulgantamir Avatar answered Sep 28 '22 10:09

Bulgantamir


The NERDTree provides two mappings you could use manually to get this effect: Typing cd on a node changes the directory to it, and typing C on a node, "enters" it via the NERDTree.

Personally, I often combine them -- just type Ccd -- the C will enter the directory and leave the cursor on it, and a cd will change the working directory to it.

Now, if you do want to create just one mapping to use directly, you could use the NERDTree's extension mechanism. Read up on :help NERDTreeAPI for the details, but the short version is: put a file in ~/.vim/nerdtree_plugin/cd_mapping.vim with the following contents:

call NERDTreeAddKeyMap({
            \ 'key':           '_C',
            \ 'callback':      'NERDTreeEnterDirectoryAndCD',
            \ 'quickhelpText': 'Enter directory and cd into it' })

function! NERDTreeEnterDirectoryAndCD()
  let node = g:NERDTreeDirNode.GetSelected()

  exec 'cd ' . node.path.str({'format': 'Cd'})
  NERDTreeCWD
endfunction

This should do the trick with the keybinding _C. Change the key property to whatever you'd like the key to be.

like image 40
Andrew Radev Avatar answered Sep 28 '22 11:09

Andrew Radev