Vim笔记 - NERDTree

Aaron最近在尝试Atom编辑器。Atom编辑器中的Tree View可以一目了然地看到目录结构,查找目录下的文件,十分方便。Vim下和Tree View类似的目录管理插件就是NERDTree了。

如下图,NERDTree帮助文档说NERDTree是一款Vim的目录浏览插件。(不过owns your momma是什么意思?那NERDTree不就成了你爸了……)使用NERDTree可以方便地通过键盘(以及鼠标)来浏览文件系统中的目录和文件。使用NERDTree也可以进行一些简单的文件操作。

*NERD_tree.txt*   A tree explorer plugin that owns your momma!



    omg its ... ~

    ________  ________   _   ____________  ____     __________  ____________~
   /_  __/ / / / ____/  / | / / ____/ __ \/ __ \   /_  __/ __ \/ ____/ ____/~
    / / / /_/ / __/    /  |/ / __/ / /_/ / / / /    / / / /_/ / __/ / __/   ~
   / / / __  / /___   / /|  / /___/ _, _/ /_/ /    / / / _, _/ /___/ /___   ~
  /_/ /_/ /_/_____/  /_/ |_/_____/_/ |_/_____/    /_/ /_/ |_/_____/_____/   ~


==============================================================================

从Aaron三年前开始使用Vim起,NERDTree就一直作为必备插件在我的vimrc中,但是鲜有使用。故而Aaron决定花些时间来学习一下这款优秀的目录浏览插件,本文记录了NERDTree常用的命令,以及Aaron针对这款的个性化设置。

全局命令

NERDTree打开一棵新的NERDTree。

:NERDTree [<start-directory> | <bookmark>]                         *:NERDTree*
    Opens a fresh NERD tree. The root of the tree depends on the argument
    given. There are 3 cases: If no argument is given, the current directory
    will be used.  If a directory is given, that will be used. If a bookmark
    name is given, the corresponding directory will be used.  For example: >
        :NERDTree /home/marty/vim7/src
        :NERDTree foo   (foo is the name of a bookmark)

带标签的NERDTree简直是神器啊!比如,Aaron在博客工程的根目录下设置了标签blog后,在任意的工作目录下,只要输入:NERDTree blog就产生了一棵博客目录作为根结点的NERDTree。这相当于打开Vim就可以开始干活了,大大的提高工作效率。

NERDTreeToggle用于隐藏/显示NERDTree。

:NERDTreeToggle [<start-directory> | <bookmark>]             *:NERDTreeToggle*
    If a NERD tree already exists for this tab, it is reopened and rendered
    again.  If no NERD tree exists for this tab then this command acts the
    same as the |:NERDTree| command.

当我们打开所需要的文件,开始专注编辑的时候,暂时不需要NERDTree了,使用这个命令可以让NERDTree快速合上。再次输入会重新打开最近关闭的NERDTree。这个命令比较常用,因此Aaron在vimrc中做了键位映射,意为tree toggle。

nnoremap <leader>tt :NERDTreeToggle<CR>

Vim默认的<leader>键是\,Aaron将其重新映射为,

let mapleader = ','

NERDTreeFind用于在NERDTree中寻找当前文件。

:NERDTreeFind                                                  *:NERDTreeFind*
    Find the current file in the tree.

NERDTreeCWD用于将NERDTree的树根修改为当前工作路径。

:NERDTreeCWD                                                    *:NERDTreeCWD*
    Change tree root to current directory. If no NERD tree exists for this
    tab, a new tree will be opened.

书签

NERDTree中的标签永安里保存感兴趣的文件或目录。例如,你可以使用书签(bookmarks)来标记所有的工程根目录。

注意以下所有的窗口只能在NERDTree缓冲区(buffer)中使用。

Bookmark命令用于创建书签。

:Bookmark [<name>]
    Bookmark the current node as <name>. If there is already a <name>
    bookmark, it is overwritten. <name> must not contain spaces.
    If <name> is not provided, it defaults to the file or directory name.
    For directories, a trailing slash is present.

