-
Notifications
You must be signed in to change notification settings - Fork 715
Format
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
hook global BufSetOption filetype=cpp %{
set-option buffer formatcmd 'astyle'
}
hook global BufSetOption filetype=css %{
set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
}
hook global BufSetOption filetype=d %{
set-option buffer formatcmd 'dfmt'
}
hook global BufSetOption filetype=elm %{
set-option buffer formatcmd 'elm-format --stdin'
}
hook global BufSetOption filetype=go %{
set-option buffer formatcmd 'gofmt'
}
hook global BufSetOption filetype=javascript %{
set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
}
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'
}
hook global BufSetOption filetype=markdown %{
set-option buffer formatcmd 'markdownfmt'
}
hook global BufSetOption filetype=markdown %{
set-option buffer formatcmd 'pandoc -f commonmark -t commonmark'
}
hook global BufSetOption filetype=php %{
set-option buffer formatcmd 'phpcbf -q --stdin-path="$kak_buffile" - || true'
}
hook global BufSetOption filetype=ruby %{
set-option buffer formatcmd "rubocop -x -o /dev/null -s '${kak_buffile}' | sed -n '2,$p'"
}
hook global BufSetOption filetype=rust %{
set-option buffer formatcmd 'rustfmt'
}
hook global BufSetOption filetype=zig %{
set-option buffer formatcmd 'zig fmt --stdin'
}
- Normal mode commands
- Avoid the escape key
- Implementing user mode (Leader key)
- Kakoune explain
- Kakoune TV