Let's say that I have the following very simple file
a
b
c
d
e
and I've decided that I want to add empty lines after each line. Several different methods immediately jump to my mind. We might just do it (and thus embrace failure). We might record a macro like qqo<ESC>jq
and repeat it several times.
Two other methods seemed more obvious to me at the time.
Firstly, I thought I would issue the :norm
command o
on each line. So I run :%norm o
. But what actually happens is that we get 5 blank lines, followed by the un-separated lines as above. I interpret this to mean that by %norm
, vim actually picks up the message issue the following normal command on the first five lines of this five line file. The o
command creates a new line and vim is "dumb" in the sense that it references by line number and not actually by some other identifier.
Well, I was embarrassed. Sure. I tried a few other things to see if I could make the above method work, but alas, I couldn't. Out of curiosity, I tried my other favorite mass-apply method. This led me to try :g/^/norm o
. To my surprise, this works just fine! So to my eyes, it seems that vim isn't "dumb" here in the same way as above and references lines by more than just line number.
What exactly is going on?
:%s/$/\r/
, or like this::%s/\n/\r\r/
. The takeaway is that newlines can be matched with\n
, but have to be written as\r
in replacement values.