<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 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)