For vim (neovim), I spend most of my time on remote machines. It's easy to copy text from remote ssh to local. If the copied content is small, you can directly select it with the mouse and copy it. The disadvantages are also shown:
- The format of the copied text has often changed. For example, the line number information has been added, and the new line needs to be adjusted.
- More than one page cannot be copied continuously
In the past, I had to use sftp to download and copy large files. But this operation is too cumbersome. Today, we finally found a better solution.
precondition
A local terminal supporting OSC 52 is required, and many terminals support it.
- xterm under Linux, setting disallowedWindowOps: 20,21,SetXprop
- Item2 under Mac, check Applications in terminal may access clipboard
I use iterm2 under Mac OS
to configure
Put the clipboard provider into the path
Create a file clipboard provider and write the following.
#!/bin/bash # Taken and modified from https://github.com/agriffis/skel/blob/master/neovim/bin/clipboard-provider # # clipboard provider for neovim # # :help provider-clipboard #exec 2>> ~/clipboard-provider.out #set -x : ${COPY_PROVIDERS:=tmux pb xclip osc52 local} : ${PASTE_PROVIDERS:=xclip pb tmux local} : ${TTY:=`(tty || tty </proc/$PPID/fd/0) 2>/dev/null | grep /dev/`} LOCAL_STORAGE=$HOME/.clipboard-provider.out main() { declare buffer status=1 case $1 in copy) buffer=$(base64 | tr -d '\n') internal() { base64 --decode <<<"$buffer"; } for p in $COPY_PROVIDERS; do internal | $p-provider copy && status=0 done ;; paste) for p in $PASTE_PROVIDERS; do $p-provider paste && status=0 && break done ;; esac exit $status } is-copy() { if [[ "$1" == "copy" ]]; then return 0; else return 1; fi } tmux-provider() { [[ -n $TMUX ]] || return is-copy case $1 in copy) internal | tmux load-buffer - ;; paste) tmux save-buffer - ;; esac } pb-provider() { if ! command -v pbcopy &>/dev/null;then return $(is-copy $1); fi case $1 in copy) internal | pbcopy ;; paste) pbpaste ;; esac } osc52-provider() { # HACK: this ignores stdin and looks directly at the base64 buffer case $1 in copy) [[ -n "$TTY" ]] && printf $'\e]52;c;%s\a' "$buffer" > "$TTY" ;; paste) return 1 ;; esac } local-provider() { case $1 in copy) internal > $LOCAL_STORAGE ;; paste) cat $LOCAL_STORAGE && return 0;; esac } xclip-provider() { if ! command -v xclip &>/dev/null;then return $(is-copy $1); fi case $1 in copy) internal | xclip -i -selection clipboard;; paste) xclip -o -selection clipboard;; esac } main "$@"
# Set executable permissions chmod +x clipboard-provider # Copy to the path and put it anywhere, just in the path cp clipboard-provider /usr/local/bin/
Configure vimrc for neovim
I use neovim. I don't test whether vim can.
Edit profile:
nvim ~/.config/nvim/init.vim
Add the following:
"ssh Remote pasting board if executable('clipboard-provider') let g:clipboard = { \ 'name': 'myClipboard', \ 'copy': { \ '+': 'clipboard-provider copy', \ '*': 'env COPY_PROVIDERS=tmux clipboard-provider copy', \ }, \ 'paste': { \ '+': 'clipboard-provider paste', \ '*': 'env COPY_PROVIDERS=tmux clipboard-provider paste', \ }, \ } endif
After configuration, restart nvim. v select the text content to be copied and press "+ y" to copy the content to the local clipboard, which is very convenient.
tmux
After the above configuration is completed, it can be used directly. However, it is found that opening nvim in tmux cannot copy the content to the local clipboard.
terms of settlement:
Edit ~ / tmux.conf add the following line:
set -g set-clipboard on
Reload tmux configuration
It can be executed in the command line mode in tmux mode (enter the command ctrl + b in command line mode, and then press a colon:)
source-file ~/.tmux.conf
That's it. If you find that your tmux still doesn't work, there may be no clipboard provider in your tmux path
, just create a new window.