Text copied from a Mosh session pastes as one long line with spaces
Lines copied from a Mosh session can paste joined by spaces instead of newlines. Why it happens, which Mosh versions did it, and the fix.
You select several lines in a Mosh session, copy, and paste somewhere else. Instead of separate lines you get one long line, with runs of spaces where the line breaks were. Over plain SSH the same copy works.
Why it happens
When you copy, your terminal decides where to put line breaks. It has to tell two kinds of line apart:
- a line that ends: copy puts a newline after it;
- a line that wrapped because the text was wider than the screen: copy joins it to the next line, because it was one line of text.
Each row carries a "wrapped" mark that says which. Over SSH the terminal sees the program's own output and sets the mark itself. Over Mosh, the terminal only sees what Mosh draws. Mosh rebuilds the screen and has to reproduce the wrap marks too: when a row wrapped on the server, Mosh lets the cursor wrap onto the next row in your terminal as well.
The Mosh bug
Mosh 1.2.6 had a bug here. When a row that had wrapped was later overwritten by text that did not wrap, Mosh kept the old wrap mark. Your terminal then treated a whole block of separate lines as one wrapped line, and copy joined them with the padding spaces in between. The bug was reported against 1.2.6 (with 1.2.5 not affected) and fixed in Mosh 1.3.0, whose release notes list "text copied from mosh-client to paste as long lines joined by spaces".
The fix is in how the screen is drawn, so update both ends: mosh-server on the host and the Mosh client.
When it is not Mosh
Some full-screen programs draw each line by writing spaces to the right edge and moving the cursor, instead of ending lines. Copying from them can give lines padded with trailing spaces, over SSH as well. In the Mosh tracker, mutt inside screen gave trailing spaces both with and without Mosh. That padding is the program's way of drawing, not the 1.2.6 wrap bug.
Ways round it
- Update Mosh on both ends to 1.3.0 or later.
- Copy on the host instead: in tmux, use copy mode and its buffer. See Scroll back and search history in tmux.
Related
- Mosh scrollback is incomplete, and how to use the mouse
- How Mosh works
- Scroll back and search history in tmux
- tmux over Mosh draws the status bar or last line wrong