In expression :s/foo/bar\n/g When replacing \n inserts a null byte, according to the docs I have to use \r. Since I insert newlines more frequently than null bytes, is there a way to change this mapping?

So I want :s/foo/bar\n/g to insert a newline, not nullbyte.

1 Answer 1


You could do this via a :cnoremap <expr> \r that checks (via getcmdtype() and getcmdpos()) that the current command-line is a :s Ex command, and you're editing the replacement part.

I would recommend against this, and just accept and learn that \r has to be used. Otherwise, you'll become dependent on your mapping and will struggle on systems that are not yours / do not have your configuration.

  • 2
    aren't we all ? I mean, I cannot efficiently use vim without my own vimrc. Commented May 18, 2015 at 15:21
  • That's why you keep your vimrc on github or bitbucket and pull it down to the new machine. Commented May 20, 2015 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.