Skip to content

Commit 1dc3891

Browse files
committed
Merge pull request preservim#204 from techlivezheng/feature/chrootcwd
Add a NERDTreeCWD command to change tree root to CWD
2 parents dff80ff + 072d468 commit 1dc3891

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

doc/NERD_tree.txt

Lines changed: 13 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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
@@ -2943,6 +2945,8 @@ function! s:createDefaultBindings()
29432945

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

2948+
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapCWD, 'scope': "all", 'callback': s."chRootCwd" })
2949+
29462950
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapRefreshRoot, 'scope': "all", 'callback': s."refreshRoot" })
29472951
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapRefresh, 'scope': "Node", 'callback': s."refreshCurrent" })
29482952

@@ -3327,6 +3331,11 @@ function! NERDTreeFocus()
33273331
endif
33283332
endfunction
33293333

3334+
function! NERDTreeCWD()
3335+
call NERDTreeFocus()
3336+
call s:chRootCwd()
3337+
endfunction
3338+
33303339
" SECTION: View Functions {{{1
33313340
"============================================================
33323341
"FUNCTION: s:centerView() {{{2
@@ -3467,6 +3476,7 @@ function! s:dumpHelp()
34673476
let @h=@h."\" ". g:NERDTreeMapMenu .": Show menu\n"
34683477
let @h=@h."\" ". g:NERDTreeMapChdir .":change the CWD to the\n"
34693478
let @h=@h."\" selected dir\n"
3479+
let @h=@h."\" ". g:NERDTreeMapCWD .":change tree root to CWD\n"
34703480

34713481
let @h=@h."\"\n\" ----------------------------\n"
34723482
let @h=@h."\" Tree filtering mappings~\n"
@@ -4077,6 +4087,21 @@ function! s:chRoot(node)
40774087
call b:NERDTreeRoot.putCursorHere(0, 0)
40784088
endfunction
40794089

4090+
" FUNCTION: s:chRootCwd() {{{2
4091+
" changes the current root to CWD
4092+
function! s:chRootCwd()
4093+
try
4094+
let cwd = s:Path.New(getcwd())
4095+
catch /^NERDTree.InvalidArgumentsError/
4096+
call s:echo("current directory does not exist.")
4097+
return
4098+
endtry
4099+
if cwd.str() == s:TreeFileNode.GetRootForTab().path.str()
4100+
return
4101+
endif
4102+
call s:chRoot(s:TreeDirNode.New(cwd))
4103+
endfunction
4104+
40804105
" FUNCTION: s:clearBookmarks(bookmarks) {{{2
40814106
function! s:clearBookmarks(bookmarks)
40824107
if a:bookmarks ==# ''

0 commit comments

Comments
 (0)