This repository contains a Bash script that detects whether a Vim-related process (e.g., vim, nvim, or vimdiff) is running in the current tmux pane. The script resolves several challenges with identifying nested Vim processes and provides a reliable way to determine whether to send key inputs to tmux or the active Vim instance.
When using tmux with tools like vim-tmux-navigator, it's common to configure key bindings (e.g., Ctrl-h, Ctrl-j, Ctrl-k, Ctrl-l) to either:
- Switch tmux panes when not in Vim.
- Send key inputs to Vim for navigation when in a Vim pane.
This script enables you to conditionally send keys to either tmux or Vim, ensuring a seamless user experience.
-
Issues with Nested Processes:
-
Previous attempts relied on directly querying the process running in the pane's TTY using commands like:
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
-
This approach failed to correctly identify nested Vim processes, as it only checked the immediate process attached to the TTY. Vim processes launched indirectly (e.g., through subshells or scripts) were missed.
-
-
Process Tree Complexity:
- Tmux panes often have multiple layers of processes (e.g., shell, background tasks, Vim).
- A reliable solution needed to traverse the entire process tree to identify Vim-related commands.
The script works by:
-
Retrieving the Pane PID:
-
It identifies the PID of the tmux pane's shell using:
tmux display -p "#{pane_pid}"
-
-
Finding Descendant Processes:
- It traverses the process tree to identify all descendant processes of the pane's shell, including nested Vim processes.
-
Checking for Vim Processes:
-
It searches the descendant processes for Vim-related commands (
vim,nvim,vimdiff, etc.) using:ps -o args= -p "$descendant_pids" | grep -iqE "(^|/)([gn]?vim?x?)(diff)?"
-
-
Exit Status:
- Exit 0 (True): A Vim-related process is detected.
- Exit 1 (False): No Vim-related process is found.
-
Clone the Repository
git clone <repository-url> cd <repository-folder>
-
Make the Script Executable
chmod +x is_vim.sh
-
Integrate with Tmux Update your
~/.tmux.confto use the script for conditional key bindings:# Example key bindings bind-key -n 'C-h' if-shell "/path/to/is_vim.sh" 'send-keys C-h' 'select-pane -L' bind-key -n 'C-j' if-shell "/path/to/is_vim.sh" 'send-keys C-j' 'select-pane -D' bind-key -n 'C-k' if-shell "/path/to/is_vim.sh" 'send-keys C-k' 'select-pane -U' bind-key -n 'C-l' if-shell "/path/to/is_vim.sh" 'send-keys C-l' 'select-pane -R'
-
Reload Tmux Configuration
tmux source-file ~/.tmux.conf -
Test
- Open Vim or Neovim in a tmux pane and use your key bindings.
- The script should correctly identify whether to send keys to tmux or Vim.
- Performance: Traversing the process tree for every key press can introduce slight latency in complex setups.
- Process Matching: Relies on process names to detect Vim; custom setups with non-standard names may require modifications to the
greppattern.
This project is licensed under the MIT License. See the LICENSE file for details.