Skip to content

Instantly share code, notes, and snippets.

@Frost
Last active April 6, 2024 09:10
Show Gist options
  • Save Frost/e1e9e2f79d99381e1be6 to your computer and use it in GitHub Desktop.
Save Frost/e1e9e2f79d99381e1be6 to your computer and use it in GitHub Desktop.
Calling `eh` from vim

Calling eh from vim

I use vim, and I occasionally write Elixir code.

Vim has built-in support for looking up documentation, keywordprg.

So since I wanted to be able to look up Elixir documentation from vim, I wrote an elixir package called eh, which is available on hex.

To call eh from vim, add the following to your .vimrc:

au FileType elixir setlocal keywordprg=mix\ eh

This tells vim to use mix eh for looking up keywords.

Caveats

Vim and keywords

There is however a small caveat here, in how vim chooses what is a keyword. By default, vim does not consider periods (.) to be part of a keyword, and in Elixir, you will most likely want to look up documentation using mix eh MyModule.my_function.

There are a few ways of solving this:

  • Use my vim-eh-docs plugin, that uses some of the below techniques to look up module or function documentation using <leader>k.

  • Add . to iskeyword:

    au FileType elixir setlocal iskeyword+=.

    This will likely mess up your syntax highlighting, and possibly cause other issues.

  • Add a new mapping to your .vimrc:

    au FileType elixir noremap <Leader>k eBvt(K

    This will make \k visually select from the start of the current word to the next instance of ( on that line, and then hit K to look up documentation using mix eh.

    It will not work if you are not always using parentheses when calling functions, or when omitting parentheses because the function called takes no arguments.

  • Overload K:

    au FileType elixir noremap K eBvt(K

    This line overloads K to select what is most likely the keyword under the cursor. It selects the keyword you are currently on, and then hits K.

    Like the above above, new mapping, this will not work when there are no parentheses.

    Also, this will render it impossible to manually select those corner cases using eBvt K or eBvt$K instead.

    If anyone has a better way of automatically selecting these, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment