I created a keyboard macro to join lines in a buffer using:

F3 C-n M-x join-line RET F4.

It works fine, except when lines get too long and start to wrap – which makes this macro dependent on the width of the frame. next-line doesn’t seem to go to the next actual line, but to the “wrapping” part of the current line.

How to get around that issue?


2 Answers 2


You have two three options:

  1. Use next-logical-line instead of next-line when defining the macro:

    Move cursor vertically down ARG lines. This is identical to next-line, except that it always moves by logical lines instead of visual lines, ignoring the value of the variable line-move-visual.

  2. Set line-move-visual to nil:

    (setq line-move-visual nil)

    This makes next-line behave like next-logical-line by default.

  3. Turn on line truncation by doing

    M-x toggle-truncate-lines RET

    before recording the macro.

  • Perfect! Thanks. I wonder in what situation you'd prefer next-line's current behaviour to next-logical-line's, though... Commented Oct 22, 2014 at 9:37
  • @SébastienLeCallonnec I guess there is a use case for everything... and it's good to know that Emacs handles them all ;)
    – itsjeyd
    Commented Oct 22, 2014 at 9:43
  • 1
    @SébastienLeCallonnec Erm... when writing in natural language? In a non-line-oriented situation, pressing <down> or C-n is usually expected to make you go one line down, not jump past the line boundary. Think that not every user comes from notepad (where there is no line wrapping, at all)... Imo, the exception is rather situations where next-logical-line is the expected behavior, and in these situations, I often tend to disable visual-line-mode altogether. And +1 @itsjeyd, it's really nice to be able to get either behavior depending on the situation.
    – T. Verron
    Commented Oct 22, 2014 at 10:21
  • 2
    @T.Verron Fair enough. I guess I can always rebind C-n to next-logical-line. ;) Commented Oct 22, 2014 at 10:42

C-a and C-e both take a prefix argument that lets you skip lines. For example, C-2 C-a will jump to the beginning of the next line and C-2 C-e will jump to the end of the next line. A prefix argument of 3 moves two lines, and so on. You can also use negative arguments to move to previous lines.

Clearly these aren't much use when you need the cursor to stay in the same column, but I find most of my macros involve going to the start or end of a line anyway, so they're very convenient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.