‹ Guides

Copy from vim to your phone's clipboard

In vim on a server, y copies inside vim and never reaches your phone. Here is why, and the small plugin that sends every yank to the phone's clipboard — inside tmux and over mosh too.

Written

Why y does not reach your phone

In vim, y copies into vim's own register. That register lives in vim, on the server, and nothing ever sends it anywhere else. You can paste it back with p in the same vim, but your phone never hears about it.

The system clipboard registers, "+ and "*, do not help either. On a server they mean the server's own desktop clipboard, and a server has no desktop. Most server builds of vim say so outright: vim --version shows -clipboard, and "+y does nothing. Even a vim that has a clipboard would copy to the server's clipboard, not the phone's. SSH carries your keys and the screen between the two computers, not their clipboards.

vim showing deploy-notes.txt in Term on a phone after V, j, j and y: vim reports 3 lines yanked, and the HarmonyOS keyboard shows no From clipboard row.
Plain vim: 3 lines yanked — into vim. The keyboard's clipboard row knows nothing about it.

One thing does get from the server to your phone: an escape sequence called OSC 52. A program prints it, and the terminal puts the text on the clipboard of the device it runs on. Term supports it: the text goes straight to the phone's clipboard. vim does not send OSC 52 by itself, so it needs a plugin to do it.

Install vim-oscyank

vim-oscyank is a small plugin that sends a copy as OSC 52. On the server, install it as a vim package, with no plugin manager needed:

on the server
mkdir -p ~/.vim/pack/plugins/start
git clone https://github.com/ojroques/vim-oscyank ~/.vim/pack/plugins/start/vim-oscyank

With vim-plug instead, add Plug 'ojroques/vim-oscyank', {'branch': 'main'} to your ~/.vimrc and run :PlugInstall.

Copy every yank to the phone

The plugin only acts when asked. To make every ordinary yank go to the phone too, add this to ~/.vimrc:

~/.vimrc
" Copy every yank to the phone's clipboard (vim-oscyank, OSC 52)
autocmd TextYankPost *
    \ if v:event.operator is 'y' && v:event.regname is '' |
    \   execute 'OSCYankRegister "' |
    \ endif

Now yy, yw, V then y — any yank into the default register — also lands on your phone, and vim says so: [oscyank] 61 characters copied. The text shows up in the keyboard's From clipboard row straight away, and it pastes anywhere on the phone. Back in Term, paste it with a long-press next to the cursor or Ctrl+Shift+V — see Copy and paste in the terminal.

vim in Term on a phone after V, j, j and y with vim-oscyank: vim reports [oscyank] 61 characters copied, and the HarmonyOS keyboard offers server: prod-web-01 deploy: 2026-10-09 under From clipboard.
With the plugin: the same yank now reaches the phone, and the keyboard offers it under From clipboard.
Two views: Term's Paste sheet showing the three yanked lines with a Paste button; then the shell prompt with the same three lines pasted, highlighted and not yet run.
Back at the prompt: a long-press, Paste, and the three lines from vim — checked in the Paste sheet, then waiting at the prompt.

Deletes (d, x) and yanks into a named register ("ay) stay in vim, as before. To copy a single piece of text on purpose instead of every yank, the plugin's README gives a mapping: select it, then <leader>c.

Inside tmux: one line

tmux catches OSC 52 from the programs inside it. By default (set-clipboard external) it keeps the copy to itself, so vim says copied and nothing arrives on the phone. Tell tmux to pass it on:

~/.tmux.conf
set -s set-clipboard on

Then reload with tmux source-file ~/.tmux.conf.

Over mosh, tmux needs one more line. mosh passes a copy on only in the form 52;c;, and tmux writes it as 52;;, so a copy made inside tmux is dropped on the way:

~/.tmux.conf
set -ag terminal-overrides ',*:Ms=\E]52;%p1%sc;%p2%s\a'

vim-oscyank on its own, without tmux, already writes the 52;c; form, so plain vim over mosh needs nothing extra.

Neovim

The same plugin works in Neovim. Clone it into ~/.local/share/nvim/site/pack/plugins/start/ and put the same lines in ~/.config/nvim/init.vim.

Neovim 0.10 and later also has OSC 52 built in (let g:clipboard = 'osc52'). It copies to the phone, but it pastes with OSC 52 as well, by asking the terminal for the clipboard. Term does not answer that request (see below), so "+p stops with Waiting for OSC 52 response from the terminal and gives up. The plugin only copies, so it never waits. Paste with Term's own paste instead.

Copy only — programs cannot read your clipboard

This works in one direction. A program on the server can put text on your phone's clipboard, but Term does not let it read the clipboard back. That is a security decision: your clipboard often holds passwords, codes and private messages, and a read request can come from any program on any server you connect to — a script, a dependency, someone else's machine. So it stays closed for now. To paste from the phone into vim, use Term's paste in insert mode: you choose what goes in, and it lands at the cursor.

Long copies over mosh

mosh carries at most 16 KB of clipboard. A larger yank arrives cut short, with no error. Over SSH the limit is several megabytes.