kakpipe.sh

· ficd's pastes · raw

expires: 2026-04-18

 1#!/bin/sh
 2
 3set -e
 4
 5usage() {
 6	cat <<'EOF'
 7Usage: kakpipe [OPTIONS] [-- KAK_ARGS...]
 8
 9Edit standard input in Kakoune, then write the result to standard output.
10
11This allows Kakoune to be used in the middle of a pipeline:
12
13	command1 | kakpipe | command2
14
15Options:
16	-s, --suffix EXT  Append .EXT to the temporary file name.
17	                  Enables Kakoune syntax highlighting
18	                  and other filetype-related functionality.
19	-h, --help        Show this message and exit.
20
21All arguments after '--' are passed directly to Kakoune.
22
23Examples:
24	ls | kakpipe | wc -l
25	jq . data.json | kakpipe -s json | less
26	command1 | kakpipe -e 'set-option window indentwidth 4' | command2
27
28Author: Daniel Fichtinger <daniel AT ficd DOT sh>
29License: BSD-0-CLAUSE
30EOF
31}
32
33while [ "$#" -gt 0 ]; do
34	case "$1" in
35	-h | --help)
36		usage
37		exit 1
38		;;
39	-s | --suffix)
40		suffix="$2"
41		shift 2
42		;;
43	--)
44		shift
45		break
46		;;
47	*)
48		break
49		;;
50	esac
51done
52
53tmp="$(mktemp /tmp/kakpipe.XXXXXX${suffix:+.$suffix})" || exit 1
54trap 'rm -f "$tmp"' EXIT INT TERM HUP
55cat >"$tmp"
56exec 3>&1
57exec </dev/tty >/dev/tty
58kak "$@" "$tmp"
59exec >&3
60cat "$tmp"