Home

Awesome

vim-ormolu

Introduction

This is a plugin to integrate ormolu or fourmolu into your vim workflow. It will run ormolu on Haskell buffers every time they are saved similar to gofmt. It requires ormolu be accessible from your $PATH.

Installation

First install ormolu via Cabal, Stack or Nix:

$ stack install ormolu --resolver=lts-15.10   # via stack
$ cabal new-install ormolu --installdir=/home/user/.local/bin # via cabal
$ nix-build -A ormolu   # via nix

If you are using pathogen.vim unpack this repository into your vim or neovim configuration directory.

$ cd ~/.vim/bundle         # for vim
$ cd ~/.config/nvim/bundle # for neovim
$ git clone https://github.com/sdiehl/vim-ormolu.git

If you are using Vundle add the following to your configuration file:

"Haskell Formatting
Plugin 'sdiehl/vim-ormolu'

If you are using vim-plug add the following to your configuration file:

"Haskell Formatting
Plug 'sdiehl/vim-ormolu'

Configuration

The default settings will work fine out of the box without any aditional configuration.

If you have a non-standard $PATH then set g:ormolu_command Vim variable to the location of the ormolu binary. For example if you want to use fourmolu instead pass this as the argument.

let g:ormolu_command="fourmolu"

When using fourmolu with a configuration file, fourmolu prefixes output with a "Loaded config from: ..." message. In order to prevent this from being included in the reformatted file, set

let g:ormolu_suppress_stderr=1

The specific flags for Ormolu can be configured by changing the Vim variable g:ormolu_options. For example to use faster and unsafe formatting:

let g:ormolu_options=["--unsafe"]

To disable the formatting on a specific buffer use let b:ormolu_disable=1.

To disable the formatting globally use let g:ormolu_disable=1.

If instead of formatting on save, you wish to bind formatting to a specific keypress add the following to your .vimrc or init.vim. For example to bind file formatting to the key sequence <kbd>t</kbd><kbd>f</kbd> use:

nnoremap tf :call RunOrmolu()<CR>

To toggle Ormolu formatting on a buffer <kbd>t</kbd><kbd>o</kbd> use:

nnoremap to :call ToggleOrmolu()<CR>

To disable Ormolu formatting to <kbd>t</kbd><kbd>d</kbd> use:

nnoremap td :call DisableOrmolu()<CR>

To enable Ormolu formatting to <kbd>t</kbd><kbd>e</kbd> use:

nnoremap te :call EnableOrmolu()<CR>

To format a visual block range call OrmoluBlock() function. Ormolu doesn't normally work this way and usually requires more context on the module to format. So this feature is experimental and may not function as expected. For example to bind to the key sequence <kbd>t</kbd><kbd>b</kbd> use:

xnoremap tb :<c-u>call OrmoluBlock()<CR>

If you see quirky behavior using TypeApplications extensions with the code being formatted into invalid Haskell, you probably need to enable -XTypeApplications globally because it is set in your global cabal file per this issue.

let g:ormolu_options=["-o -XTypeApplications"]

To manually install the formatter on a specific file extension invoke RunOrmolu() as a BufWritePre hook.

autocmd BufWritePre *.hs :call RunOrmolu()

To run formatting inside an unsaved buffer as a scratchpad, set your buffer's filetype to haskell and then invoke any of the formatter commands (i.e. :call RunOrmolu()) or bind it key sequence as above.

set filetype=haskell

License

MIT License Copyright (c) 2019-2020, Stephen Diehl