vimmanuals

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


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?


Solution

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