0

In a file containing number ranges (4-5, 12-20), I want to replace all "number-number+1" ranges by "number f." (45-46 > 45 f.).

Any idea how to achieve that? Sorry if this has been covered before, I found nothing until now.

2 Answers2

3

As this condition (M + 1 = N) is hard to express in a regular expression, this is a case for a :help sub-replace-expression: Match all number ranges, separating them into start and end numbers via \(...\) capture groups. Then in the \= replacement expression, check for the condition, accessing the numbers via submatch(), and either return the original result (submatch(0)), or the condensed form.

:%substitute/\(\d\+\)-\(\d\+\)/\=submatch(1) + 1 == submatch(2) ? submatch(1) . ' f.' : submatch(0)/g
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

If the file is already created use the GREP command to parse the file and SED command to replace. Or use find. Both ways I'm sure are on here.

ZOMGnerd
  • 21
  • 6
  • Here's one example http://stackoverflow.com/questions/15402770/how-to-grep-and-replace – ZOMGnerd Jul 03 '14 at 11:51
  • This answer is about replacement in a single file (no GREP), and within Vim (not SED). I think the OP knows the general mechanics of `:s` already, the challenge is the special number condition. Have you actually understood that?! – Ingo Karkat Jul 03 '14 at 11:58