Skip to content
losnappas edited this page Aug 7, 2020 · 25 revisions

Formatting

format.kak is a built-in script that allows formatting multiple selections or the entire buffer.

The editor does not have language-specific parsers to format a buffer’s contents, but instead relies on third-party tools specified via the formatcmd option.

The benefit is that it can apply more clever rules than simple ones like "when <ret> is pressed after a { it should indent the new opened line". So the user can focus on just raw typing, execute the :format command and let the magic happens. This command can also be run automatically on buffer-save via hooks.

For example, a popular formatting tool in the web community is prettier.

Here are other points to consider, in relation with other built-in commands:

  • if possible, the formatting rules should be in sync with the ones used by EditorConfig

  • some LSP servers provide a format command but it’s often not very configurable

AsciiDoc

C

hook global BufSetOption filetype=c %{
    set-option buffer formatcmd 'astyle'
}
hook global BufSetOption filetype=c %{
    set-option buffer formatcmd 'indent'
}

C++

hook global BufSetOption filetype=cpp %{
    set-option buffer formatcmd 'astyle'
}

Clojure

CoffeeScript

CSS

hook global BufSetOption filetype=css %{
    set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
}

D

hook global BufSetOption filetype=d %{
    set-option buffer formatcmd 'dfmt'
}

Elixir

Elm

hook global BufSetOption filetype=elm %{
    set-option buffer formatcmd 'elm-format --stdin'
}

Go

hook global BufSetOption filetype=go %{
    set-option buffer formatcmd 'gofmt'
}

HAML

HTML

Handlebars

Haskell

INI

Java

JavaScript

hook global BufSetOption filetype=javascript %{
    set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
}

JSON

hook global BufSetOption filetype=json %{
    set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
}
hook global BufSetOption filetype=json %{
    set-option buffer formatcmd "jq --indent %opt{tabstop} ."
}
hook global BufSetOption filetype=json %{
    set-option buffer formatcmd 'python3 -m json.tool'
}

Julia

Latex

Lua

LISP

Makefile

Markdown

hook global BufSetOption filetype=markdown %{
    set-option buffer formatcmd 'markdownfmt'
}
hook global BufSetOption filetype=markdown %{
    set-option buffer formatcmd 'pandoc -f commonmark -t commonmark'
}

MoonScript

Nim

Ocaml

Perl

PHP

hook global BufSetOption filetype=php %{
    set-option buffer formatcmd 'phpcbf -q --stdin-path="$kak_buffile" - || true'
}

Python

hook global BufSetOption filetype=python %{
    set-option buffer formatcmd 'autopep8'
}
hook global BufSetOption filetype=python %{
    set-option buffer formatcmd 'black -'
}

Pony

Pug

Ragel

Ruby

hook global BufSetOption filetype=ruby %{
    set-option buffer formatcmd "rubocop -x -o /dev/null -s '${kak_buffile}' | sed -n '2,$p'"
}

Rust

hook global BufSetOption filetype=rust %{
    set-option buffer formatcmd 'rustfmt'
}

SASS & SCSS

Scala

Shell

Swift

TUP

YAML

Zig

hook global BufSetOption filetype=zig %{
    set-option buffer formatcmd 'zig fmt --stdin'
}
Clone this wiki locally