This is similar to There's a way to use emacs like MS Word, to make text permanently colored, stylized, etc...? However, I do only wish to preserve linebreaks so that the pasted raw text from word looks somewhat readable and not just one long line. Are there any utilities in Emacs to achieve this?
-
Maybe, you are actually looking for `visual-line-mode`. You can switch that on by the menu item `Options -> Line Wrapping In This Buffer -> Word Wrap (Visual Line Mode)`. I suggest to combine it with [adaptive-wrap](https://elpa.gnu.org/packages/adaptive-wrap.html). – Tobias Jul 22 '19 at 12:34
-
@Tobias Thank you for a quick response. I just tried it. It does not quite do what I need it to do. It looks like I want however when I copy-paste it into notepad I lose the linebreaks. So it is similar to the question I linked but I am not quite looking for the same thing I think. Using white-space mode I can see that no line breaks have been inserted – JKRT Jul 22 '19 at 12:39
-
Are the linebreaks preserved when you copy directly from Word to Notepad? Note, that I very much assume that there are no linebreaks in the text copied from Word but Word also does [linewrapping](https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap). – Tobias Jul 22 '19 at 12:45
-
Sorry for not being familiar with the lingo Tobias. I basically want to keep the linebreaks as Emacs displays them in adaptive-wrap. When pasting to notepad – JKRT Jul 22 '19 at 13:06
-
I figured out how to do it, alt q! – JKRT Jul 22 '19 at 13:32
-
`M-q` runs `fill-paragraph`. That is actually not the same as preserving what `adaptive-wrap` shows. – Tobias Jul 22 '19 at 13:48
-
You can write your own answer with `fill-paragraph` and accept it. I have added my version basing on Visual line mode and `adaptive-wrap`. – Tobias Jul 22 '19 at 14:17
-
@Tobias Your answer was more than satisfactory, I will add your code to my .emacs on Github and link to your profile :) – JKRT Jul 23 '19 at 02:03
2 Answers
The linebreaks in a paragraph of a word document are usually not hard.
Therefore, in the text that you copy and paste from Word there are no linebreaks to be kept.
You can use Visual line mode (Menu item Options -> Line Wrapping in This Buffer -> Word Wrap (Visual Line Mode)
) to get a similar filling of paragraphs in Emacs.
I suggest to use it in combination with adaptive-wrap
. That mode can be installed from elpa with install-package
.
Visual line mode adapts the line filling to the window-width. Thereby you can adjust the width of the text with the mouse by resizing Emacs or the window border.
If you want to keep the line ends found by Visual line mode for copying and pasting you can use the following command make-visual-lines-hard
.
(require 'adaptive-wrap)
(defun make-visual-lines-hard (&optional beg end)
"Insert ?\\n at each visual line end which is not a hard line end."
(interactive)
(when (and (called-interactively-p 'any)
(region-active-p))
(setq beg (region-beginning)
end (set-marker (make-marker) (region-end))))
(save-excursion
(goto-char (or beg (point-min)))
(unless end (setq end (set-marker (make-marker) (point-max))))
(while (progn
(end-of-visual-line)
(< (point) end))
(if (looking-at "\n")
(forward-line)
(insert "\n" (adaptive-wrap-fill-context-prefix (line-beginning-position) (line-end-position)))
(looking-at "[[:space:]]*")
(replace-match "")
))))
Install adaptive-wrap
, copy the above Elisp code to your init file and restart Emacs.
Afterwards you can use make-visual-lines-hard
.

- 32,569
- 1
- 34
- 75
Save as plain text from word. Dialog box appears and you select “Line breaks” as one of the options!

- 1