VisualLineMode

Visual line mode is a new mode in Emacs 23 that is on by default.

Actually, it is not “on” by default at all, you have to activate it. It is available though.

According to the NEWS file,

    Visual Line mode provides support for editing by visual lines.
    It turns on word-wrapping in the current buffer, and rebinds C-a, C-e,
    and C-k to commands that operate by visual lines instead of logical
    lines.  This is a more reliable replacement for longlines-mode.
    This can also be turned on using the menu bar, via
    Options -> Line Wrapping in this Buffer -> Word Wrap

To use the original behavior put the following in your .emacs:

    (setq line-move-visual nil)

This mode will likely deprecate LongLines in a future version of Emacs.

But can you have visual-line-mode wrap at a given column and not just at the window border? I like how longlines-mode keeps text at 80 columns or less. --BryanMurdock

The following code convinces visual-line-mode to wrap at a given column by expanding the right margin of the buffer’s window. It’s worked pretty well for me, although it depends on being the only one that fiddles with the margins. --JamesWright

    (defvar visual-wrap-column nil)
    (defun set-visual-wrap-column (new-wrap-column &optional buffer)
      "Force visual line wrap at NEW-WRAP-COLUMN in BUFFER (defaults
    to current buffer) by setting the right-hand margin on every
    window that displays BUFFER.  A value of NIL or 0 for
    NEW-WRAP-COLUMN disables this behavior."
      (interactive (list (read-number "New visual wrap column, 0 to disable: " (or visual-wrap-column fill-column 0))))
      (if (and (numberp new-wrap-column)
               (zerop new-wrap-column))
        (setq new-wrap-column nil))
      (with-current-buffer (or buffer (current-buffer))
        (visual-line-mode t)
        (set (make-local-variable 'visual-wrap-column) new-wrap-column)
        (add-hook 'window-configuration-change-hook 'update-visual-wrap-column nil t)
        (let ((windows (get-buffer-window-list)))
          (while windows
            (when (window-live-p (car windows))
              (with-selected-window (car windows)
                (update-visual-wrap-column)))
            (setq windows (cdr windows))))))
    (defun update-visual-wrap-column ()
      (if (not visual-wrap-column)
        (set-window-margins nil nil)
        (let* ((current-margins (window-margins))
               (right-margin (or (cdr current-margins) 0))
               (current-width (window-width))
               (current-available (+ current-width right-margin)))
          (if (<= current-available visual-wrap-column)
            (set-window-margins nil (car current-margins))
            (set-window-margins nil (car current-margins)
                                (- current-available visual-wrap-column))))))

See also LineWrap.

I turned it on globally with `(global-visual-line-mode 1)’ and it’s really useful. But I also use iswitchb which shows the buffer list in multiple lines in the minibuffer if there are lots of open buffers. That’s how I like it. The problem is visual line mode wraps lines in the minibuffer too which is not good, because it breaks the nice iswitchb display.

Is there a way to turn it on globally, but disable it in the minibuffer? I tried this, but it didn’t seem to do the trick:

    (add-hook 'minibuffer-setup-hook (lambda ()
                                       (visual-line-mode -1)))

Anonymous

You may want to ask help-gnu-emacs. An conflict between global visual line mode and Iswitchb may be a bug.

Concerning your workaround(?), note that ‘iswitchb-minibuffer-setup-hook’ may be more appropriate, and you may also want to just try setting ‘line-move-visual’ to nil, then setting it back with ‘minibuffer-exit-hook’. So something along the lines of.

    (add-hook 'iswitchb-minibuffer-setup-hook
	      (lambda ()
		(setq line-move-visual nil)))
    
    (add-hook 'minibuffer-exit-hook (lambda ()
				      (setq line-move-visual t)))

This is just an untested guess on what to also try, so no guarantees that it will work. – AaronHawley

Tried your suggestion. Unfortunately, it doesn’t help. The problem is the same.

A solution, or rather workaround, is to enable ‘visual-line-mode’ for text-modes only:

    (add-hook 'text-mode-hook 'turn-on-visual-line-mode)

I find this superior to enabling ‘visual-line-mode’ globally, because I prefer the old behavior when editing code or configuration files.

The only thing that’s bothering me about VisualLineMode is that I can’t visually discern whether there is an actual newline at the end of a displayed line or not. I have to either disable VisualLineMode temporarily or glance at the line number while navigating through the file. This is especially cumbersome if some paragraphs are filled and some not.

I think a neat solution would be if VisualLineMode also used the fringe to mark wrapped lines. Is there a way to achieve that?

Anonymous

You can use ‘visual-line-fringe-indicators’ variable for that, for example:

    (setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))

CategoryEditing CategoryFilling CategoryModes