Last active
February 24, 2024 09:47
-
-
Save PhilRunninger/99f0c78e48de42bf2010f30d983ff61f to your computer and use it in GitHub Desktop.
Open multiple files in NERDTree at once with Visual selection and [NERDTree defaults] o, i, s, and t.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Filename: ~/.vim/ftplugin/nerdtree.vim | |
" Purpose: Given a Visual-Line selection in the NERDTree buffer, this code | |
" lets you open all selected files with the same key mappings, which, by | |
" default, are: | |
" o - open files in previous window | |
" i - open files in horizontally split windows | |
" s - open files in vertically split windows | |
" t - open files in new tabs | |
execute "vnoremap <buffer> " . g:NERDTreeMapActivateNode . " :call <SID>OpenMultiple('p')<CR>" | |
execute "vnoremap <buffer> " . g:NERDTreeMapOpenSplit . " :call <SID>OpenMultiple('h')<CR>" | |
execute "vnoremap <buffer> " . g:NERDTreeMapOpenVSplit . " :call <SID>OpenMultiple('v')<CR>" | |
execute "vnoremap <buffer> " . g:NERDTreeMapOpenInTab . " :call <SID>OpenMultiple('t')<CR>" | |
function! s:OpenMultiple(target) range | |
let curLine = a:firstline | |
while curLine <= a:lastline | |
call cursor(curLine, 1) | |
let node = g:NERDTreeFileNode.GetSelected() | |
call nerdtree#echo("Opening " . (curLine - a:firstline + 1) . " of " . (a:lastline - a:firstline + 1) . "...") | |
if !empty(node) && !node.path.isDirectory | |
call node.open({'where':a:target, 'stay':1, 'keepopen':1}) | |
endif | |
let curLine += 1 | |
endwhile | |
if g:NERDTreeQuitOnOpen | |
NERDTreeClose | |
endif | |
call nerdtree#echo("") | |
endfunction |
Switching to the ftplugin
folder allowed me to remove the augroup
code, where the BufNew
wasn't working in all cases, and BufEnter
ran more often than needed. Plugins can have any folder structure that they need. While it no longer is in the nerdtree_plugin
folder, it's still a plugin that would be dependent on NERDTree being installed. I will create a plugin that works with plugin managers, adding some new functionality.
This gist is now available as a standard Vim plugin here: https://github.com/PhilRunninger/nerdtree-visual-selection
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry I took a look at Xuyuanp/nerdtree-git-plugin (Which I always used) and saw that plugins for
NERDTree
are being wrote like I just said 😥 . But in this approach changed from~/.vim/nerdtree_plugin
to~/.vim/ftplugin/nerdtree.vim/
. Does this still count as a candidate for anerdtree_plugin
?By the way it works nice 👌