3

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.)

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
    Mar 15 '18 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
    Mar 15 '18 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 Mar 15 '18 at 18:11
  • Ah, I didn't realise that I was inadvertently concatenating two commands. Thanks!
    – LSpice
    Mar 15 '18 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
    Mar 15 '18 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.

2
  • Thanks! It's not the spirit of what I wanted, but it's an interesting approach.
    – LSpice
    Feb 4 at 18:25
  • I also use it to count number of occurrences in the buffer: %s/foo//n
    – Ran Regev
    Feb 5 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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