Skip to content
Dmitry Matveyev edited this page Aug 13, 2024 · 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}"
}
hook global BufSetOption filetype=css %{
    set-option buffer formatcmd "npx stylelint --fix --stdin-filename='%val{buffile}'"
}

D

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

Elixir

hook global BufSetOption filetype=elixir %{
    set-option buffer formatcmd 'mix format -'
}

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

hook global WinSetOption filetype=html %{
    set-option buffer formatcmd "run(){ tidy -q --indent yes --indent-spaces %opt{tabstop}  2>/dev/null || true; } && run"
}

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=%val{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 %val{buffile} | sed -n '2,$p'"
}

Rubocop can also be executed with bundler:

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

}

Rust

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

SASS & SCSS

Scala

Shell

hook global WinSetOption filetype=sh %{
    set-option buffer formatcmd "shfmt"
}

Swift

Install swift-format and make sure the executable is in your $PATH. Make sure you check out the correct branch of swift-format — the main branch is synced to the main branch in swift, which is likely not what you need.

hook global WinSetOption filetype=swift %{
    set-option buffer formatcmd 'swift-format'
}

TUP

YAML

Zig

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