The note is generated by ChatGPT.
This note explains how to:
- Synchronize text copied in a remote Neovim session with your local clipboard via tmux.
- Implement a
cpwdcommand to copy the current working directory of your shell to the local clipboard. - Understand the underlying principle of how tmux buffers bridge remote Neovim and local clipboard.
1. Prerequisites
- tmux 3.3a+ (supports
load-buffer -w) - Neovim installed on the remote server
2. Principle: How tmux Buffers Bridge Remote and Local Clipboard
tmux Buffers as an Intermediate Clipboard
- tmux maintains its own buffer system independent of the local system clipboard.
- Commands:
tmux load-buffer <file>→ loads content into a buffer (like “copy”).tmux save-buffer <file>→ writes buffer content to a file (like “paste”).
Remote Neovim Integration
- Neovim running in a remote SSH session cannot access your local clipboard directly.
- Neovim can be configured to use tmux as a clipboard provider:
- Yanking text in Neovim triggers
tmux load-buffer -w -→ sends content to tmux buffer. - With
-w(tmux ≥ 3.3a), buffer content is forwarded to the local system clipboard. - Pasting triggers
tmux save-buffer -→ reads content from tmux buffer into Neovim.
- Yanking text in Neovim triggers
How
cpwdUses the Same Mechanismcpwdwrites the output ofpwdinto a tmux buffer:1tmux load-buffer -w <(pwd)With
-w, the current path is copied directly to the local clipboard, just like yanking text in Neovim.
Key Idea: tmux acts as a bridge between remote Neovim and the local clipboard. The -w option enables tmux to forward the buffer directly to the client.
3. Configure Neovim Clipboard via tmux
Lua (init.lua)
| |
4. Implement tpwd Command
Copy the current working directory to the local clipboard:
| |
Usage:
| |
5. Implement tcopy Command
| |
✅ Usage
Silent mode (only copies, no output shown):
| |
Non-silent mode (copies and prints output):
| |
📝 How It Works
tee >(tmux load-buffer -w -)→ sends stdout both to tmux and terminal."$@"→ runs the command with all arguments.-sflag sets silent mode.
5. Notes
Requires tmux 3.3a+ for
-w(local clipboard sync).Works in remote SSH sessions, syncing text to the local machine.
Can be used with Neovim registers
+.cpwdmimicszsh’scopypathfunctionality.