Home » Questions » Computers [ Ask a new question ]

How to remove this symbol "^@" with vim?

How to remove this symbol "^@" with vim?

I have some files that are corrupted with this symbol:

Asked by: Guest | Views: 320
Total answers/comments: 5
bert [Entry]

"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 ^@."
bert [Entry]

"This actually worked for me within vim:

:%s/\%x00//g"
bert [Entry]

"FWIW, in my case I had to use vim on cygwin to edit a text file created on a mac. The accepted solution didn't work for me, but was close. According to Vim wiki page about working with Unicode, there is a difference between Big Endian and Little Endian versions of the BOM byte. So, I had to explicitly tell vim to use a Little Endian version of BOM encoding.

Only after picking the right encoding I converted the file format (line endings) to dos so I could edit the file in Windows editor. Trying to set reset the file format before specifying the encoding gave me grief. Here is the full list of commands I used:

:e ++enc=utf16le
:w!
:e ++ff=mac
:setlocal ff=dos
:wq"
"FWIW, in my case I had to use vim on cygwin to edit a text file created on a mac. The accepted solution didn't work for me, but was close. According to Vim wiki page about working with Unicode, there is a difference between Big Endian and Little Endian versions of the BOM byte. So, I had to explicitly tell vim to use a Little Endian version of BOM encoding.

Only after picking the right encoding I converted the file format (line endings) to dos so I could edit the file in Windows editor. Trying to set reset the file format before specifying the encoding gave me grief. Here is the full list of commands I used:

:e ++enc=utf16le
:w!
:e ++ff=mac
:setlocal ff=dos
:wq"
bert [Entry]

"The accepted solution did not work for me. I made vim pipe the file through tr instead:

:%!tr -d '\000'

This would also work well with visual mode (just type :!tr -d '\000') or on a range of lines:

# Remove nulls from current line:
:.!tr -d '\000'

# Remove nulls from lines 3-5:
:3,5!tr -d '\000'"
bert [Entry]

"^@ not a bad character if you use a proper encoding, but if you want to remove then try:

tr -d '\000'
sed 's/\000//g'

^M character is there in your example data

To convert your file to Unix/Linux format before any processing, try:

dos2unix filename - rhel and other

dos2ux filename [newfilename] - HP-UX"