How to Put Newline Characters in Substitutions of Regular Expressions of GVim
Solution 1
^@ is a nul
For some reason, vim wants to see \r
in the replacement text in order to insert a newline. (\n
in the replacement inserts a nul.)
So, after succesfully locating newlines by running /\n
, try:
s//\r/
Or to add an extra newline:
s/$/\r/
On the pattern side, \r
means an actual (015) CR character, leading to the following strange-looking command when you want to replace returns with newlines:
s/\r/\r/
No, it's not a no-op.
Solution 2
This
s/\r/\r/
fails. But
s/\n/\r/
works fine.
Related videos on Youtube
Mert Nuhoglu
Software developer and data scientist. Working in btgrubu.com / layermark.com
Updated on July 09, 2022Comments
-
Mert Nuhoglu almost 2 years
I use GVim on Windows 7.
I want to learn how to put newline characters by using regex substitutions. To do this I try to use \r and \n metacharacters but the substituted text doesn't show normal newlines.
For example, at the beginning I have:
line 1 line 2
I use the following substitution expression:
:%s/\n/\n\n/g
Then GVim produces the following text:
line 1^@^@line 2^@^@
Instead, if I use \r\n in the substitution expression
:%s/\n/\r\n/g
Then GVim produces the following text:
line 1 ^@line 2 ^@
What are those ^@ characters?
How to use newline characters in the substitution expression properly?
-
rpyzh over 10 yearsIn my Vim 7.3 on Windows 7, the suggested
s/\r/\r/
gives error 'E486: Pattern not found: \r'. Was it tested to work on a prior version somehow? -
rpyzh over 10 yearsFor more info on metacharacter usage difference in search vs. replace, see this SO answer.