(defun make-lines-invisible (regexp &optional arg) "Make all lines matching a regexp invisible and intangible. With a prefix arg, make it visible again. It is not necessary that REGEXP matches the whole line; if a hit is found, the affected line gets automatically selected. This command affects the whole buffer." (interactive "MRegexp: \nP") (let (ov ovs count) (cond ((equal arg '(4)) (setq ovs (overlays-in (point-min) (point-max))) (mapc (lambda (o) (if (overlay-get o 'make-lines-invisible) (delete-overlay o))) ovs)) (t (save-excursion (goto-char (point-min)) (setq count 0) (while (re-search-forward regexp nil t) (setq count (1+ count)) (if (= (% count 100) 0) (message "%d" count)) (setq ov (make-overlay (line-beginning-position) (1+ (line-end-position)))) ; (overlay-put ov 'make-lines-invisible t) ; (overlay-put ov 'invisible t) ; (overlay-put ov 'intangible t) (goto-char (line-end-position)))))))) (global-set-key "\C-cz" 'make-lines-invisible)