Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim search and replace, newline is rendering as "^@" [duplicate]

I copy pasted cells of a long google spreadsheet into a txt file that is a list of email addresses separated by commas. There are many "blank" cells as well, i.e. a blank space surrounded by commas. So I could have the following list:

[email protected], ,[email protected], , , , [email protected]

In vim, when I try to add separate each address by a new line with this command:

:%s/, /,\n/g

instead of adding a new line after the comman, I get "^@" instead.

I know this has something to do with character sets, but I don't know how to fix it.

like image 624
mehulkar Avatar asked Jul 29 '12 07:07

mehulkar


People also ask

How do I insert a new line in Vim search?

Substituting by \n inserts a null character into the text. To get a newline, use \r . When searching for a newline, you'd still use \n , however.

How do you replace N with a new line?

So: Press CTRL-h and the Replace dialog will open. Type \r\n in "Find what" and \\r\\n in "Replace with". Then select search mode Extended (\r, \n, \t, \x..., \0) and click "Replace All".

What is new line in Vim?

Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing. To add 10 empty lines below the cursor in normal mode, type 10o<Esc> or to add them above the cursor type 10O<Esc> .


1 Answers

In :s's replacement field, you need to use \r not \n for newline characters.

^@ is the ASCII null character. Vim internally uses \r for newlines (which is ^M), and \n for ASCII null, so in the replacement, if you use \n you're getting those null characters instead of newlines. See also :h sub-replace-special

like image 69
Julian Avatar answered Oct 20 '22 01:10

Julian