Skip to content

Commit 15a9c90

Browse files
committed
WIP: new nvim in tmux only works correctly in one tmux session - find solution for multiple sessions
1 parent bff5325 commit 15a9c90

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/zsh
2+
3+
is_nvim_running_in_current_tmux_session() {
4+
# echo 'Starting session check...'
5+
local TMUX_SESSION_NAME=$(tmux display-message -p '#S')
6+
# echo "Current tmux session name: $TMUX_SESSION_NAME"
7+
8+
# Get the list of all running nvim PIDs
9+
local NVM_PIDS=($(pgrep -f "nvim"))
10+
11+
if [[ ${#NVM_PIDS[@]} -eq 0 ]]; then
12+
echo 'No Neovim instances found at all.'
13+
return 1 # No nvim processes found
14+
fi
15+
16+
# echo "Neovim PIDs found: ${NVM_PIDS[*]}"
17+
18+
# Iterate over each PID of nvim processes
19+
for PID in "${NVM_PIDS[@]}"; do
20+
# echo "Checking PID: $PID"
21+
22+
# Ensure valid PID
23+
if ps -p "$PID" > /dev/null 2>&1; then
24+
# TODO: sesion id is not the tmux session, but user login shell session; therefore in a new tmux session this code never works as expected
25+
local SESSION_ID=$(ps -o sess= -p "$PID" | sed 's/^[^[:alnum:]]*//; s/[^[:alnum:]]*$//') # Get the session ID
26+
local TMUX_SESSION_ID=$(tmux display-message -p '#{session_id}'| sed 's/^[^[:alnum:]]*//; s/[^[:alnum:]]*$//') # Get current tmux session ID
27+
28+
# echo "Valid PID: $PID with Session ID = $SESSION_ID, TMUX Session ID: $TMUX_SESSION_ID"
29+
30+
# Check if this process belongs to the current tmux session
31+
if [[ "$SESSION_ID" == "$TMUX_SESSION_ID" ]]; then
32+
echo "$PID" # Return the PID if it matches
33+
return 0 # nvim found in current session
34+
fi
35+
# else
36+
# echo "Invalid PID: $PID."
37+
fi
38+
done
39+
40+
# echo "No Neovim found in the current session."
41+
return 1 # No nvim found in the current session
42+
}
43+

tmux-nvim

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/zsh
2+
3+
# Source the function to check nvim status
4+
source "./is_nvim_running_in_current_tmux_session"
5+
6+
# Main logic to run the nvim session
7+
NVIM_PID=$(is_nvim_running_in_current_tmux_session) # Call the function directly
8+
9+
if [[ $? -eq 0 && -n "$NVIM_PID" ]]; then # Successful call and NVIM_PID is not empty
10+
11+
# Use the found nvim PID to get the current working directory
12+
NVIM_DIR=$(lsof -p "$NVIM_PID" | grep cwd | awk '{print $9}')
13+
14+
# Check if NVIM_DIR was found and is not empty
15+
if [[ -d "$NVIM_DIR" ]]; then
16+
cd "$NVIM_DIR" || exit 1
17+
echo "Changed directory to Neovim working directory: $NVIM_DIR"
18+
else
19+
echo "Neovim running, but its working directory could not be determined."
20+
exit 1
21+
fi
22+
23+
echo "nvim is already running; Launching nvim ."
24+
nvim .
25+
# else
26+
# echo "No Neovim instance running in the current session, launching new one..."
27+
# nvim . || { echo "Neovim failed to start. Press any key to exit."; read -n 1; }
28+
fi
29+
30+
# Prevent tmux from exiting on Neovim quit
31+
exec zsh

0 commit comments

Comments
 (0)