psmux-plugins icon indicating copy to clipboard operation
psmux-plugins copied to clipboard

Add psmux-vim-navigator plugin (port of vim-tmux-navigator)

Open gtbuchanan opened this issue 8 months ago • 7 comments

Summary

Port vim-tmux-navigator as a psmux plugin. vim-tmux-navigator enables seamless Ctrl+h/j/k/l navigation between Vim splits and tmux panes without a prefix key, by detecting whether the active pane is running Vim.

Motivation

psmux-pain-control provides Prefix + h/j/k/l pane navigation, but it requires the prefix key and isn't Vim-aware. vim-tmux-navigator is one of the most popular tmux plugins because it makes Vim and tmux feel like a single unified workspace.

Prior Art

marlocarlo/psmux#30 confirmed the building blocks exist in psmux:

  • bind-key -n (root table bindings without prefix)
  • if-shell (conditional execution)
  • select-pane -L/-R/-U/-D

The missing piece was Windows-native foreground process detection for if-shell. The issue noted that psmux already detects foreground processes via CreateToolhelp32Snapshot for auto-rename, so extending this to if-shell conditions is feasible.

Proposed Behavior

  • Ctrl+h — move left (Vim split or psmux pane)
  • Ctrl+j — move down
  • Ctrl+k — move up
  • Ctrl+l — move right
  • When the active pane is running Vim/Neovim, send the key to Vim instead of navigating panes
  • When at the edge of Vim splits, fall through to psmux pane navigation

Workaround

Until this plugin exists, simplified (non-Vim-aware) bindings work:

bind-key -n C-h select-pane -L
bind-key -n C-j select-pane -D
bind-key -n C-k select-pane -U
bind-key -n C-l select-pane -R

gtbuchanan avatar Mar 10 '26 19:03 gtbuchanan

Hey Taylor! Great writeup and thanks for the thorough prior art research.

This is a solid feature request. The building blocks are there as you noted: bind-key -n for root table bindings, if-shell for conditional execution, and select-pane -L/-R/-U/-D for navigation.

The main piece that makes vim-tmux-navigator special is the Vim awareness. On Windows, psmux already has foreground process detection via CreateToolhelp32Snapshot (used for automatic window renaming). Extending that to if-shell conditions so plugin scripts can detect vim/nvim as the foreground process is definitely feasible.

For now, the simplified bindings you mentioned work great as a stopgap:

bind-key -n C-h select-pane -L
bind-key -n C-j select-pane -D
bind-key -n C-k select-pane -U
bind-key -n C-l select-pane -R