BookmarkToRoot将书签作为新的根结点。

:BookmarkToRoot <bookmark>
    Make the directory corresponding to <bookmark> the new root. If a treenode
    corresponding to <bookmark> is already cached somewhere in the tree then
    the current tree will be used, otherwise a fresh tree will be opened.
    Note that if <bookmark> points to a file then its parent will be used
    instead.

NERDTree键位映射

键位映射直接从帮助文档中复制过来,并给出Aaron的偏好程度。

偏好程度说明:0颗*表示Aaron最不喜欢到3颗*表示Aaron最喜欢。

Default  Description~                                           Aaron Preference 
Key~

o       Open files, directories and bookmarks                   **
go      Open selected file, but leave cursor in the NERDTree     
t       Open selected node/bookmark in a new tab                 
T       Same as 't' but keep the focus on the current tab        
i       Open selected file in a split window                    ** 
gi      Same as i, but leave the cursor on the NERDTree          
s       Open selected file in a new vsplit                      ** 
gs      Same as s, but leave the cursor on the NERDTree          
O       Recursively open the selected directory                  
x       Close the current nodes parent                          ** 
X       Recursively close all children of the current node       
e       Edit the current dir                                     

<CR>               same as |NERDTree-o|                         * 
double-click       same as the |NERDTree-o| map 
middle-click       same as |NERDTree-i| for files, same as
                   |NERDTree-e| for dirs 

D       Delete the current bookmark                             * 

P       Jump to the root node                                   ** 
p       Jump to current nodes parent                            *
K       跳转到与光标相同深度的第一个子节点
J       跳转到与光标相同深度的最后一个子节点
<C-J>   Jump down to the next sibling of the current directory  * 
<C-K>   Jump up to the previous sibling of the current directory *

C       Change the tree root to the selected dir                *
u       Move the tree root up one directory                      
U       Same as 'u' except the old root node is left open        
r       Recursively refresh the current directory                
R       Recursively refresh the current root                     
m       Display the NERD tree menu                              *** 
cd      Change the CWD to the dir of the selected node          *** 
CD      Change tree root to the CWD                              

I       Toggle whether hidden files displayed                    
f       Toggle whether the file filters are used                 
F       Toggle whether files are displayed                       
B       Toggle whether the bookmark table is displayed          * 

q       Close the NERDTree window                                
A       Zoom (maximize/minimize) the NERDTree window            * 
?       Toggle the display of the quick help                    *** 

自定义设置

以下是Aaron针对NERDTree插件的个性化设置:

"""""""""""NERDTree""""""""""""
"切换显示NERDtree
nnoremap <leader>tt :NERDTreeToggle<CR>

"在NERDtree中找到当前文件的位置
nnoremap <leader>tf :NERDTreeFind<CR>

"将当前目录设置为NERDTree根节点
nnoremap <leader>tc :NERDTreeCWD<CR>

" If set to 1 then the CWD is changed when the NERD tree is first loaded to the
" directory it is initialized in.
let NERDTreeChDirMode = 1

" This option is used to specify which files the NERD tree should ignore.
" All files ending in .vim or ~ will be ignored.
" All dirs ending in ".d" to be ignored and all files ending in ".o" to be ignored.
let NERDTreeIgnore=['\.vim$', '\~$', '.d$[[dir]]', '.o$[[file]]']

" If this option is set to 1 then the bookmarks table will be displayed.
let NERDTreeShowBookmarks = 1

" Display line numbers for the NERD tree window.
let NERDTreeShowLineNumbers=1

致敬

读完键位映射这个章节,我已经对NERDTree的作者肃然起敬了!这个插件可以称得上是效率发动机了!

在About中作者是这么介绍自己的。真是一个可爱的程序员。

The author of the NERD tree is a terrible terrible monster called Martyzilla
who gobbles up small children with milk and sugar for breakfast.
写于2016年11月26日