<span class="mw-page-title-namespace">Blog</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Hacks/Redact sensitive parts of a text with Emacs</span>
Fabrice P. Laussπ•ͺ’s β„€ygentoman Web

Say you want to send some ASCII text, part of which has been redacted, e.g., "He once found a β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ stuck in a bathroom on the second floor, which was, of course, β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ to the case of β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ."

I wrote an Emacs command to do this, which I bound to C-c r, so you take the original:

He once found a horse stuck in a bathroom on the second floor, which was, of course, fundamentally connected to the case of the missing cat.

select the offending (or sensitive) material, and C-c r over it. Here is the command:

(defun redact-region ()
  "Replace the selected region with full block characters (β–ˆ) of the same length."
  (interactive)
  (if (use-region-p)
      (let* ((start (region-beginning))
             (end (region-end))
             (length (- end start)))
        (delete-region start end)
        (insert (make-string length ?β–ˆ)))
    (message "No region selected!")))

(defun redact-word-or-region ()
  "Redact current word or selected region with β–ˆ blocks."
  (interactive)
  (if (use-region-p)
      (redact-region)
    (let ((bounds (bounds-of-thing-at-point 'word)))
      (if bounds
          (let ((start (car bounds))
                (end (cdr bounds)))
            (delete-region start end)
            (goto-char start)
            (insert (make-string (- end start) ?β–ˆ)))
        (message "No word at point")))))

(global-set-key (kbd "C-c r") 'redact-word-or-region)