NERDTree command automatically change directory and root directory
Asked Answered
P

3

7

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!

Palmapalmaceous answered 10/6, 2017 at 12:1 Comment(0)
S
15

set in your vimrc: let g:NERDTreeChDirMode = 2

Shrewsbury answered 14/11, 2017 at 13:29 Comment(3)
you're my hero.Outsize
This answer is a diamond. Shine and value! Should be the accepted answer.Carious
cd CD sequence does about the same. Pros: it's blazingly fast - no lags in comparison to C Cons: longer to typeComitative
I
6

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.

Iquique answered 11/6, 2017 at 9:12 Comment(3)
Thanks Andrew! That's awesome! However, there is a problem when trying to hit the parent directory: E716 Key not present in Dictionary: path.str({'format': 'Cd'}) E15: Invalid expression: 'cd ' . node.path.str({'format': 'Cd'})Palmapalmaceous
Hmm, that's odd. Is it possible you have an older version of the NERDTree? Could you check if you've got the master version installed locally?Iquique
you save my dayChaunce
C
2

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

Comitative answered 5/4, 2020 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.