Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
Milad edited this page Apr 8, 2020 · 30 revisions

Alchemist.vim

This plugin uses ElixirSense to give inside about your elixir project in vim.

Installation

Dependencies

  • Elixir > v1.3 to run alchemist-server
  • OTP 19 and above
  • Python elixir_sense_client is the script that talks to ElixirSense
  • vim-elixir to enable syntax highlighting, indentation and etc.

Setting up

You can use your favourite VIM plugin mannger for installation, in this doc we use Vundle

Add this line to your .vimrc

Plugin 'slashmili/alchemist.vim'

Important Notes:

  • ElixirSense is included into this plugin and YOU DON'T NEED to set it up separately
  • auto complete and jump to definition only works if your project or newly added file has been compiled. Usually it's not a problem if you are building a web app with phoenix, because it gets compiled automatically while mix phoenix.server is running.

Omni completion

Short keys

  • Auto completion: <C-x><C-o> while your are in INSERT mode.

Optional plugin

Demo

ExDoc

By default the docs are shown as markdown.

Short keys

  • Press K while cursor is under a module or function in NORMAL mode.

Commands

  • ExDoc: ExDoc [module/function] provides document (press TAB to get autocomplete).

Demo

Jump to definition

This plugin works with user defined and downloaded hex packages, for Jumping to Elixir/Erlang source code look at g:alchemist#elixir_erlang_src configs.

Short keys

  • Jump to the definition: Press <C-]> while cursor is under the keyword in NORMAL mode.
  • Jump through tag stack: Press <C-T> to jump between tag stack in NORMAL mode.

Commands

  • ExDef: ExDef [module/function] jumps to the definition.

Configs

  • Disable short keys

In case you don't like alchemist.vim semi-tag functionality!

let g:alchemist_tag_disable = 1
  • Change short keys bindings
let g:alchemist_tag_map = '<C-]>'
let g:alchemist_tag_stack_map = '<C-T>'
  • Set source path for Elixir and Erlang

If you haven't build Elixir/Erlang from source, you need to set g:alchemist#elixir_erlang_src to be able to jump to Elixir/Erlang modules.

let g:alchemist#elixir_erlang_src = "/usr/local/share/src"

This directory should contains two directories, elixir and otp

/usr/local/share/src
├── elixir
│ ├── CHANGELOG.md
│ ├── lib
│ ├── Makefile
│ ├── ...
├── otp
│ ├── AUTHORS
│ ├── lib
│ │ ├── asn1
│ │ ├── stdlib
│ │ ├── ...
│ ├── ...

Demo

IEx

IEx shell within vim requires either Neovim or ConqueShell. For ConqueShell the setting g:ConqueTerm_CloseOnEnd should be enabled to properly exit IEx.

Commands

  • IEx: IEx [command] opens a new IEx session if none exists (iex -S mix). If one already exists, it switches to that window (or reopens the window if it was closed). can take a command, which it will run. For example: :IEx h Enum.reverse
  • IExHide: hides the window that the IEx session is in.

Configs

  • Set IEx terminal size. :IEx opens a new horizontal split for the IEx session. The size of the split can be configured by changing g:alchemist_iex_term_size.
let g:alchemist_iex_term_size = 15
  • Set IEx window split
let g:alchemist_iex_term_split = 'split'

Demo

Mix

Allows you to run Mix commands from vim.

If you already have an existing Mix command/plugin in vim, Alchemist won't redefine this command.

Commands

  • Mix: Mix [command] run mix command (press TAB to get commands autocomplete).

Demos

Docker Env

If the project is compiled in a different system than the one running vim, then the path of the source file used when Jumping to Definitions (C-]) will be the one from the system where the code was compiled, instead of the local path where vim is running.

This is usually the case when you are running your project inside a Docker container and mounting the files inside the container or when you are mounting a remote filesystem locally with FUSE

setting g:alchemist_compile_basepath to the base dir where your project was compiled will allow us to translate it correctly to a relative path in the local file system.

let g:alchemist_compile_basepath = '/app/'

What about formatting Elixir files on file save?

This plugin doesn't do that for you! However it's simple to set it up in your vim

autocmd BufWritePost *.exs,*.ex silent :!mix format %