Keeping this open as a tracked feature request. The approach would likely be:

  1. Expose foreground process name in if-shell conditions (or via a format variable like #{pane_current_command})
  2. Build a psmux-vim-navigator plugin that uses if-shell with Vim detection
  3. Port the Vim side integration (vim-tmux-navigator Vim plugin sends keys to psmux when at the edge of splits)

Appreciate the detailed proposal!

psmux avatar Apr 04 '26 13:04 psmux

Hope it worked. Closing this assuming it did

psmux avatar Apr 22 '26 04:04 psmux

@psmux I'm not sure what you were hoping worked. You re-hashed the incomplete workaround I already provided and said you were keeping it open as a feature request

gtbuchanan avatar Jul 01 '26 20:07 gtbuchanan

My bad, i didn't read it properly and thought it was a stale issue. Reopening it again.

psmux avatar Jul 03 '26 19:07 psmux

Hey @gtbuchanan, thanks for your patience here, and sorry again for closing this on you twice with nothing but the workaround you'd already posted yourself. That was on me, not on the merit of the request. You asked for real vim awareness, and that's what this comment is finally about.

What shipped

psmux-vim-navigator in commit bd00f9c. Prefix less Ctrl+h/j/k/l navigation that, when the active pane is running vim, nvim, view, or fzf, passes the key straight through to that program instead of switching panes. Ctrl+\ for previous pane, and Prefix + Ctrl+l restored as clear screen since Ctrl+l is claimed by the new binding, matching upstream vim-tmux-navigator.

Install:

set -g @plugin 'psmux-plugins/psmux-vim-navigator'

and on the vim side, the standard plugin:

Plug 'christoomey/vim-tmux-navigator'

That vim plugin shells out to tmux at the edge of a split, and works unmodified on psmux because psmux ships tmux.exe as an alias for psmux.exe and sets $TMUX inside every pane. I checked both directly inside a psmux pane before relying on it.

What was actually tested, and how

Per the house rule around here, nothing got built on top of an assumption, everything was reproduced first. On Windows, on an isolated -L socket so it wouldn't collide with anything else running:

  • #{pane_current_command} reporting. This depends on your fix in b003802 for #299, and I confirmed it live: an idle pane reports pwsh, a pane running nvim reports nvim, a pane running vim reports vim, no placeholder, no deep-descendant noise. That's exactly the signal the vim detection regex needs, and it's why this plugin was buildable at all right now.
  • bind-key -n root table registration, if-shell -F with #{m/r:...} regex matching against #{pane_current_command}, bind-key -T copy-mode-vi, and C-\ as a key spec all register and evaluate correctly on demand.
  • Real key dispatch, not send-keys from the CLI, which bypasses the actual input handling path and would have hidden a real bug. I used WriteConsoleInput via AttachConsole + CreateFile("CONIN$") to inject genuine keystrokes into an attached psmux client's console input buffer. That's how I caught the thing that actually blocked a faithful upstream style port, see below, and also how I proved the plugin as shipped actually works: injecting Ctrl+h into an nvim pane sitting in insert mode with placeholder text correctly triggered a real backspace (the key reached vim), and injecting Ctrl+h into a plain shell pane correctly switched panes.

The catch, and how the plugin handles it today

Upstream binds each key straight to if-shell -F '<pattern>' 'send-keys <key>' 'select-pane -<dir>', re-checking the condition every keystroke. That registers fine on psmux 3.3.6 and the condition evaluates correctly when queried directly, but I found that the bound if-shell (and run-shell) command never actually fires its chosen branch when triggered by a real keypress, only when invoked directly via CLI or the command prompt. Root caused it to src/commands.rs, both handlers forward the command to the server's control port and discard the result whenever the client has a control port set, which is true for basically every attached session. Filed it separately with a minimal repro and a suggested fix at psmux/psmux#476.

Until that lands, psmux-vim-navigator uses a small background poller (scripts/poll-vim-state.ps1) that watches #{pane_current_command} for the active pane and swaps the direction keys between two plain, non conditional commands, which do dispatch correctly on a real keypress, whenever vim starts or exits. Same end result, with a bounded lag of one poll interval (default 250ms) right after vim launches or quits, and it only tracks one attached client's active pane at a time if you have several attached simultaneously. Both are called out honestly in the plugin's README so nobody hits them as a surprise.

I'm closing this one since the vim aware path genuinely works end to end today, verified with real keystrokes, not just registered bindings. #476 is the follow up if you want the zero lag upstream mechanism once that core fix lands, happy to simplify this plugin down to the one liner the day it does.

Thanks again for pushing on this, and for the original writeup, it made building the real thing straightforward once the blocker was findable.

psmux avatar Jul 17 '26 20:07 psmux

Closing as shipped and verified. Ping me here or open a fresh issue if anything about the vim side setup does not behave as documented.

psmux avatar Jul 17 '26 20:07 psmux

Quick follow up for anyone tracking this. While running the plugin's test suite after the initial landing I found that the background poller shipped in bd00f9c had a PowerShell argument binding bug: its internal helper declared a parameter named $Args, which PowerShell's automatic $args variable shadows on positional calls, so every psmux invocation inside the poller ran with no arguments and the poller exited silently. The direction keys still navigated panes, but the vim aware rebinding never actually armed from the poller.

Fixed in fcd64d1 (parameter renamed to $CmdArgs, plus two small test suite hardenings). The full suite now passes 22 of 22 on a live server, including the checks that the poller rebinds C-h to passthrough while nvim is running in the active pane and restores pane navigation after nvim exits.

If you pulled the plugin in the last few hours, please update to pick this up.

psmux avatar Jul 17 '26 20:07 psmux