- Notifications
You must be signed in to change notification settings - Fork750
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 theformatcmd
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 isprettier.
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 byEditorConfig
someLSP servers provide a format command but it’s often not very configurable
hookglobalBufSetOptionfiletype=cpp %{set-optionbuffer formatcmd'astyle'}
hookglobalBufSetOptionfiletype=d %{set-optionbuffer formatcmd'dfmt'}
hookglobalBufSetOptionfiletype=elm %{set-optionbuffer formatcmd'elm-format --stdin'}
hookglobalBufSetOptionfiletype=go %{set-optionbuffer formatcmd'gofmt'}
hookglobalBufSetOptionfiletype=javascript %{set-optionbuffer formatcmd"prettier --stdin-filepath=%val{buffile}"}
hookglobalBufSetOptionfiletype=json %{set-optionbuffer formatcmd"prettier --stdin-filepath=%val{buffile}"}
hookglobalBufSetOptionfiletype=json %{set-optionbuffer formatcmd"jq --indent %opt{tabstop} ."}
hookglobalBufSetOptionfiletype=json %{set-optionbuffer formatcmd'python3 -m json.tool'}
hookglobalBufSetOptionfiletype=markdown %{set-optionbuffer formatcmd'markdownfmt'}
hookglobalBufSetOptionfiletype=markdown %{set-optionbuffer formatcmd'pandoc -f commonmark -t commonmark'}
hookglobalBufSetOptionfiletype=php %{set-optionbuffer formatcmd"phpcbf -q --stdin-path=%val{buffile} - || true"}
hookglobalBufSetOptionfiletype=ruby %{set-optionbuffer formatcmd"rubocop -x -o /dev/null -s %val{buffile} | sed -n '2,$p'"}
Rubocop can also be executed withbundler:
hookglobalBufSetOptionfiletype=ruby %{set-optionbuffer formatcmd"bundle exec rubocop -x -o /dev/null -s %val{buffile} | sed -n '2,$p'"}
hookglobalBufSetOptionfiletype=rust %{set-optionbuffer formatcmd'rustfmt'}
hookglobalWinSetOptionfiletype=sh %{set-optionbuffer formatcmd"shfmt"}
Installswift-format and make sure the executable is in your$PATH
. Make sure you check out the correct branch ofswift-format
— the main branch is synced to the main branch in swift, which is likely not what you need.
hookglobalWinSetOptionfiletype=swift %{set-optionbuffer formatcmd'swift-format'}
hookglobalBufSetOptionfiletype=zig %{set-optionbuffer formatcmd'zig fmt --stdin'}
- Normal mode commands
- Avoid the escape key
- Implementing user mode (Leader key)
- Kakoune explain
- Kakoune TV