18

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

My buffer:

dam madam
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?

2
  • 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
    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
    Dec 21, 2015 at 15:48

2 Answers 2

26

...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
2
  • Great! That's what I was looking for! 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. May 17, 2022 at 19:31
1

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.

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

Your Answer

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

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