Trailing newline handling in rectangular regions varies with rectangle-mark-mode
0 reputation · 03 Jan 2020, 23:47 UTC
0 reputation · 03 Jan 2020, 23:47 UTC
Rectangular editing in Emacs provides blockwise cut and paste independent of line boundaries as documented in the manual under Rectangular editing. When a rectangular region spans multiple lines, the handling of the trailing newline at the bottom edge is reported to depend on the rectangle-mark-mode setting. This variation affects how yanked text integrates with the kill ring and subsequent editing operations. The precise conditions under which the newline is preserved omitted or inserted remain ambiguous across different Emacs configurations and region shapes.
Under what rectangle-mark-mode values is the trailing newline consistently preserved during rectangular yank?
Does the kill-ring insertion order change when rectangular yank follows a non-rectangular cut, and if so, why?
Is the documented behavior intended to support multiple line counts, or is it an unresolved design ambiguity?
35275 reputation · 04 Jan 2020, 09:06 UTC
In Emacs, rectangle-mark-mode is a boolean toggle, not a multi-valued setting. There is no specific "value" that preserves trailing newlines because of how rectangular data is stored: a rectangular kill is not stored as a single string with newlines, but as a list of strings (one for each line of the rectangle).
The perceived variation in trailing newlines usually stems from using the wrong yank command rather than the mode setting itself. Because the kill ring entry is a list, the behavior changes based on the command used to retrieve it:
yank-rectangle (C-x r y): This is the intended command for rectangular data. It inserts the strings at the current column across multiple lines. It does not add or remove trailing newlines because it operates on the buffer's existing line structure.yank (C-y): If you use the standard yank command on a rectangular kill, Emacs attempts to treat the list of strings as a single entity, joining them with newlines. This often results in a trailing newline that was not part of the original rectangular selection, leading to the appearance of "inserted" or "preserved" newlines.The kill-ring insertion order remains a standard stack (LIFO). Whether a cut was rectangular or linear does not change the order of entries; it only changes the type of data stored in that entry (a string vs. a list of strings). This design is intentional to allow rectangles to span any number of lines regardless of the surrounding buffer content.
To verify this behavior in your current Emacs version, run these tests in a scratch buffer:
rectangle-mark-mode (C-x SPC), select a block, and kill it (C-x r k).(car kill-ring) via M-:. You will see a list of strings, confirming that no trailing newline character is stored within the data structure.C-x r y (rectangular yank) and C-y (standard yank) to observe how the latter introduces newlines during the joining process.Diagnostic Detail: Are you using any third-party packages that modify the kill ring or provide "enhanced" yanking (e.g., which-function or custom kill-ring filters)? Such packages can transform the list-of-strings into a single string, altering the newline behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.