用vim阅读代码的最好方法

zz from http://www.linux-ren.org/thread-66415-1-1.html


刚发现了一套阅读代码的好工具,更为欣喜之处是作者北京的,在此推荐与各位,并向作者表达敬意与感谢。这套工具的核心是 srcexpl 和 Trinity,作者博客:http://weibo.com/2404376375/info

srcexpl 和 Trinity是vim的两个插件,对了,我们今天说的就是如何用vim看代码。除了srcexpl Trinity,还需要插件taglist.

安装说明:

   下载
       taglist: http://www.vim.org/scripts/script.php?script_id=273
       trinity:  http://www.vim.org/scripts/script.php?script_id=2347
       srcexpl:  http://www.vim.org/scripts/script.php?script_id=2179
   解压下载的zip文件, 复制XXXX.vim到/usr/share/vim/../plugin/或者.vim/plugin/

使用说明:

   进入需要阅读的源码,执行'ctags -R‘, 启动vim即可.
   vim启用鼠标更方便一些: set mouse=a

vim.png (238.63 KB, 下载次数: 16)

vim.png



Source Explorer (SrcExpl) : the Source code Explorer which works like the context window of "Source Insight"

created by
Wenlong Che
 
script type
utility
 
description
[GitHub] 

The latest dev version of Source Explorer and screenshots can be downloaded from GitHub Homepage: 

https://github.com/wesleyche/SrcExpl 

[Introduction] 

The plugin (srcexpl.vim) is a source code explorer based on tags and it provides an overview of the function and type definitions.It has such a mechannism that works like the context window component of "Source Insight" software. 

If you have used Source Insight for browseing C/C++/Java project before, you may find that you can browse the definitions with the Source Explorer as well as the context window of "Source Insight". 

For example, you put the cursor on a function name in the Normal mode, its definition will show on the Source Explorer window a moment later. As soon as you do the 'double-click' operation using your mouse onto the Source Explorer window which had appeared on the bottom of (G)Vim, the definition and its context will be shown on the editor window.(Just like Source Insight does). Besides, multi-definitions' listing and jumping works like the Source Insight too. In srcexpl v1.2 and above, the function key which users mapped will play a important role that you can not only use mouse but also use keyboard in order to exploring source code in both GUI and cterm VIM. 

The Source Explorer can work with 'Taglist' and 'NERD tree' very well. Both of them make (G)Vim looks like the "Source Insight" in Unix/Windows/Mac enviroment. 

[Features] 

1\ Go insight the definitions of functions and various types of C/C++/Java language project, including function, marcos, structure, array, method, class, global variable, local variable etc. 
2\ Double click using left mouse and your own map key (such as: <ENTER>) onto the  Source Explorer window can go forward to the preview context. 
3\ Popup menu and Your own map key (such as <A-,> in Source Insight) to go back from the preview context. 
4\ Auto create and update the 'tags' file intelligently. 

[Tips] 

1\ Ensure that there is only one 'tags' file in your project PATH, or it would make VIM load all of them on the same time. For example: There are two tags file in the 'your_project' directory, so we will delete the excess 'tags' file in the 'sub_directory'. 

... ... 

[chewenlong@localhost ~]$ cd your_project/ 
[chewenlong@localhost your_project]$ pwd 
/home/chewenlong/your_project 
[chewenlong@localhost your_project]$ ls 
bar.c  sub_directory  tags 
[chewenlong@localhost your_project]$ cd sub_directory/ 
[chewenlong@localhost sub_directory]$ ls 
foo.c  tags 
[chewenlong@localhost sub_directory]$ rm -rf tags 
[chewenlong@localhost sub_directory]$ ls 
foo.c 
[chewenlong@localhost sub_directory]$ cd .. 
[chewenlong@localhost your_project]$ ls 
bar.c  sub_directory  tags 
[chewenlong@localhost your_project]$ vim bar.c 

... ... 

2\ Make sure the files you are exploring is unix-format, that is, there is no '\r'(displayed as '^M') at the end of each line. Because the plugin would make a jump mistake when executing EX-commands. 

3\ If you want your VIM works like the Source Insight, just try a VIM plugin named 'Trinity (trinity.vim)': 

http://www.vim.org/scripts/script.php?script_id=2347 

This simple plugin can manage 'Source Explorer', 'Taglist' and 'NERD Tree' work together very well. 

If you want to use VIM with Source Explorer in the UNIXs-console, I suggest map the keys below to jump from one window to another. 

nmap <C-H> <C-W>h 
nmap <C-J> <C-W>j 
nmap <C-K> <C-W>k 
nmap <C-L> <C-W>l 

Below key mappings can replace the Vim feature for jumping to previously visited locations via jump list. 

nmap <C-I> <C-W>j:call g:SrcExpl_Jump()<CR> 
nmap <C-O> :call g:SrcExpl_GoBack()<CR> 

The typical actions using srcexpl.vim are: 

