From bbe55a3f647f07a6b7c9852a499c45d03ac068af Mon Sep 17 00:00:00 2001 From: xs Date: Thu, 5 Nov 2026 10:45:29 +0800 Subject: core: shared clipboard over ssh to root@p.noml.ch tmux.conf and vimrc push yanks to /dev/shm/clip/buf on CLIP_HOST (root@p.noml.ch) and pull before every paste (tmux prefix+], every vim p variant, 3 s limit, local paste when no buffer exists yet). One persistent ssh link per machine (ControlPersist=yes, ControlPath ~/.ssh/cm-%C), host key not checked. Replaces the openssl+netcat sender. vimrc: set nocompatible so /etc/vimrc parses \ continuations and . --- core/tmux.conf | 29 +++++++++++++++++------------ core/vimrc | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/core/tmux.conf b/core/tmux.conf index 97084f1..2a0e161 100644 --- a/core/tmux.conf +++ b/core/tmux.conf @@ -23,19 +23,24 @@ bind -r J resize-pane -D 1 bind -r K resize-pane -U 1 bind -r L resize-pane -R 1 -# v to start selection, y to yank (like vim) +# ===== Shared clipboard: RAM file /dev/shm/clip/buf on CLIP_HOST ===== +%hidden CLIP_HOST="root@p.noml.ch" +# One persistent ssh link (opens on first use, one key touch). +# Close it: ssh -O exit -o ControlPath=~/.ssh/cm-%C +%hidden CLIP="ssh -o ControlMaster=auto -o ControlPersist=yes -o ControlPath=~/.ssh/cm-%C -o ServerAliveInterval=30 -o ConnectTimeout=3 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR $CLIP_HOST" +# push (yank): copy mode v to select, then y / Enter / mouse drag +%hidden YANK="$CLIP 'umask 077; mkdir -p /dev/shm/clip && cat >/dev/shm/clip/new && mv /dev/shm/clip/new /dev/shm/clip/buf'" bind-key -T copy-mode-vi v send-keys -X begin-selection -# macOS: pbcopy. Linux: wl-copy/xclip when usable, else just drain (tmux's own -# buffer + set-clipboard/OSC 52 still apply). Netcat send is the same on both. -if-shell 'uname | grep -q Darwin' { - bind-key -T copy-mode-vi y send-keys -X copy-pipe-no-clear "bash -c 'tee >(pbcopy) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" - bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-no-clear "bash -c 'tee >(pbcopy) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" - bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-no-clear "bash -c 'tee >(pbcopy) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" -} { - # Linux only: true-color regardless of what TERM the outer terminal/distro sets +bind-key -T copy-mode-vi y send-keys -X copy-pipe-no-clear "$YANK" +bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-no-clear "$YANK" +bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-no-clear "$YANK" +# pull (paste): prefix + ] fetches the buffer into tmux, then pastes it; +# no buffer on CLIP_HOST yet (exit 3, e.g. after its reboot) = paste tmux's own buffer +bind-key ] run-shell "f=\$(mktemp); $CLIP '[ -f /dev/shm/clip/buf ] || exit 3; cat /dev/shm/clip/buf' >\$f; r=\$?; [ \$r -eq 0 ] && tmux load-buffer \$f; rm -f \$f; [ \$r -eq 0 ] || [ \$r -eq 3 ] && tmux paste-buffer" +# ===== end shared clipboard ===== + +# Linux only: true-color regardless of what TERM the outer terminal/distro sets +if-shell 'uname | grep -q Darwin' {} { set -g default-terminal "tmux-256color" set -ga terminal-overrides ",*256col*:Tc" - bind-key -T copy-mode-vi y send-keys -X copy-pipe-no-clear "bash -c 'tee >(wl-copy 2>/dev/null || xclip -selection clipboard 2>/dev/null || cat >/dev/null) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" - bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-no-clear "bash -c 'tee >(wl-copy 2>/dev/null || xclip -selection clipboard 2>/dev/null || cat >/dev/null) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" - bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-no-clear "bash -c 'tee >(wl-copy 2>/dev/null || xclip -selection clipboard 2>/dev/null || cat >/dev/null) | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1'" } diff --git a/core/vimrc b/core/vimrc index 06d1782..017e754 100644 --- a/core/vimrc +++ b/core/vimrc @@ -1,5 +1,6 @@ " One vimrc for Mac and Linux. Source of truth: /Users/xs/dev_env/core/vimrc " Mac: cp core/vimrc ~/.vimrc Linux: sudo bash core/vim.sh (copies it to /etc/vimrc or /etc/vim/vimrc) +set nocompatible " /etc/vimrc is read before vim leaves vi mode; without this \ continuations and break " gt: go to next tab (right, wraps) " gT: go to previous tab (left, wraps) " 2gt: go to tab number 2 (counted from 1 at the left) @@ -53,10 +54,42 @@ nnoremap f :LspDocumentFormat| " \f: format file nnoremap em :call ExpandMacro()| " \em: expand macro under cursor -augroup netcat_clipboard +" ===== Shared clipboard: RAM file /dev/shm/clip/buf on g:clip_host (same as tmux.conf) ===== +let g:clip_host = 'root@p.noml.ch' +" One persistent ssh link (opens on first use, one key touch). +let g:clip = ['ssh', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=yes', '-o', 'ControlPath=~/.ssh/cm-%C', '-o', 'ServerAliveInterval=30', '-o', 'ConnectTimeout=3', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'LogLevel=ERROR', g:clip_host] +" push (yank): every yank or delete sends @" in a background job +augroup ssh_clipboard au! -au TextYankPost * call system("echo " . shellescape(@") . " | openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023 >/dev/null 2>&1 &") +au TextYankPost * let g:clip_job = job_start(g:clip + ['umask 077; mkdir -p /dev/shm/clip && cat >/dev/shm/clip/new && mv /dev/shm/clip/new /dev/shm/clip/buf'], {'in_io': 'pipe', 'out_io': 'null', 'err_io': 'null'}) | call ch_sendraw(g:clip_job, @") | call ch_close_in(g:clip_job) augroup END +" pull (paste): every p P gp gP ]p [p (normal) and p P (visual) first fetches the buffer into @". +" Waits up to 3 s for a pending push, then up to 3 s for the fetch (perl alarm kills ssh); +" no buffer on g:clip_host yet (exit 3, e.g. after its reboot) = normal local paste; +" any other failure prints a message and skips the paste. Counts and "x register prefixes still work. +function! ClipPull() abort + let l:i = 0 + while exists('g:clip_job') && job_status(g:clip_job) ==# 'run' && l:i < 30 + sleep 100m + let l:i += 1 + endwhile + let l:out = system("perl -e 'alarm 3; exec @ARGV' " . join(g:clip) . " '[ -f /dev/shm/clip/buf ] || exit 3; cat /dev/shm/clip/buf'") + if v:shell_error == 3 + return 1 + elseif v:shell_error + echohl ErrorMsg | echo 'clip: pull from ' . g:clip_host . ' failed or took >3s, paste skipped' | echohl None + return 0 + endif + call setreg('"', l:out) + return 1 +endfunction +for s:k in ['p', 'P', 'gp', 'gP', ']p', '[p'] + execute 'nnoremap ' . s:k . " ClipPull() ? '" . s:k . "' : ''" +endfor +for s:k in ['p', 'P'] + execute 'xnoremap ' . s:k . " ClipPull() ? '" . s:k . "' : ''" +endfor +" ===== end shared clipboard ===== filetype plugin indent on -- cgit