How do you use the “K”-button in the normal mode of VIM?

五迷三道 提交于 2019-12-04 18:15:05

问题


K is used to open manuals. I am interested how you use it. Can you use it to all kind of manuals, such as C, java and other things?


回答1:


Simply put, K runs the command specified by the 'keywordprg' option on the "word" under the cursor (where a "word" is a contiguous block of letters, numbers, and any other characters specified by the 'iskeyword' option).

On Unix-based systems, 'keywordprg' defaults to 'man', so anything for which the 'man' command returns a useful manual can be looked up using K in this default setup. Most Unix systems have man pages for C libraries, so you can look up C library functions pretty easily.

Most systems do not have man pages for Java, however, so to look up Java documentation you'd need to either install man pages for Java or change the 'keywordprg' setting to invoke a program (other than "man") that will display Java documentation.

Here's a Python script you could use:

#!/usr/bin/python

import urllib, os, sys, commands

os.system('firefox' + commands.mkarg(
  'http://www.google.com/search?q='
  + urllib.quote_plus(' '.join(sys.argv[1:]))
  + '+site%3Ajava.sun.com+inurl%3Ajavase%2F6%2Fdocs%2Fapi&btnI=')
  + ' &')

Save this as javaman.py, chmod +x javaman.py, put it in your path and then in vim:

:setlocal keywordprg=javaman.py

Then pressing K will invoke javaman.py which in turn will do an "I'm feeling Lucky" search on Google for the relevant Java API docs.

On non-Unix systems you may need to include python in the command:

:let &keywordprg='python javaman.py'

You'll probably also need to modify the script (for example, it currently uses "&" to background firefox, which is a Unix-ism).




回答2:


:help K can give you more explanation than anyone else here could




回答3:


Yes. You need to update the keywordprg variable to point to a program that can lookup a keyword in the language of your choice. In general you can google for particular languages and solutions. Here are a couple of them

  • Java: http://vim.wikia.com/wiki/Vim_Doclet
  • Perl: http://vimrc-dissection.blogspot.com/2009/02/vims-keywordprg-for-perl-and-vim-itself.html


来源:https://stackoverflow.com/questions/843823/how-do-you-use-the-k-button-in-the-normal-mode-of-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!