Skip to content

Instantly share code, notes, and snippets.

@ramn
Created January 26, 2011 10:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramn/796527 to your computer and use it in GitHub Desktop.
Save ramn/796527 to your computer and use it in GitHub Desktop.
Emacs function that removes duplicate rows in a region
;; uniq-lines
;; ----------
;; This is something of a companion to the built-in sort-lines function.
;; Like the uniq utility, this removes duplicate lines from the region. It
;; is best used after sorting a region.
;;
(defun uniq-lines (start end)
"Removes duplicate lines from the selected region."
(interactive "*r")
(goto-char start)
(beginning-of-line)
(let ((last ""))
(while (< (point) end)
(let* ((bol (point))
(eol (progn (end-of-line) (point)))
(text (buffer-substring bol eol)))
(forward-char)
(if (string= last text)
(delete-region bol (point))
(setq last text))))))
(define-key global-map [(control ?z) ?u] 'uniq-lines) ; Bind to C-z u
@kuanyui
Copy link

kuanyui commented Jun 19, 2014

In Emacs 24.4, just use build-in command delete-duplicate-lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment