-
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=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=%val{buffile} - || true"
}
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'"
}
hook global BufSetOption filetype=rust %{
set-option buffer formatcmd 'rustfmt'
}
hook global WinSetOption filetype=sh %{
set-option buffer formatcmd "shfmt"
}
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'
}
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