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?
Asked
Active
Viewed 66 times
2
muk.li
- 449
- 3
- 8
1 Answers
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
-
2
-1in a range can be shorted to-as the1can be assumed. e.g.:5,$-s/^/X– Peter Rincker May 10 '16 at 14:12
$-1could be used to express the second to last line. Does:5,$-1s/^/Xwork for you? – user9433424 May 10 '16 at 12:35