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

前端 未结 3 1185
失恋的感觉
失恋的感觉 2021-02-08 12:27

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?

相关标签:
3条回答
  • 2021-02-08 13:08

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

    0 讨论(0)
  • 2021-02-08 13:12

    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).

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题