2025-10-23    2026-01-01    614 字  2 分钟

The note is generated by ChatGPT.

This note explains how to:

  1. Synchronize text copied in a remote Neovim session with your local clipboard via tmux.
  2. Implement a cpwd command to copy the current working directory of your shell to the local clipboard.
  3. 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

  1. 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”).
  2. 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.
  3. How cpwd Uses the Same Mechanism

    • cpwd writes the output of pwd into a tmux buffer:

      1
      
      tmux 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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if vim.env.TMUX then
  vim.g.clipboard = {
    name = 'tmux-clipboard',
    copy = {
      ['+'] = {'tmux', 'load-buffer', '-w', '-'},
    },
    paste = {
      ['+'] = {'tmux', 'save-buffer', '-'},
    },
    cache_enabled = true,
  }
end

4. Implement tpwd Command

Copy the current working directory to the local clipboard:

1
2
3
4
5
tpwd() {
    local path="$(pwd)"
    tmux load-buffer -w <(echo "$path")
    echo "$path"
}

Usage:

1
2
3
cpwd
# Output:
# /home/user/project copied to clipboard.

5. Implement tcopy Command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Copy any command output to tmux + local clipboard
# Usage:
#   tcopy [-s] <command>   # -s for silent mode
tcopy() {
    local silent=0

    # Check for -s option
    if [ "$1" = "-s" ]; then
        silent=1
        shift
    fi

    if [ -z "$1" ]; then
        echo "Usage: tcopy [-s] <command>"
        return 1
    fi

    if [ "$silent" -eq 1 ]; then
        # Silent: pipe to tmux, suppress output
        "$@" | tmux load-buffer -w -
        echo "Output of '$*' copied to tmux buffer and clipboard."
    else
        # Non-silent: use tee to also output to terminal
        "$@" | tee >(tmux load-buffer -w -)
    fi
}

✅ Usage

Silent mode (only copies, no output shown):

1
2
tcopy -s ls -l
# Output: Output of 'ls -l' copied to tmux buffer and clipboard.

Non-silent mode (copies and prints output):

1
2
tcopy ls -l
# Output: printed to terminal and also copied to tmux buffer + clipboard

📝 How It Works

  • tee >(tmux load-buffer -w -) → sends stdout both to tmux and terminal.

  • "$@" → runs the command with all arguments.

  • -s flag 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 + .

  • cpwd mimics zsh’s copypath functionality.


6. References