How to remove this symbol "^@" with vim?
Solution 1:
You could try:
%s/<CTRL-2>//g
(on regular PCs)%s/<CTRL-SHIFT-2>//g
(on Mac PCs)
where <CTRL-2>
means first press down the CTRL on regular PCs, keeping it as pressed down, hit 2, release CTRL.
and <CTRL-SHIFT-2>
means first press down the control on Mac PCs, keeping it as pressed down, press down shift on Mac PCs, keeping it as pressed down, hit 2, release control and shift.
Finally, both of the two commands should result in %s/^@//g
on screen. ^@
means a single character (a NULL byte, which otherwise couldn’t be displayed), not ^
followed by @
, so you can't just type ^
and @
in a row in the above command.
This command removes all the ^@
.
Solution 2:
I don't think your files are corrupted. Your example line looks like it contains regular text with null bytes between each character. This suggests it's a text file that's been encoded in UTF-16 but the byte-order mark is missing from the start of the file. See http://en.wikipedia.org/wiki/Byte-order_mark
Suppose I open Notepad, type the word 'filename', and save as Unicode Big-endian. A hex dump of this file looks like this:
fe ff 00 66 00 69 00 6c 00 65 00 6e 00 61 00 6d 00 65
If I open this file in Vim it looks fine - the 'fe ff' bytes tell Vim how the file is encoded. Now suppose I create a file containing the exact same sequence of bytes, but without the leading 'fe ff'. Vim inserts ^@ (or <00>, depending on your config), in place of the null bytes; Notepad inserts spaces.
So rather than remove the nulls, you should really be looking to get Vim to interpret the file correctly. You can get Vim to reload the file with the correct encoding with the command:
:e ++enc=utf16
Solution 3:
This actually worked for me within vim:
:%s/\%x00//g
Solution 4:
That 'symbol' represents a NULL character, with ASCII value 000.
It's difficult to remove with vim, try
tr -d '\000' < file1 > file2