I have a very long line of text (8000 characters). I would like to format this nicely so it splits across multiple lines in a block. Where the line breaks occurs does not matter for this purpose.





I chose eight characters before a line-break, but that number does not matter either- it could be 8 or 80. fill-paragraph does not seem to work for me- nothing happens.

N.B. I'm not asking about Line Wrapping, or how the text is displayed.

  • @Drew I must disagree. This has nothing todo with keyboard macros. (replace-regexp "\\(.\\{8\\}\\)" "\\1\n") will also do. (Naturally I would use the equivalent interactive command.)
    – Tobias
    May 11, 2018 at 14:37
  • fill-paragraph does not work here because it breaks lines at whitespace.
    – Tobias
    May 11, 2018 at 15:05
  • @Tobias: The question does not ask about keyboard macros, true. But someone looking at questions tagged keyboard-macros might well be helped by this question. Feel free to remove the tag, if you feel that tag text-editing is enough or you have a more helpful tag in mind.
    – Drew
    May 11, 2018 at 16:59

1 Answer 1


You can use a keyboard macro. Generally using a keyboard macro means to record some steps for later reuse. (Find documentation about keyboard macros within Emacs by entering C-h F kmacro-start-macro-or-insert-counter <RET>.)

Recent versions of Emacs provide the function key <F3> to start a recording of a keyboard macro and <F4> to finalize the recording and also replay the last recording (if not currently recording.)

To solve you question using a keyboard macro perform the following steps.

  1. Move the cursor to the beginning of the line with the 8000 characters.

  2. Press <F3> (to start macro recording.)

  3. Perform the action. In this case e.g. move forward eight characters (press the left arrow key eight times) and press the enter key.

  4. Press <F4> (to finalize the macro recording. The macro then is recorded.)

  5. Type M-999 <F4>. Concretely this (typically) means: hold down the Alt key, press the 9-key three times, release the Alt key, press <F4>. Explanation: This plays the macro 999 times. Recall that <F4> replays the previously recorded macro. One can further specify a number how often the macro will be replayed (see the Emacs documentation for "Arguments"). The M-999 stands for 999 repetitions.

  6. Be patient while the macro performs.

  7. See the result of the transformation!

  • 2
    Please be more verbose. What does F3 and F4 do? Maybe somebody has re-mapped the keys. For an example consider users of ergomacs where F3 is suggested for re-mapping by the user. What does C-u 8 do? What does C-f do?
    – Tobias
    May 11, 2018 at 15:01
  • 1
    What @Tobias said - that will help (e.g. show also the command name). But what is meant here are the default bindings of F3 and F4. It's a judgment call whether someone should always spell out the default command bound to a key (e.g. C-x C-f).
    – Drew
    May 11, 2018 at 17:02
  • @Marco Wahl, This did work. I must admit I didn't think of a macro, because I was hoping for a 'single command' to do what I wanted. Thanks for the help.
    – ender.qa
    May 15, 2018 at 15:33

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.