如何使Vim插件NERDTree的<cr>表现得更像`go`?

时间:2015-11-20 16:35:04

标签: vim mapping nerdtree

在NERDTree中,<CR>与快捷o相同,快捷方式Default key: o Map option: NERDTreeMapActivateNode Applies to: files and directories. If a file node is selected, it is opened in the previous window. If a directory is selected it is opened or closed depending on its current state. If a bookmark that links to a directory is selected then that directory becomes the new root. If a bookmark that links to a file is selected then that file is opened in the previous window. 在其帮助文档中描述如下:

go

我想打开一个文件,光标只保留在NERDTree 中。

我发现它只是捷径go所做的动作。但files仅适用于o,而files and directories适用于Default key: go Map option: None Applies to: files. If a file node is selected, it is opened in the previous window, but the cursor does not move. The key combo for this mapping is always "g" + NERDTreeMapActivateNode (see NERDTree-o).

在NERDTree的帮助文档中:

<CR>

更具体地说,我希望o在应用于directories时表现为go,在files时表现为Customisation

没有{{1}}标志可以做到这一点。

有没有什么好方法可以实现它,包括修改源脚本?

非常感谢!

1 个答案:

答案 0 :(得分:2)

似乎可以使用自定义映射来完成,遗憾的是,这些映射无法在.vimrc中以常规方式定义。您需要区分节点是'FileNode'还是'DirNode',因此必须在~/.vim/nerdtree_plugin/mymappings.vim等文件中指定映射。

call NERDTreeAddKeyMap({
    \ 'key': '<CR>', 
    \ 'scope': 'DirNode',
    \ 'callback': 'NERDTreeCustomActivateDirNode',
    \ 'quickhelpText': 'open or close a dirctory',
    \ 'override': 1 })

function! NERDTreeCustomActivateDirNode(node)
    call a:node.activate()
endfunction

call NERDTreeAddKeyMap({
    \ 'key': '<CR>',
    \ 'scope': 'FileNode',
    \ 'callback': 'NERDTreeCustomPreviewNodeCurrent',
    \ 'quickhelpText': 'open a file with the cursor in the NERDTree',
    \ 'override': 1 })

function! NERDTreeCustomPreviewNodeCurrent(node)
    call a:node.open({'stay': 1, 'where': 'p', 'keepopen': 1})
endfunction

我在/autoload/nerdtree/ui_glue.vim找到了灵感。

(我没有NERDTree所以它没有经过测试。)