SebiSebi
SebiSebi

Reputation: 295

NERDTree command automatically change directory and root directory

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!

Upvotes: 7

Views: 8440

Answers (3)

daGo
daGo

Reputation: 2968

cd CD ( cd -> changes pwd/cwd and CD -> sets tree root to pwd/cwd)

Upvotes: 2

Bulgantamir
Bulgantamir

Reputation: 1644

set in your vimrc: let g:NERDTreeChDirMode = 2

Upvotes: 16

anon
anon

Reputation:

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.

Upvotes: 6

Related Questions