4

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?

2 Answers 2

1

This is simply a quirk of your terminal emulator.

When you paste something, the application that receives the paste sends a message to the application that holds the selection to ask for the content of the selection. That application is your terminal emulator. Emacs is not involved at all. Your terminal emulator probably has heuristics to try to determine which characters in the screen buffer should be copied and which shouldn’t. You should read the documentation to find out what those heuristics are and to see if there are any settings that can change how it behaves.

1

Turn off font-lock mode. As noted in the other answer, the copy is being handled by your terminal emulator, not emacs. The emacs terminal display code likes to pad out lines with trailing spaces when there's a face that doesn't end (multi-line strings, multi-line comments) or ends at and the end of the line (your # comment). There are still other problems if you want a faithful copy made through the terminal emulator, though; the emacs display may also replace spaces with tabs or trailing white space might not be included. The map is not the terrain, as they say.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.