I agree with comments that suggest using flush-lines
. I would likely use that in this case, especially since your use case is line-oriented.
But if you want to make text that matches a regexp invisible, so that search, query-replace, etc. ignore it but it is still present, then you can do that with libraries Zones
and Isearch+.
For example, you can use C-x n R
(command zz-set-zones-matching-regexp
) to set the value of the current izones variable to the list of zones that match a given regexp. Then use command isearchp-make-zones-invisible
to make those zones invisible.
If you want to make other lines, which match other regexps, then use C-x n r
, not C-x n R
. The former adds zones to the zone set; the latter sets the zone set (removing any existing zones in it).
There are also these related commands: isearchp-make-zones-visible
, isearchp-toggle-zones-invisible
, and the same for anti-zones (complement of zones).
C-x n R
and C-x n r
match a regexp against your text, like re-search-forward
does - the search hits define the zones. They are not line-oriented like flush-lines
, occur
, and grep
. So if you want an entire line to become invisible then you need to use a regexp that matches the entire line (containing whatever text you're interested in).