define-command -docstring %{ Format selection with shfmt -p, keeping whitespace prefix. } \ shfmt %{ evaluate-commands -save-regs | %{ set-register | %{ input=$(cat) prefix=$(printf '%s\n' "$input" | head -n1 | grep -o '^[[:space:]]*') printf '%s\n' "$input" | \ perl -pe "s/^\Q$prefix\E//" | \ shfmt -p | \ sed "s/^/$prefix/" } execute-keys '|' } } define-command -hidden sh-object-outer %~ execute-keys '%sh[{([]M' ~ define-command -hidden sh-object-inner %{ execute-keys ':sh-object-outerKJx' } define-command -docstring 'format %sh blocks in kakscript' sh-kak-shfmt %{ execute-keys -itersel -draft 'sh-object-wrap:shfmt' } map global object 'sh-object-wrap' -docstring 'shell block' try %{ declare-user-mode kak } map global kak = ':sh-kak-shfmt' -docstring 'format current sh block'