provide-module clipboard %~ declare-option -docstring %{ Command for copying to system clipboard. } str clipboard_copy_cmd 'wl-copy' declare-option int clip_selcount 0 define-command -hidden clip-trim %{ try %{ execute-keys \n execute-keys bjGjd } } define-command -docstring %{ clip-copy [-split]: copy selections to system clipboard Set the clipboard_copy_cmd option to change the command Switches: -split ensure each selection separated by newline } -params 0..1 clipboard-copy %{ # preserve registers evaluate-commands -save-regs 'a|' %{ set-option local clip_selcount %val{selection_count} # copy selections execute-keys '"ay' # set shell register to copy command set-register | %opt{clipboard_copy_cmd} # branch based on switch execute-keys %sh{ if [ "$kak_opt_clip_selcount" -gt 1 ]; then echo ': edit -scratch' if [ ${#} = 1 ] && [ ${1} = '-split' ]; then # paste all # reduce selections to those without newline # append a newline # delete extra newlines # select all, pipe to copy cmd echo '"a\na' echo 'gj: clip-trim' echo '%' else # paste all, select all, pipe to copy cmd echo '"a%' fi echo ": delete-buffer" else echo '' fi } } } map -docstring "yank the selections into the clipboard" global user y ": clipboard-copy" map -docstring "yank the split selections into the clipboard" global user Y ": clipboard-copy -split" map -docstring "paste the clipboard" global user p " wl-paste -n" map -docstring "paste the clipboard before" global user P "! wl-paste -n" map -docstring "replace with the clipboard" global user R '"_d! wl-paste -n' ~