A simple, cross language Vim code formatter plugin supporting both range and full-file formatting. By default, it provides configurations for the following code formatters:
- biblatex: bibtool
- css: prettier
- go: gofmt
- html: prettier
- javascript/jsx: prettier
- json: prettier
- jsonc: prettier
- markdown: prettier
- ocaml: ocamlformat
- python: black
- rust: rustfmt
- svelte: prettier + prettier-plugin-svelte
- terraform: terraform fmt
- toml: toml-sort
- typescript/tsx: prettier
- yaml: prettier
Don't like the defaults? Writing custom commands is easy!
Each Vim filetype maps to one command-line code-formatting command. This plugin supports any language code formatter as long as it:
- Reads from standard input.
- Writes to standard output.
- Is in your PATH.
vim-filetype-formatter
uses code formatters; it does not install them.
Requires:
- A recent version of Neovim or Vim 8.
- Bash (/bin/bash)
- Respects configuration files (pyproject.toml, .rustfmt.toml, .prettierrc.toml, etc)
- Accepts visually-selected ranges for any formatter
- Preserves Vim cursor location after the formatter has run
- Clear logging so you can see why a formatter is or isn't working (:LogFiletypeFormat)
- Easy debugging of user configuration (:DebugFiletypeFormat)
- Chain formatters together with Unix pipes
- Configurable, with sane defaults
- Simple, extendable codebase
- Modular: does not pollute your Vim environment with remappings / poor Vim plugin practices
If using vim-plug, place the following line in the Plugin section of your inti.vim / vimrc:
" ~/.vimrc
Plug 'pappasam/vim-filetype-formatter'
Then run the Ex command:
:PlugInstall
I personally use vim-packager, so if you'd like to go down the "package" rabbit hole, I suggest giving that a try.
From within Vim, type:
:help filetype_formatter
This plugin provides no default key mappings. I recommend setting a key mapping for normal mode and visual mode like this:
" ~/.vimrc
nnoremap <silent> <leader>f :FiletypeFormat<cr>
vnoremap <silent> <leader>f :FiletypeFormat<cr>
Default configurations may be overridden by creating our own g:vim_filetype_formatter_commands
dictionary. If you would like to map one filetype to another, see g:vim_filetype_formatter_ft_maps
. See here for specifics on how to do this.
This plugin prioritizes simplicity and ease of use on a POSIX-compliant system. Support for Windows and other non-Unix derivatives is out of scope.
Samuel Roeca samuel.roeca@gmail.com