a\ Move the cursor onto a symbol word. 
b\ As soon as the definition of the word is displayed on the preview window, we type <Ctrl-j> to jump into the previw window. 
c\ Type the SrcExpl_jumpKey(<ENTER> as default) to jump to the exact context of its definition. 
d\ Type the SrcExpl_gobackKey(<SPACE> as default) to go back to the previous position of the symbol word. 
 
install details
1\ Unzip the archive into your ~/.vim directory. That should put srcexpl.vim in ~/.vim/plugin and srcexpl.txt in ~/.vim/doc. 

2\ Add the followings in your .vimrc or _vimrc and change them by yourself 

" // The switch of the Source Explorer                                         " 
" nmap <F8> :SrcExplToggle<CR> 
"                                                                              " 
" // Set the height of Source Explorer window                                  " 
" let g:SrcExpl_winHeight = 8 
"                                                                              " 
" // Set 100 ms for refreshing the Source Explorer                             " 
" let g:SrcExpl_refreshTime = 100 
"                                                                              " 
" // Set "Enter" key to jump into the exact definition context                 " 
" let g:SrcExpl_jumpKey = "<ENTER>" 
"                                                                              " 
" // Set "Space" key for back from the definition context                      " 
" let g:SrcExpl_gobackKey = "<SPACE>" 
"                                                                              " 
" // In order to avoid conflicts, the Source Explorer should know what plugins " 
" // except itself are using buffers. And you need add their buffer names into " 
" // below listaccording to the command ":buffers!"                            " 
" let g:SrcExpl_pluginList = [ 
"         \ "__Tag_List__", 
"         \ "_NERD_tree_" 
"     \ ] 
"                                                                              " 
" // Enable/Disable the local definition searching, and note that this is not  " 
" // guaranteed to work, the Source Explorer doesn't check the syntax for now. " 
" // It only searches for a match with the keyword according to command 'gd'   " 
" let g:SrcExpl_searchLocalDef = 1 
"                                                                              " 
" // Do not let the Source Explorer update the tags file when opening          " 
" let g:SrcExpl_isUpdateTags = 0 
"                                                                              " 
" // Use 'Exuberant Ctags' with '--sort=foldcase -R .' or '-L cscope.files' to " 
" //  create/update a tags file                                                " 
" let g:SrcExpl_updateTagsCmd = "ctags --sort=foldcase -R ." 
"                                                                              " 
" // Set "<F12>" key for updating the tags file artificially                   " 
" let g:SrcExpl_updateTagsKey = "<F12>" 
"                                                                              " 
" // Set "<F3>" key for displaying the previous definition in the jump list    " 
let g:SrcExpl_prevDefKey = "<F3>" 
"                                                                              " 
" // Set "<F4>" key for displaying the next definition in the jump list        " 
let g:SrcExpl_nextDefKey = "<F4>" 

3\ Enjoy vimming :-)

Trinity : the Trinity of taglist, NERDtree and SrcExpl: an IDE works like "Source Insight" 

created by
Wenlong Che
 
script type
utility
 
description
[GitHub] 

The latest dev version of Trinity and screenshots can be downloaded from GitHub Homepage: 

https://github.com/wesleyche/Trinity 

[Introduction] 

The Trinity plugin manages Source Explorer, Taglist and NERD Tree, and build them as a great IDE which works like the "Source Insignt". 

[Features] 

1\ Automatic Display of Declarations in the Context Window on the bottom in the (G)VIM window using the script named 'Source Explorer(srcexpl.vim)' : 
http://www.vim.org/scripts/script.php?script_id=2179 

2\ Symbol Windows For Each File on the left in the (G)VIM window (G)VIM using the script named 'taglist.vim': 
http://www.vim.org/scripts/script.php?script_id=273 

3\ Quick Access to All Files on the right in the (G)VIM window using the script named 'The NERD tree(NERD_tree.vim)': 
http://www.vim.org/scripts/script.php?script_id=1658 

4\ You can change various options of each plugin in trinity.vim according to you request. 

[Declaration] 

In order to build such an IDE that implement the main functions(function list, definition and context explorer, file brower), I had modified the source code of 'NERD_tree.zip 2.13.0'. You would note the comments which begin with 'Wenlong Che add/delete for trinity.vim' in the NERD_tree.vim.
 
install details
1\ Unzip the 'trinity.zip', and copy all the script files into to the plugin directory of Vimfiles, such as ~/.vim/plugin/ 

2\ Add the followings in your .vimrc or _vimrc and change them by yourself 

" Open and close all the three plugins on the same time 
nmap <F8>   :TrinityToggleAll<CR> 

" Open and close the srcexpl.vim separately 
nmap <F9>   :TrinityToggleSourceExplorer<CR> 

" Open and close the taglist.vim separately 
nmap <F10>  :TrinityToggleTagList<CR> 

" Open and close the NERD_tree.vim separately 
nmap <F11>  :TrinityToggleNERDTree<CR> 

3\ Enjoy vimming :-)
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值