Skip to content

Commit 072d468

Browse files
committed
Add a NERDTreeCWD command to change tree root to CWD
1 parent 22de74a commit 072d468

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

doc/NERD_tree.txt

+13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ The following features and functionality are provided by the NERD tree:
137137

138138
In any case, the current file is revealed and the cursor is placed on it.
139139

140+
:NERDTreeCWD *:NERDTreeCWD*
141+
Change tree root to current directory. If no NERD tree exists for this
142+
tab, a new tree will be opened.
143+
140144
------------------------------------------------------------------------------
141145
2.2. Bookmarks *NERDTreeBookmarks*
142146

@@ -247,6 +251,7 @@ r.......Recursively refresh the current directory................|NERDTree-r|
247251
R.......Recursively refresh the current root.....................|NERDTree-R|
248252
m.......Display the NERD tree menu...............................|NERDTree-m|
249253
cd......Change the CWD to the dir of the selected node...........|NERDTree-cd|
254+
CD......Change tree root to the CWD..............................|NERDTree-CD|
250255

251256
I.......Toggle whether hidden files displayed....................|NERDTree-I|
252257
f.......Toggle whether the file filters are used.................|NERDTree-f|
@@ -514,6 +519,14 @@ Applies to: files and directories.
514519

515520
Change vims current working directory to that of the selected node.
516521

522+
------------------------------------------------------------------------------
523+
*NERDTree-CD*
524+
Default key: CD
525+
Map option: NERDTreeMapCWD
526+
Applies to: no restrictions.
527+
528+
Change tree root to vims current working directory.
529+
517530
------------------------------------------------------------------------------
518531
*NERDTree-I*
519532
Default key: I

plugin/NERD_tree.vim

+25
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ call s:initVariable("g:NERDTreeMapToggleHidden", "I")
140140
call s:initVariable("g:NERDTreeMapToggleZoom", "A")
141141
call s:initVariable("g:NERDTreeMapUpdir", "u")
142142
call s:initVariable("g:NERDTreeMapUpdirKeepOpen", "U")
143+
call s:initVariable("g:NERDTreeMapCWD", "CD")
143144

144145
"SECTION: Script level variable declaration{{{2
145146
if s:running_windows
@@ -171,6 +172,7 @@ command! -n=1 -complete=customlist,s:completeBookmarks -bar NERDTreeFromBookmark
171172
command! -n=0 -bar NERDTreeMirror call s:initNerdTreeMirror()
172173
command! -n=0 -bar NERDTreeFind call s:findAndRevealPath()
173174
command! -n=0 -bar NERDTreeFocus call NERDTreeFocus()
175+
command! -n=0 -bar NERDTreeCWD call NERDTreeCWD()
174176
" SECTION: Auto commands {{{1
175177
"============================================================
176178
augroup NERDTree
@@ -2936,6 +2938,8 @@ function! s:createDefaultBindings()
29362938

29372939
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapQuit, 'scope': "all", 'callback': s."closeTreeWindow" })
29382940

2941+
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapCWD, 'scope': "all", 'callback': s."chRootCwd" })
2942+
29392943
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapRefreshRoot, 'scope': "all", 'callback': s."refreshRoot" })
29402944
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapRefresh, 'scope': "Node", 'callback': s."refreshCurrent" })
29412945

@@ -3305,6 +3309,11 @@ function! NERDTreeFocus()
33053309
endif
33063310
endfunction
33073311

3312+
function! NERDTreeCWD()
3313+
call NERDTreeFocus()
3314+
call s:chRootCwd()
3315+
endfunction
3316+
33083317
" SECTION: View Functions {{{1
33093318
"============================================================
33103319
"FUNCTION: s:centerView() {{{2
@@ -3445,6 +3454,7 @@ function! s:dumpHelp()
34453454
let @h=@h."\" ". g:NERDTreeMapMenu .": Show menu\n"
34463455
let @h=@h."\" ". g:NERDTreeMapChdir .":change the CWD to the\n"
34473456
let @h=@h."\" selected dir\n"
3457+
let @h=@h."\" ". g:NERDTreeMapCWD .":change tree root to CWD\n"
34483458

34493459
let @h=@h."\"\n\" ----------------------------\n"
34503460
let @h=@h."\" Tree filtering mappings~\n"
@@ -4055,6 +4065,21 @@ function! s:chRoot(node)
40554065
call b:NERDTreeRoot.putCursorHere(0, 0)
40564066
endfunction
40574067

4068+
" FUNCTION: s:chRootCwd() {{{2
4069+
" changes the current root to CWD
4070+
function! s:chRootCwd()
4071+
try
4072+
let cwd = s:Path.New(getcwd())
4073+
catch /^NERDTree.InvalidArgumentsError/
4074+
call s:echo("current directory does not exist.")
4075+
return
4076+
endtry
4077+
if cwd.str() == s:TreeFileNode.GetRootForTab().path.str()
4078+
return
4079+
endif
4080+
call s:chRoot(s:TreeDirNode.New(cwd))
4081+
endfunction
4082+
40584083
" FUNCTION: s:clearBookmarks(bookmarks) {{{2
40594084
function! s:clearBookmarks(bookmarks)
40604085
if a:bookmarks ==# ''

0 commit comments

Comments
 (0)