4

Suppose that I have marks 'a and 'b, and I want to search for all occurrences of foo between them.

The ex command

:'a,'bs/foo/bar

will replace only between 'a and 'b. Is there any command like

:'a,'b/foo

that will find foo only between those two marks? (That exact syntax does something, but I can't figure out what. It doesn't seem to be what I want.)

2 Answers 2

5

You can use \%>'m and \%<'m to match after and before a given mark, m.

/\%>'afoo\%<'b

For more helps see:

:h /\%'m
6
  • Thank you! And then some more characters.
    – LSpice
    Commented Mar 15, 2018 at 18:06
  • Do you know what my imaginary syntax :'a,'b/foo is doing? The effect that I observe is that I jump to some other part of the document, way outside the mark-delimited region, but I can't figure out why I'm going there.
    – LSpice
    Commented Mar 15, 2018 at 18:08
  • 1
    'a,'b is a range. So you can actually type this out as it to see what it does: :'a,'b. It will move to the 'b position as that is the end of the range. You then execute a command, e.g. /. So it searches for foo after line 'b. For more help see :h :range Commented Mar 15, 2018 at 18:11
  • Ah, I didn't realise that I was inadvertently concatenating two commands. Thanks!
    – LSpice
    Commented Mar 15, 2018 at 18:40
  • @LSpice, see if you can learn how this one works: :'a,'b/hello/d "delete from the line of mark a through the first line containing the word hello after the line of mark b"
    – Mass
    Commented Mar 15, 2018 at 19:17
1

FWIW: :'a,'bs/foo//n substitute foo with nothing and choose 'no' when asked for the replacement. afterwards just hit the next (n) to get to the next occurrence.

5
  • Thanks! It's not the spirit of what I wanted, but it's an interesting approach.
    – LSpice
    Commented Feb 4, 2021 at 18:25
  • I also use it to count number of occurrences in the buffer: %s/foo//n
    – Ran Regev
    Commented Feb 5, 2021 at 20:39
  • Re, how do you extract the count (or do you just tally it manually)?
    – LSpice
    Commented Jun 21, 2022 at 13:53
  • 1
    e.g. When you have 24 foo in your buffer, the command %s/foo//n outputs the answer: 24 matches on 24 lines.
    – Ran Regev
    Commented Jun 22, 2022 at 18:24
  • I was overthinking that! Thanks!
    – LSpice
    Commented Jun 22, 2022 at 20:32

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.