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

How to make Vim plugin NERDTree's <CR> behave more like `go`?

在 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.

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

我发现这只是快捷方式 go 所做的操作。但是go只能应用于files,而o应用于files and directories

在 NERDTree 的帮助文档中:

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).

更具体地说,我希望 <CR> 在应用于 directories 时表现得像 o,在 files 时表现得像 go

没有 Customisation 标志可以做到这一点。

有什么好的实现方法包括修改源脚本吗?

非常感谢!

似乎可以使用自定义映射来完成,不幸的是,在您的 .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所以没有测试。)