2

I have 69 files in which I want to prepend lines 5 to penultimate with a short string. How could I address the penultimate line? :5,61s/^/X/g would do it if I manually look up that the last line is 62, but if I want to script it for all the files, how can I do that? I could imagine doing something like vim "+:5,$((`cat test.txt|wc -l` - 1))s/^/X/g|:x" test.txt from the command line, but as I'm not dealing with a simple search and replace as in my above example I think I won't be able to provide it from the command line, how can I do it from within vim?

muk.li
  • 449
  • 3
  • 8

1 Answers1

3

You can express the penultimate line with $-1. So, in your case, to prepend X in front of all the lines from the fifth down to the second to last one, you could use the following substitution command:

:5,$-1s/^/X

You can find more information on how to write a range in :help :range.

user9433424
  • 6,138
  • 2
  • 21
  • 30