Automatic line breaks in emacs

So I have the following problem. I use emacs (specifically orgmode, but my problem applies to emacs in general) quite a lot for writing longer texts (In the long term I even plan on writing scientific papers with emacs). However, one of the problems I have is line formatting. As you probably all know, emacs does not really have default "automatic line breaks" as you know them from rich text editors like LibreOffice. Instead emacs then just moves the entire window you see to the right, if a single line gets too long. Since this is awful for editing I started to add line breaks manually, however the problem is, that you then have to manually re-do the entire formatting if you decide to change the structure of one ore more sentences/lines, which is kind of annoying. The next problem is, that it may look good on your screen with the manual linebreaks, but the formatting then often looks bad when viewed on smaller screens.

One solution I saw is, that if a line gets to long, that emacs then just puts the rest of the line into a new line on the screen, without inserting a line break. How can I enable this behaviour in my emacs config?

I also found out about Auto Fill mode, however I kind of dont like, that it inserts a new linebreak relatively fast (my screen is wide, so I want to use it) and the problem with exporting still stands.

Anyone got any good workarounds that might be helpful?

6 points · 3 comments · view on lemmy.world

3 Comments

tal@lemmy.today · 5 pts · 127d (1 reply)

You probably want visual-line-mode. You can flip it on with M-x and then visual-line-mode to try it out.

C-h f for visual-line-mode:

:::spoiler visual-line-mode docs

visual-line-mode is an interactive native-comp-function in
‘simple.el’.

(visual-line-mode &optional ARG)

Inferred type: (function (&optional t) t)

Toggle visual line based editing (Visual Line mode) in the current buffer.

When Visual Line mode is enabled, ‘word-wrap’ is turned on in
this buffer, and simple editing commands are redefined to act on
visual lines, not logical lines.  See Info node ‘Visual Line
Mode’ for details.
Turning on this mode disables line truncation set up by
variables ‘truncate-lines’ and ‘truncate-partial-width-windows’.

This is a minor mode.  If called interactively, toggle the ‘Visual-Line
mode’ mode.  If the prefix argument is positive, enable the mode, and if
it is zero or negative, disable the mode.

If called from Lisp, toggle the mode if ARG is ‘toggle’.  Enable the
mode if ARG is nil, omitted, or is a positive number.  Disable the mode
if ARG is a negative number.

To check whether the minor mode is enabled in the current buffer,
evaluate the variable ‘visual-line-mode’.

The mode’s hook is called both when the mode is enabled and when it is
disabled.

:::

:::spoiler The info node in question

16.23 Visual Line Mode
======================

Another alternative to ordinary line continuation (*note Continuation
Lines::) is to use “word wrap”.  Here, each long logical line is divided
into two or more screen lines, or "visual lines", like in ordinary line
continuation.  However, Emacs attempts to wrap the line at word
boundaries near the right window edge.  (If the line's direction is
right-to-left, it is wrapped at the left window edge instead.)  This
makes the text easier to read, as wrapping does not occur in the middle
of words.

   Word wrap is enabled by Visual Line mode, an optional minor mode.  To
turn on Visual Line mode in the current buffer, type ‘M-x
visual-line-mode’; repeating this command turns it off.  You can also
turn on Visual Line mode using the menu bar: in the Options menu, select
the ‘Line Wrapping in this Buffer’ submenu, followed by the ‘Word Wrap
(Visual Line mode)’ menu item.  While Visual Line mode is enabled, the
mode line shows the string ‘wrap’ in the mode display.  The command ‘M-x
global-visual-line-mode’ toggles Visual Line mode in all buffers.

   Since word wrap and line truncation (described in the previous
section) are contradictory, turning on ‘visual-line-mode’ disables line
truncation.

   In Visual Line mode, some editing commands work on screen lines
instead of logical lines: ‘C-a’ (‘beginning-of-visual-line’) moves to
the beginning of the screen line, ‘C-e’ (‘end-of-visual-line’) moves to
the end of the screen line, and ‘C-k’ (‘kill-visual-line’) kills text to
the end of the screen line.

   To move by logical lines, use the commands ‘M-x next-logical-line’
and ‘M-x previous-logical-line’.  These move point to the next logical
line and the previous logical line respectively, regardless of whether
Visual Line mode is enabled.  If you use these commands frequently, it
may be convenient to assign key bindings to them.  *Note Init
Rebinding::.

   By default, word-wrapped lines do not display fringe indicators.
Visual Line mode is often used to edit files that contain many long
logical lines, so having a fringe indicator for each wrapped line would
be visually distracting.  You can change this by customizing the
variable ‘visual-line-fringe-indicators’.

   By default, Emacs only breaks lines after whitespace characters like
<SPC> and <TAB>, but does not break after whitespace characters like <EN
QUAD>.  Emacs provides a minor mode called ‘word-wrap-whitespace-mode’
that switches on word wrapping in the current mode, and sets up which
characters to wrap lines on based on the
‘word-wrap-whitespace-characters’ user option.  There's also a
globalized version of that mode called
‘global-word-wrap-whitespace-mode’.

   Only breaking after whitespace character produces incorrect results
when CJK and Latin text are mixed together (because CJK characters don't
use whitespace to separate words).  You can customize the option
‘word-wrap-by-category’ to allow Emacs to break lines after any
character with ‘|’ category (*note (elisp)Categories::), which provides
better support for CJK characters.  Also, if this variable is set using
Customize, Emacs automatically loads ‘kinsoku.el’.  When ‘kinsoku.el’ is
loaded, Emacs respects kinsoku rules when breaking lines.  That means
characters with the ‘>’ category don't appear at the beginning of a line
(e.g., U+FF0C FULLWIDTH COMMA), and characters with the ‘<’ category
don't appear at the end of a line (e.g., U+300A LEFT DOUBLE ANGLE
BRACKET).  You can view the category set of a character using the
commands ‘char-category-set’ and ‘category-set-mnemonics’, or by typing
‘C-u C-x =’ with point on the character and looking at the "category"
section in the report.  You can add categories to a character using the
command ‘modify-category-entry’.

:::

I have something like this in my init.el:

(defun my-text-mode-hook ()
  (flyspell-mode t)
  (visual-line-mode t))

(add-hook 'text-mode-hook 'my-text-mode-hook)

So that when I'm in text-mode, visual-line-mode also comes on automatically by default.

It doesn't do so in things like programming modes, where one doesn't want that sort of thing.

(I also turn on checking for misspelled words in text-mode, and highlighting of them, which is what flyspell-mode is for.)

cows_are_underrated@feddit.org · 5 pts · 127d

Thanks, thats exactly what I was searching for.

donio@lemmy.world · 1 pts · 125d

To tune auto-fill-mode (and manual fill invocation with M-q etc) you can set the fill-column variable. You can set it globally, per-mode (using mode hooks) or even per-buffer (manually or using file-local variables).

When thinking about line breaks in Emacs it's important to consider if you want visual line breaks that only affect display or actual line breaks that also appear in the file as newline characters.