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?
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
.
visual-line-mode
. You can switch that on by the menu itemOptions -> Line Wrapping In This Buffer -> Word Wrap (Visual Line Mode)
. I suggest to combine it with adaptive-wrap. – Tobias Jul 22 '19 at 12:34