-
-
Save PhilRunninger/99f0c78e48de42bf2010f30d983ff61f to your computer and use it in GitHub Desktop.
" 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 |
This file needs to be put in your ~/.vim/nerdtree_plugin
folder.
@PhilRunninger thank you for creating this! Can you please provide additional instructions? I've created a file at ~/.vim/nerdtree_plugin/open_multiple.vim
with the contents above (note I manually created the nerdtree_plugin
directory). Then I try to use visual line (Shift+V) in the nerdtree file browser to select multiple files and then press o to try to open them, it simply toggles the cursor between the top and bottom line selected. Do I have to enable this plugin in my ~/.vimrc
somehow? Thanks!
@jimafisk I found I need to put it in $HOME/.vim/plugged/nerdtree/nerdtree_plugin
since .vim/plugged is my plugins directory for plugged. I bet you will find a 'nerdtree/nerdtree_plugin' that already exists in your own plugin directory, to whatever it is set.
@jimafisk, @elig0n, my guess is that you are using autocmd VimEnter * NERDTree
or something similar. This is creating your NERDTree buffer before the vimscript above is run. Therefore, o
is never remapped to call this function. I've changed the gist to use autocmd BufEnter
instead of autocmd BufNew
. That way, o
will be remapped correctly.
@PhilRunninger that was it! The updated code ^^^ works for me now :). Thank you!
Updated again to include mappings for opening files in splits or tabs. Also the location of the file has changed to ~/.vim/ftplugin/nerdtree.vim
I think you should put it in a repo like you did to this one π https://github.com/PhilRunninger/nerdtree-buffer-ops so that way you can use it in your plugin manager of choice, if it is not going to be a feature of NERDTree
in the near future. π
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 a nerdtree_plugin
?
By the way it works nice π
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
This would be very useful.. if it works. I am not very familiar with how
augroup
works.. and when I just tried pasting this in myvimrc
, visual selection of certain files and then pressingo
didnt work.Should this
.vim
file be a specific directory, for this to work?