This page is about ‘M-x hide-lines’ written by MarkHulmeJones. It provides a function to quickly hide lines based on a regexp.

There is no copyright information with this, but contributions to the Wiki are multi-licensed with the copying terms at the bottom of each Web page.

Remove the word “cucumber” to decode MarkHulmeJones’s email address found at the top of the Lisp file.

For similar functionality, check out HideRegion and also HighLight. For isearch-like functionality that hides non-matching lines incrementally, check out HideSearch (it utilizes HideLines).

Does anyone knows why it doesn’t work in shell-script-mode?

The buffer-invisibility-spec gets reset when changing major modes. I had the same problem with other major modes, not just shell-script-mode. hide-lines.el adds the ‘hl symbol to the buffer-invisibility-spec via (add-to-invisibility-spec ‘hl) when the package is loaded, but buffer-invisibility-spec automatically becomes buffer-local when set, and changing modes usually clears all local variables. The simplest fix is to change (overlay-put overlay ‘invisible ‘hl) to (overlay-put overlay ‘invisible ‘t) in hide-lines-add-overlay or add the following hook:

  (add-hook 'after-change-major-mode-hook (lambda () (add-to-invisibility-spec 'hl)))


Why not make it reversely hidden, i.e. when the Regxp is found ,the correspoended line will be preserved,while other will be hidden, I find this feature in Multieditor

the prefix arg (press C-u first, before the key for the hide-lines function) lets you do this. - MarkHulmeJones

I suggest the addition of the following two lines in the definition of “hide-matching-lines”, allowing the user to show all hidden areas giving an empty regexp for the normal call of the function. In this way there is no need to define yet another keystroke for “show-all-invisible” (which will presumably be used aprox. as many times as “hide-matching-lines” itself):

  (interactive "MHide lines matching regexp: ")
  ;; --- addition ---
  (if (equal search-text "")
    ;; else, the rest of the function definition follows

You can use advice if you’d like to use an empty regexp to ‘show-all’:

  (defadvice hide-lines-matching (around hide-line-clear activate)
    (if (equal search-text "")

Another suggestion: it would be good to have some indicator in the mode line about having lines currently hidden.

HideLines works perfectly well in conjunction with Linum (Emacs function to display line numbers in the left margin), now a standard feature of the Emacs distribution. Hidden lines may be easily detected by the non-sequential line numbers in the left margin.

Also, one nice feature (for me) is that if you edit a line to match the regexp which suppressed that line, the line being edited is not automatically or instantly hidden. Lines are only hidden at the time the hide-lines command is invoked; the buffer is not automatically re-evaluated—and lines are not automatically hidden—on every cursor-movement, buffer save, or screen refresh. This is exactly the behavior I needed.

CategoryEditing CategoryHideStuff CategoryRegexp