I SSH to a remote server running Ubuntu 24.04 that has Emacs 29.3 installed. This Emacs installation uses CUA mode for copying, defined in its ~/.config/emacs/init.el as shown:
;; Set copy/cut/paste to C-c/x/v: (cua-mode t) ;; additional options: (setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands (transient-mark-mode 1) ;; No region when it is not highlighted (setq cua-keep-region-after-copy t) ;; Standard Windows behaviour Using Emacs, I open a file on that server in terminal mode.
$ emacs filename.txt This file has many lines, some of which are comments beginning with #, which Emacs recognizes as comments and highlights accordingly.
# This is a comment Line of content number 1 Line of content number 2 I highlight these lines in the terminal and press Ctrl+c to copy them. I then move focus to a file on my laptop and press Ctrl+v to paste them into it. In the resulting file, I find that Emacs "fills in" all whitespace at the end of comment lines with literal whitespaces instead of nothing.
This is the copied text, with _ representing a literal space character:
# This is a comment________________________________________________________ Line of content number 1 Line of content number 2 This is a huge pain for keeping everything formatted when copying, and I'd like it to stop.
It seems to happen when copying anything Emacs recognizes as a comment in its current mode. It happens for lines containing hashes # in Apache config files, but it also happened for lines beginning with ;; when I copied the init.el file above. I left that whitespace as-is, if it helps to inspect it.
It doesn't matter what program I paste the content into. Copying and pasting into a local Emacs buffer, Libre Office Writer, and Obsidian all show the same behavior with unwanted trailing whitespace in comment lines.
If I open the remote file using Nano and copy the lines into a local file, there is no trailing whitespace. It seems to be an Emacs thing, not a quirk of my terminal emulator. If it matters, though, the SSH is done from my laptop running Ubuntu 22.04 through GNOME Terminal.
How do I get Emacs to stop copying trailing whitespace in comment lines?