Created
January 26, 2011 10:29
-
-
Save ramn/796527 to your computer and use it in GitHub Desktop.
Emacs function that removes duplicate rows in a region
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Emacs 24.4, just use build-in command
delete-duplicate-lines