]> Freerunner's - dotfiles.git/commitdiff
kak: update kks scripts
authorAndre Ramnitz <tux.rising@gmail.com>
Mon, 27 Oct 2025 22:33:09 +0000 (23:33 +0100)
committerAndre Ramnitz <tux.rising@gmail.com>
Mon, 27 Oct 2025 22:33:09 +0000 (23:33 +0100)
dot-local/bin/kks-buffers [new file with mode: 0755]
dot-local/bin/kks-fifo [new file with mode: 0755]
dot-local/bin/kks-files [new file with mode: 0755]
dot-local/bin/kks-filetypes [new file with mode: 0755]
dot-local/bin/kks-git-files [new file with mode: 0755]
dot-local/bin/kks-grep [new file with mode: 0755]
dot-local/bin/kks-lf [new file with mode: 0755]
dot-local/bin/kks-lines [new file with mode: 0755]
dot-local/bin/kks-md-heading [new file with mode: 0755]
dot-local/bin/kks-mru [new file with mode: 0755]
dot-local/bin/kks-select [new file with mode: 0755]

diff --git a/dot-local/bin/kks-buffers b/dot-local/bin/kks-buffers
new file mode 100755 (executable)
index 0000000..e8a7a03
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# pick buffers
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+# - bat (change to your liking) (https://github.com/sharkdp/bat)
+
+preview_cmd="bat --color=always --line-range=:500"
+history_file="$HOME/.cache/kks-buffers-history"
+
+[ -f "$history_file" ] || touch "$history_file"
+
+kks get '%val{buflist}' |
+       grep -F "$*" |
+       fzf --height 100% --layout reverse --style full --prompt 'buf> ' --preview "kks cat -b {} | $preview_cmd" \
+               --header="[c-x] delete, [c-t] new scratch" \
+               --bind="ctrl-x:execute-silent(kks send -b {} delete-buffer)+reload(kks get '%val{buflist}')" \
+               --bind="ctrl-t:execute-silent(kks send edit -scratch {q})+reload(kks get '%val{buflist}')" \
+               --history="$history_file" |
+       while read -r name; do
+               kks send buffer "$name"
+       done
diff --git a/dot-local/bin/kks-fifo b/dot-local/bin/kks-fifo
new file mode 100755 (executable)
index 0000000..0b2f212
--- /dev/null
@@ -0,0 +1,26 @@
+#!/bin/sh
+#
+# pipe stdin into Kakoune fifo buffer
+#
+# Example:
+#    make | kks-fifo
+set -euf
+
+# Fail early if no session in context
+kks env >/dev/null 2>&1 || {
+    # show error
+    kks env
+    exit 1
+}
+
+tmp=$(mktemp -d "${TMPDIR:-/tmp}"/kks-fifo.XXXXXXXX)
+fifo="$tmp/fifo"
+
+cleanup() {
+       rm -r "$tmp"
+}
+
+trap 'cleanup; trap - EXIT' EXIT INT HUP
+mkfifo "$fifo"
+kks send edit! -fifo "$fifo" '*fifo*'
+cat >"$fifo"
diff --git a/dot-local/bin/kks-files b/dot-local/bin/kks-files
new file mode 100755 (executable)
index 0000000..a5f88ee
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+#
+# pick files
+#
+# requires:
+# - fd (https://github.com/sharkdp/fd)
+# - fzf (https://github.com/junegunn/fzf)
+# - bat (change to your liking) (https://github.com/sharkdp/bat)
+
+preview_cmd="bat --color=always --line-range=:500"
+history_file="$HOME/.cache/kks-files-history"
+
+[ -f "$history_file" ] || touch "$history_file"
+
+fd --type file . "$@" |
+       fzf --multi --height 100% --style full --prompt 'files> ' \
+               --preview "$preview_cmd {}" --history="$history_file" |
+       while read -r file; do
+               kks edit "$file"
+       done
diff --git a/dot-local/bin/kks-filetypes b/dot-local/bin/kks-filetypes
new file mode 100755 (executable)
index 0000000..71e3357
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+#
+# pick filetype from Kakoune's runtime dir and set in current buffer
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+
+ft_dir="$(kks get %val[runtime])/rc/filetype"
+
+find "$ft_dir"/*.kak -type f -exec basename -s .kak {} \; |
+       fzf --height 100% --style full --prompt 'filetypes> ' |
+       xargs -I {} kks send 'set buffer filetype {}'
diff --git a/dot-local/bin/kks-git-files b/dot-local/bin/kks-git-files
new file mode 100755 (executable)
index 0000000..084dcca
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+#
+# pick files from git ls-files
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+# - bat (change to your liking) (https://github.com/sharkdp/bat)
+
+preview_cmd="bat --color=always --line-range=:500"
+history_file="$HOME/.cache/kks-files-history"
+
+[ -f "$history_file" ] || touch "$history_file"
+
+git ls-files --full-name "$(git rev-parse --show-toplevel)" "$@" |
+       fzf --multi --height 100% --style full --prompt 'files> ' \
+               --preview "$preview_cmd {}" --history="$history_file" |
+       while read -r file; do
+               kks edit "$file"
+       done
diff --git a/dot-local/bin/kks-grep b/dot-local/bin/kks-grep
new file mode 100755 (executable)
index 0000000..5ca9c88
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+#
+# search for pattern in workdir
+#
+# requires:
+# - ripgrep (https://github.com/BurntSushi/ripgrep)
+# - fzf (https://github.com/junegunn/fzf)
+# - bat (change to your liking) (https://github.com/sharkdp/bat)
+
+history_file="$HOME/.cache/kks-grep-history"
+query=""
+
+[ -f "$history_file" ] || touch "$history_file"
+[ "$(kks get %val[selection_length])" -gt 1 ] && query="$(kks get %val[selection])"
+
+rg --vimgrep '.+' "$@" |
+       SHELL=sh fzf --delimiter=":" --query="$query" --height="100%" --prompt="grep> " --history="$history_file" \
+               --preview='range="$(echo {2}-5 | bc | sed "s/^-.*/0/"):$(echo {2}+20 | bc)"; bat -r "$range" -n --color always -H {2} {1}' |
+       awk -F':' '{print $1 " " "+" $2 ":" $3 }' |
+       xargs -r kks edit
diff --git a/dot-local/bin/kks-lf b/dot-local/bin/kks-lf
new file mode 100755 (executable)
index 0000000..e37335a
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+#
+# open lf in single-pane view with current buffer selected
+#
+# requires:
+# - lf (https://github.com/gokcehan/lf)
+
+kks get '%val{buffile}' |
+       xargs -I {} lf -command "set nopreview; set ratios 1" {}
diff --git a/dot-local/bin/kks-lines b/dot-local/bin/kks-lines
new file mode 100755 (executable)
index 0000000..64ed2ba
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+#
+# jump to line in buffer
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+
+kks cat |
+       nl -ba -w4 -s' │ ' |
+       fzf --height 100% --style full --prompt 'lines> ' |
+       awk '{print $1}' |
+       xargs -r -I {} kks send "execute-keys '<esc>{}gx'"
diff --git a/dot-local/bin/kks-md-heading b/dot-local/bin/kks-md-heading
new file mode 100755 (executable)
index 0000000..acec966
--- /dev/null
@@ -0,0 +1,14 @@
+#!/bin/sh
+#
+# jump to heading in markdown file
+#
+# requires:
+# - ripgrep (https://github.com/BurntSushi/ripgrep)
+# - fzf (https://github.com/junegunn/fzf)
+
+kks cat |
+       rg -n '^#+' |
+       column -t -s ':' |
+       fzf --height 100% --style full --prompt 'heading> ' |
+       awk '{print $1}' |
+       xargs -r -I {} kks send "execute-keys '<esc>{}gx'"
diff --git a/dot-local/bin/kks-mru b/dot-local/bin/kks-mru
new file mode 100755 (executable)
index 0000000..62dfd0e
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+#
+# pick recent file
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+# - bat (change to your liking) (https://github.com/sharkdp/bat)
+#
+# for this to work, add the following in kakrc:
+# (requires sponge from moreutils: https://joeyh.name/code/moreutils/)
+# hook global BufCreate [^*].* %{
+#     nop %sh{
+#         mru=~/.cache/kak-mru
+#         echo "$kak_buffile" | awk '!seen[$0]++' - "$mru" | sponge "$mru"
+#     }
+# }
+
+preview_cmd="bat --color=always --line-range=:500"
+
+(fzf --height 100% --prompt 'mru> ' --style full --preview "$preview_cmd {}" |
+       xargs -r kks edit) < ~/.cache/kak-mru
diff --git a/dot-local/bin/kks-select b/dot-local/bin/kks-select
new file mode 100755 (executable)
index 0000000..13f7574
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# select kak session and client to set environment
+# use as `eval $(kks-select)`, eg: `alias ks="eval $(kks-select)"` in shell config
+#
+# requires:
+# - fzf (https://github.com/junegunn/fzf)
+
+command kak -clear
+
+kks list |
+       fzf -d '\t*: *' --style full \
+               --header="[c-x] kill, [c-t] new, [c-r] reload" \
+               --bind="ctrl-x:execute-silent(kks kill -s {1})+reload(sleep 0.1; kks list)" \
+               --bind="ctrl-t:execute-silent(kks new {q})+reload(sleep 0.1; kks list)" \
+               --bind="ctrl-r:reload(kks list)" \
+               --preview-window=down:0% --preview="kks send -a info; kks send -s {1} -c {2} info -markup '{2}@{+b}[{1}]'" |
+       awk -F '\t*: *' '{print "export KKS_SESSION=" $1 "; export KKS_CLIENT=" $2}'