Emacs: Remove Wikipedia Square Bracket Citations 📜

By Xah Lee. Date: . Last updated: .

Remove Wikipedia Citation Mark

In Wikipedia article, there are many citation marks like this: [1], [2], etc. If you quote Wikipedia in your blog, those citation marks don't make sense and are distracting.

Here's a command to remove them.

(defun xah-remove-square-brackets (&optional Begin End)
  "Delete any text of the form [1], [2] etc in current text block or selection.

For example
 as Blu-ray Disc [11][12],
becomes
 as Blu-ray Disc,

When called non-interactively, Begin End are region positions.

URL `http://xahlee.info/emacs/emacs/emacs_remove_square_brackets.html'
Created: 2017-06-10
Version: 2025-03-25"
  (interactive)
  (let (xbeg xend xchangedItems)
    (seq-setq (xbeg xend) (if (and Begin End) (list Begin End) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point)))))))
    (save-restriction
      (narrow-to-region xbeg xend)
      (progn
        (goto-char (point-min))
        (while (re-search-forward "\\(\\[[0-9]+?\\]\\)" nil t)
          (setq xchangedItems (cons (match-string-no-properties 1) xchangedItems))
          (replace-match "" t)))
      (progn
        (goto-char (point-min))
        (while (search-forward "[citation needed]" nil t)
          (setq xchangedItems (cons "[citation needed]" xchangedItems))
          (backward-char 17)
          (delete-char 17)))
      (goto-char (point-max)))
    (if (> (length xchangedItems) 0)
        (mapcar
         (lambda (xx)
           (princ xx)
           (terpri))
         (reverse xchangedItems))
      (message "No change needed."))))