問題描述
如何使 Vim 插件 NERDTree 的 表現得更像 go
? (How to make Vim plugin NERDTree's behave more like go
?)
在 NERDTree 中, 與快捷方式
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
應用於文件和目錄
。
在 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).
更具體地說,我希望 在應用於
目錄時表現得像
和 o
go
當 files
.
沒有 Customisation
標誌可以做到這一點。
有沒有什麼好的方法來實現它,包括修改源腳本?
非常感謝!
參考解法
方法 1:
It seems that it can be accomplished using custom mappings which unfortunately cannot be defined usual way in your .vimrc
. You need to distinguish if the node is a 'FileNode'
or a 'DirNode'
so the mappings must be specified in a file like ~/.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
I found the inspiration in /autoload/nerdtree/ui_glue.vim
.
- More information in
:h NERDTreeKeymapAPI
.
(I don't have NERDTree so it is not tested.)