I would like to see all the lines from the current buffer, that contain some phrase, or match regex. Example:

My buffer:

dam madam

this is da
m a
dam 1
dam 2
dam pi
dam 321:)
is dam 2?

Looking for dam. Result: dam madam dam 1 dam 2 dam pi dam 321:) is dam 2?

The same buffer. Looking for regexp: ^dam\s\d. Result: dam 1 dam 2 dam 321:)

How can I do this?

  • helm-swoop github.com/ShingoFukuyama/helm-swoop provides this as an interactive search, supporting regexps. It gives you a temporary menu of lines that match, from which you can quickly jump to one for editing
    – Tyler
    Commented Dec 21, 2015 at 15:43
  • 5
    Alternatively, the built-in function occur creates a new buffer that lists all the lines that match your regexp. Hitting e allows you to edit those lines directly from the occur buffer.
    – Tyler
    Commented Dec 21, 2015 at 15:48

2 Answers 2


...see all the lines from the current buffer...

With built-in commands and no external packages or dependencies in

a new buffer, use:

M-x occur

the same buffer, use:

M-x keep-lines
  • Great! That's what I was looking for! Commented Dec 23, 2015 at 0:09
  • Note that keep-lines operates on a region rather than the whole buffer. You can use C-x h (mark-whole-buffer) before this to select the whole buffer as your region. Commented May 17, 2022 at 19:31

Icicles multi-command icicle-occur (bound to C-' in Icicle mode) does this.

As you type into the minibuffer, the lines matching the pattern you type are shown as completion candidates. Your pattern can be as simple or as complex (e.g. a regexp) as you like. You can change the pattern dynamically, and the set of matches changes accordingly.

You can combine search patterns, that is, you can use any number of them to narrow the set of search hits. It is typically much easier to use two or more simple search patterns than it is to try to come up with a complex regexp that does exactly what you want. (But each of the individual search patterns can be as complex as you like.)

You can cycle among matches, or you can visit any of them, in any order. You can even replace matches selectively, on demand.

See the Overview of Icicles Search.

  • Care to explain the downvote (6 years later)?
    – Drew
    Commented Dec 8, 2020 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.