20

I need replace all ; to \n , but :%s/;/\n/gc not works

guilin 桂林
  • 17,050
  • 29
  • 92
  • 146

2 Answers2

53

See http://vim.wikia.com/wiki/Search_and_replace

When searching: \n is newline, \r is CR (carriage return = Ctrl-M = ^M)
When replacing: \r is newline, \n is a null byte (0x00).

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 13
    doh! the first thing about vim that i hate. i love vim ... but wow ... this one is evil. – underrun Jul 23 '13 at 14:05
  • Is there any way to change this behaviour? I want \n to be LF and \r to be CR - *always*. – Bell Dec 23 '16 at 03:29
  • Was trying to match multi-line stuff, including newlines, and replace them with matching groups containing newlines. That wasn't working, as I was getting the whole \n vs \r problem. The only way I've found to get around this is to replace \n with some improbable sequence, do whatever regex stuff I was trying to do (using the improbable sequence in place of where I would've used \n), then replace the improbable sequence with \r. – Meower68 Dec 31 '19 at 23:53
10

You need to use \r as the replacement instead: :%s/;/\r/gc

too much php
  • 88,666
  • 34
  • 128
  • 138