Wednesday, May 9, 2012

Perldoc in Vim

昨天上 NA 時用 vim 寫筆記

寫到一半不小心按到 CapsLock 鍵,然後我按下 k 時竟然彈出了神奇的 man !!!

:help K 看一下,竟然有有趣的東西XDD

K                       Run a program to lookup the keyword under the           
                        cursor.  The name of the program is given with the      
                        'keywordprg' (kp) option (default is "man").  The       
                        keyword is formed of letters, numbers and the           
                        characters in 'iskeyword'.  The keyword under or        
                        right of the cursor is used.  The same can be done      
                        with the command >                                      
                                :!{program} {keyword}                           
<                       There is an example of a program to use in the tools    
                        directory of Vim.  It is called 'ref' and does a        
                        simple spelling check.                                  
                        Special cases:                                          
                        - If 'keywordprg' is empty, the ":help" command is      
                          used.  It's a good idea to include more characters    
                          in 'iskeyword' then, to be able to find more help.    
                        - When 'keywordprg' is equal to "man", a count before   
                          "K" is inserted after the "man" command and before    
                          the keyword.  For example, using "2K" while the       
                          cursor is on "mkdir", results in: >                   
                                !man 2 mkdir                                    
<                       - When 'keywordprg' is equal to "man -s", a count       
                          before "K" is inserted after the "-s".  If there is   
                          no count, the "-s" is removed.                        
                        {not in Vi}


K 會對著現在游標的 word 去找 man ,這個功能寫 Shell script  應該很好用XDD

然後這個功能給我了一個啟發,喜歡寫 Perl 的我,何不把這功能跟 perldoc 結合起來呢?

於是我在我的 vimrc 底下寫了這個

au FileType perl nmap K :!perldoc -f <cword><CR>


用 autocmd 來幫我完成從 perldoc 找到現在的 cword 去匹配裏面的說明

原本很開心的以為這是我自己原創的 hack

後來 google 了一下  perlmonks.org  上早就有人問過了XD

perlbuzz.com 的文章跟我幾乎一模一樣 XDDD

他還去找了非內建函式的 perldoc

於是我把我的版本修改成這樣 (多一個 <CR> 讓離開 less 時不用多按一下 Enter)

au FileType perl nmap K :!perldoc <cword> <bar><bar> perldoc -f <cword><CR><CR>


不過其實很多 vim plugins 就已經有類似的功能了

http://vim.sourceforge.net/scripts/script.php?script_id=1913
http://vim.sourceforge.net/scripts/script.php?script_id=1217
http://www.vim.org/scripts/script.php?script_id=209

No comments:

Post a Comment