Lost newlines and repeated junk when copying text from terminal
In this example, I open a fresh a-Terminal session, display a file on screen with cat, copy its text, and paste it in this GitHub issue. Note that extra newlines got added before and after the selected text, and the newlines after the shebang line and function got lost.
Selected text:
Copied text:
[~/Documents]$ cat repro5.sh
#! /bin/sh
foo() {
printf "baa "
printf "caa "
printf "daa "
}
foo | tr 'a' 'z'
printf "\n"
In some other cases, particularly when I’ve been repeatedly opening and closing and editing files, or when I’m struggling a bit to get text selection to cooperate, I end up with bigger mismatches between what is shown selected on screen and what actually gets copied, such as lines or entire paragraphs/functions repeated. This issue is rather tricky to reproduce and actual behaviour seems to vary. Nonetheless, there do seem to be bugs in the text selection and/or text copying logic for the terminal.
The main issues I see are:
- Blank lines in selected text are usually lost in what is copied.
- Sometimes, copied text has blank lines prepended and appended.
- Sometimes lines or groups of lines get repeated/duplicated in the copied text.
While I'm looking for ways to fix this one, you can use the pbcopy command: cat repro5.sh | pbcopy or pbcopy < repro5.sh for a reliable copy (there is also pbpaste).