Skip to content

Commit

Permalink
feat(tmux): add popup session for vimwiki
Browse files Browse the repository at this point in the history
changes:
    + moved tmux config to .config/tmux
    + add bash script (wiki.sh) to open a tmux session
      in popup.
  • Loading branch information
PrashanthaTP committed Oct 24, 2021
1 parent 30a44fe commit 87bcac5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .config/tmux/scripts/wiki.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh

# Script to open Vimwiki dir in tmux popup

# exit if tmux not running
# -z checks if len of string is zero
# $TMX variable is automatically set if tmux server is running
[ -z "$TMUX" ] && exit 1

SESSION="wiki"

if [ "$(tmux display-message -p -F "#{session_name}")" = "${SESSION}" ];then
echo "Detaching from ${SESSION}"
tmux detach-client # detach if already in the popup
tmux display-message "detached from session:${SESSION}"
exit 1
fi

WINDOW="vimwiki"
SESSION_EXISTS=0

WIKI_INDEX_FILE="/d/dotfiles/wiki/vimwiki/index.md"

open_wiki(){
echo "Creating wiki session..."
# create a session if doesn't exist in a detached state
tmux new-session -d -s "${SESSION}" \;\
rename-window -t "${SESSION}:0" "${WINDOW}" \;\
split-window -t "${SESSION}:${WINDOW}.0" -h \;\
send-keys -t ${SESSION}:${WINDOW}.0 "vi ${WIKI_INDEX_FILE}" ENTER \;\
send-keys -t ${SESSION}:${WINDOW}.1 "cd \$(dirname ${WIKI_INDEX_FILE});clear" ENTER \;\
select-pane -t "${SESSION}:${WINDOW}.0" \;
}

tmux has-session -t "${SESSION}" &> /dev/null
[ $? == 0 ] && SESSION_EXISTS=1
[ ${SESSION_EXISTS} == 0 ] && open_wiki
echo "Attaching to session 'wiki'"
tmux display-message "Attaching to session:${SESSION}"
tmux display-popup -E -h "80%" -w "80%" "tmux attach -t ${SESSION}"

#tmux display-popup -E -h "80%" -w "80%" "tmux new-session -A -s ${SESSION} \;\
#rename-window -t 0 \"${WINDOW}\" \;\
#split-window -h \;\
#select-pane -t 0 \;\
#send-keys -t ${SESSION}.0 \"${VI_CMD}\" ENTER"

12 changes: 11 additions & 1 deletion .tmux.conf → .config/tmux/tmux.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
set -s default-terminal 'xterm-256color'

######################
# Custom scripts
######################
bind-key N run-shell -b "bash ~/.config/tmux/scripts/wiki.sh"

# credits : https://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/
######################
# Prefix
Expand All @@ -20,16 +26,19 @@ bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

unbind-key l
# by default used for last used window
bind h select-pane -L
bind l select-pane -R
bind k select-pane -U
bind j select-pane -D


######################
# Source config
######################

bind R source-file ~/.tmux.conf \; display-message "Reloading tmux config"
bind R source-file ~/.config/tmux/tmux.conf \; display-message "Reloading tmux config"

######################
# Mouse control
Expand All @@ -45,6 +54,7 @@ set -g mouse on
######################
set-option -g allow-rename off
set-window-option -g mode-keys vi
bind L last-window
######################
# Copy-Paste Mode
######################
Expand Down

0 comments on commit 87bcac5

Please sign in to comment.