From 958de4e03cc5c6f75065db905dda3422354c94a4 Mon Sep 17 00:00:00 2001 From: Hektor Misplon Date: Fri, 26 Jan 2024 12:27:41 +0100 Subject: [PATCH] Add 'zk' script --- .bash_aliases/all | 1 - .bin/zk | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 .bin/zk diff --git a/.bash_aliases/all b/.bash_aliases/all index b6a4678..c4b8902 100644 --- a/.bash_aliases/all +++ b/.bash_aliases/all @@ -51,7 +51,6 @@ alias path='echo -e ${PATH//:/\\n}' # Pretty print path variables # Programs alias o="xdg-open" alias v="nvim" -alias zk='cd ~/.zk && nvim "$(cat ~/.zk/current-zettel.txt)"' alias g='git' alias t=' task' alias tsh='tasksh' diff --git a/.bin/zk b/.bin/zk new file mode 100755 index 0000000..a2dab07 --- /dev/null +++ b/.bin/zk @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +if [ "$TERM_PROGRAM" = tmux ]; then + cd ~/.zk && $EDITOR "$(cat ~/.zk/current-zettel.txt)" +else + echo 'Not in tmux' + echo 'Choose an option:' + echo '1. Open in tmux' + echo '2. Open in current terminal' + read -r -p 'Enter your choice: ' choice + case $choice in + 1) + # Check if a tmux session is running with a window named zk + if tmux list-windows -F '#{window_name}' | grep -q zk; then + # Attach to the session containing the 'zk' window + session="$(tmux list-windows -F '#{window_name} #{session_name}' | grep zk | head -n 1 | awk '{ print $2 }')" + tmux attach -t "$session" + else + # Create session with a window named 'zk' and start nvim + tmux new-session -s zk -n zk -d + tmux send-keys -t zk:zk "cd ~/.zk && $EDITOR \"\$(cat ~/.zk/current-zettel.txt)\"" Enter + tmux attach -t zk + fi + ;; + 2) + cd ~/.zk && $EDITOR "$(cat ~/.zk/current-zettel.txt)" + ;; + *) + echo 'Not opening Zettelkasten' + exit 1 + ;; + esac +fi