remap NERDTree Double Click to 'T'
Asked Answered
H

2

12

Using VIM NERDTree Plugin.

Is there any way to remap the Double Click on a File action to open the file silently in a new tab (T)?

Haukom answered 20/5, 2012 at 1:30 Comment(2)
Shift+t on any file in NERDTree. Oh sorry, just re-read the question. You required mapping and not just silently opening the file. TryingSeifert
You'll probably need to edit NERDTree's code by yourself.Contributory
P
7

1 Introduction

This works for NERD tree version 4.2.0.

2 Open directories and files in a new tab

If you would like to open directories and files in a new tab you can simply add the following line to your ~/.vimrc.

let g:NERDTreeMapOpenInTabSilent = '<2-LeftMouse>'

3 Only open files in a new tab

If you only want to open files in a new tab you have to do something more sophisticated.

Add this function somewhere in NERD_tree.vim:

" opens a file in a new tab
" KeepWindowOpen - dont close the window even if NERDTreeQuitOnOpen is set
" stayCurrentTab: if 1 then vim will stay in the current tab, if 0 then vim
" will go to the tab where the new file is opened
function! s:openInTabAndCurrent(keepWindowOpen, stayCurrentTab)
    if getline(".") ==# s:tree_up_dir_line
        return s:upDir(0)
    endif

    let currentNode = s:TreeFileNode.GetSelected()
    if currentNode != {}
        let startToCur = strpart(getline(line(".")), 0, col("."))

        if currentNode.path.isDirectory
            call currentNode.activate(a:keepWindowOpen)
            return
        else
            call s:openInNewTab(a:stayCurrentTab)
            return
        endif
    endif
endfunction

and replace the line

nnoremap <silent> <buffer> <2-leftmouse> :call <SID>activateNode(0)<cr>

with:

nnoremap <silent> <buffer> <2-leftmouse> :call <SID>openInTabAndCurrent(0,1)<cr>

You can find this line in the function s:bindMappings() in the file NERD_tree.vim.

Peddada answered 12/11, 2012 at 13:49 Comment(0)
D
2

Although my NERDtree version is also reported as 4.2.0 (git cloned 2015-07-22), there seems to have been some major refactoring in the mean time, hence the solution by jens-na in section (3) did not transfer (but there still doesn't seem to be an out-of-the-box solution, either). I had to replace a line in autoload/nerdtree/ui_glue.vim, as per the diff below. (Note: tested on MacVim)

--- .vim/bundle/nerdtree/autoload/nerdtree/ui_glue.vim.backup    2015-07-22 19:39:53.000000000 +0200
+++ .vim/bundle/nerdtree/autoload/nerdtree/ui_glue.vim  2015-07-22 19:40:44.000000000 +0200
@@ -10,7 +10,7 @@
     call NERDTreeAddKeyMap({ 'key': '<MiddleRelease>', 'scope': "all", 'callback': s."handleMiddleMouse" })
     call NERDTreeAddKeyMap({ 'key': '<LeftRelease>', 'scope': "all", 'callback': s."handleLeftClick" })
     call NERDTreeAddKeyMap({ 'key': '<2-LeftMouse>', 'scope': "DirNode", 'callback': s."activateDirNode" })
-    call NERDTreeAddKeyMap({ 'key': '<2-LeftMouse>', 'scope': "FileNode", 'callback': s."activateFileNode" })
+    call NERDTreeAddKeyMap({ 'key': '<2-LeftMouse>', 'scope': "FileNode", 'callback': s."openInNewTab" })
     call NERDTreeAddKeyMap({ 'key': '<2-LeftMouse>', 'scope': "Bookmark", 'callback': s."activateBookmark" })
     call NERDTreeAddKeyMap({ 'key': '<2-LeftMouse>', 'scope': "all", 'callback': s."activateAll" })
Dreibund answered 22/7, 2015 at 18:17 Comment(1)
This seems to be the only way of doing it for the current NERDTree, also does not have problems with double click on dirs (using Linux with GVim).Komsa

© 2022 - 2024 — McMap. All rights reserved.