【发布时间】:2012-05-20 01:30:36
【问题描述】:
使用 VIM NERDTree 插件。
有没有办法重新映射双击文件操作以在新选项卡中静默打开文件 (T)?
【问题讨论】:
-
Shift+t 在 NERDTree 中的任何文件上。哦,抱歉,请重新阅读问题。您需要映射,而不仅仅是静默打开文件。尝试
-
您可能需要自己编辑NERDTree's code。
使用 VIM NERDTree 插件。
有没有办法重新映射双击文件操作以在新选项卡中静默打开文件 (T)?
【问题讨论】:
1 简介
这适用于 NERD 树版本 4.2.0。
2 在新标签页中打开目录和文件
如果您想在新选项卡中打开目录和文件,您只需将以下行添加到您的~/.vimrc。
let g:NERDTreeMapOpenInTabSilent = '<2-LeftMouse>'
3 仅在新标签页中打开文件
如果您仅想在新标签中打开文件,您必须做一些更复杂的事情。
在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
并替换该行
nnoremap <silent> <buffer> <2-leftmouse> :call <SID>activateNode(0)<cr>
与:
nnoremap <silent> <buffer> <2-leftmouse> :call <SID>openInTabAndCurrent(0,1)<cr>
您可以在文件NERD_tree.vim 的函数s:bindMappings() 中找到这一行。
【讨论】:
虽然我的 NERDtree 版本也被报告为 4.2.0(git cloned 2015-07-22),但似乎同时进行了一些重大的重构,因此 jens-na 在第 (3) 节中的解决方案确实做到了不转移(但似乎也没有开箱即用的解决方案)。根据下面的差异,我不得不替换 autoload/nerdtree/ui_glue.vim 中的一行。 (注意:在 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" })
【讨论】: