Araujo1914 Araujo1914 - 1 year ago 83
Markdown Question

How can I add two spaces after each line on a given text in EMACS?

Seeking how could add two spaces after each line on a given text buffer in EMACS.

This is a way for add LINE BREAKS on EMACS export via org-mode.

Answer Source

First off, make sure you (or some package you've installed) haven't added delete-trailing-whitespace on a hook. It's not on any hooks by default, but it's fairly common to add it to before-save-hook.

Quick and dirty: C-M-< C-M-% $ RET SPACE SPACE RET !. This will go to the beginning of the buffer (C-M-<), start query-replace-regexp (C-M-%), replacing end of line ($) with two spaces, and applying it to the every match (!). Above, RET means press the return key and SPACE means press the space bar. Replace $ with [^ ][^ ]$ and SPACE SPACE with \& SPACE SPACE to only work on lines that end in less than two spaces (this will also make the operation idempotent). \& means the entire matched string (in this case, the last two characters).


(defun ensure-trailing-spaces ()  
  "Ensure there are two spaces at the end of every line."
  (interactive "*")
    (goto-char (point-min))
    (while (not (eobp))
      (unless (looking-back "  $")
        (insert "  "))

Note that this unconditionally inserts two spaces at the end of every line, even if there is already one space, thus there will either be two or three spaces at the end of every line.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download