Defindit Docs and Howto Home

This page last modified: Oct 02 2009
title:Emacs hints include lines, paste, unfill
description:Emacs cursor logical move, paste at cursor, unfill paragraph
keywords:line wrap, line, move, visual, wrapping, continuation, visual, logical, cursor,cursor jump, cursor skip, skip line, skip continuation, wrapped lines,continuation lines, line continuation, line continuation mode, cursormovement mode, cursor mode, next line, next logical line, skip tological line, cursor move, line visual move, line-move-visual, movelogical, logical lines


Table of contents
-----------------
Logical lines with line-move-visual
Paste at cursor with mouse-yank-at-point
Unfill paragraph


Logical lines with line-move-visual
-----------------------------------

A line that wraps because it is longer than the window is wide is
called a "contination line". Emacs (and most other linux packages) use
the \ as the signal that a line has been continued. Emacs does this
dynamically with long lines: the \ is not actually in the file.

The problem is what the cursor does when you move up and down a
line. Logically, the cursor should skip to the next logical line. This
is especially true when running keyboard macros. If your macro has C-n
(next-line) and your macro processes a long line, you don't want the
macro to break by running in the middle of the line instead of at the
beginning. The visual mode also changes C-a and C-e which compounds
the problem. Logical lines are a tiny bit confusing visually, but they
are logical and sensible.

Sadly, the default in Emacs was changed in 2008 or 2009 to be
line-move-visual 't ('t is "true" in Lisp).

Return to the sensible logical move by adding the following line to
your .emacs file:

(setq line-move-visual 'nil)

In customization group Editing Basics, Line Move Visual controls
whether or not the cursor moves to logical lines or visual lines. The
difference is for continuation lines, the visual line is the next line
on the screen. The logical line is the next actual line in the file,
and not necessarily what is "visual" on the screen. This should not be
confused with visual line mode.

Keywords: line wrap, wrapping, continuation, visual, logical, cursor,
cursor jump, cursor skip, skip line, skip continuation, wrapped lines,
continuation lines, line continuation, line continuation mode, cursor
movement mode, cursor mode, next line, next logical line, skip to
logical line, cursor move, line visual move, line-move-visual, move
logical, logical lines

http://www.gnu.org/software/emacs/manual/html_node/emacs/Continuation-Lines.html


Paste at cursor with mouse-yank-at-point
----------------------------------------

You may have noticed that when you paste into an X-windows (graphical)
Emacs session, the insertion point becomes the location of the mouse
cursor. This is a minor irritatant since you almost always want to
insert at the current text cursor location. (And if you wanted to move
the text cursor, you could left-click at that new location. Moving the
text cursor upon middle-click is inconsistent and odd to my view of
the UI.)

There is an easy fix. Add this line to your .emacs or .xemacs/init.el file:

(setq mouse-yank-at-point t)


Unfill paragraph
----------------

Every now and then you may want to unfill a paragraph, turning several
line wrapped lines into a single line. This would be the opposite of
fill-paragraph. The multi-step solution is to query-replace C-j with
nothing (use C-qC-j to enter a C-j).

The naive single keystroke solutions simply set the fill-column to some
very large number. It is unlikely that I'll have a paragraph with
10000000 characters, but that naive method is inelegant.

If found the following bit of code is slightly better because it uses
point-max (a function) as the fill-column value. Using the function
unfill-paragraph does not alter your fill-column value.

;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph
;; Takes a multi-line paragraph and makes it into a single line of text.

(defun unfill-paragraph ()
  (interactive)
  (let ((fill-column (point-max)))
  (fill-paragraph nil)))

You could put this in your .emacs file and bind it to a key (or not I
suppose) or you could paste it into your *scratch* buffer and hit C-j
at the end of the last line. 

The *scratch* buffer is a "Lisp interaction" buffer which means that
Lisp statements in this buffer are executed if you type C-j at the end
of the statement. The "result" of the statement will print after you
type C-j (control-j).