Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing new line characters with literal '\n' in macvim

Tags:

vim

macvim

I want to replace all new line characters with the two literal characters '\' and 'n' in macvim under a selected area.

For example

i have selected the following lines (in .js file) :

# /messages/{id}

## GET
+ Response 200 (text/plain)

        Hello World!

## DELETE
+ Response 204

Then i am applying

:'<,'>s#\n#/\n#g

that replaces into :

'# /messages/{id}/^@/^@## GET/^@+ Response 200 (text/plain)/^@/^@       Hello World!/^@/^@## DELETE/^@+ Response 204'/^@

My desired output is :

# /messages/{id}/\n/\n## GET/\n+ Response 200 (text/plain)/\n/\n       Hello World!/\n/\n## DELETE/\n+ Response 204'/\n

Why there is ^@ instead of \n?

And how can i get \n?

like image 794
codeofnode Avatar asked Oct 27 '25 10:10

codeofnode


1 Answers

Use \n in the search part of your substitution and \r in the replace part:

:%s#\n#/\r#g

EDIT

Oh! Do you want to replace all EOLs with literal \n?

:%s/\n/\\n
like image 121
romainl Avatar answered Oct 28 '25 23:10

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!