5

Hopefully for you this would be trivial to answer.

I have gvim and vim on Win7. Any time I redirect command result to file and open it with Vim (or Gvim, same effect) I have binary 0 (^@ sign) inserted between every characters.

So, instead of

r96130

I have

^@r^@9^@6^@1^@3^@0^@

Any idea how to (preferred) prevent it or (fine) get rid of it?

I tried substitution but obviously this ain't just ^@, it's a binary zero (I think) so it's not picked up on /^@.

Edit(s):

1) Tried setting encoding to UTF-8 (encoding of content I am working on) and UTF-16 (per @Philippe Wendler's answer, thank you), to no avail.

2) __PowerShell__ is the guilty party. When I do the redirection on usual command line (via cmd) then there are no side-effects. Only files created on PowerShell sessions have these binary zeroes inserted.

3) Powershell goodies that @Christian mentioned didn't help. I'll leave the question unanswered as I've switched to Ubuntu as my main OS and use Vim there, so the only 'solution' here is now not to use PowerShell + Vim, which obviously is lacking as an answer. :-)

Solution!

Thanks @Emperor XLII for a solution! Posting here as it was in a comment so might not be visible enough:

dir | OutFile -Encoding UTF8 test 

creates a file that appears in Vim without binary zeroes. Same happens if I run with Encoding to OEM, ASCII, and two others I've tested.

A promising tip lies in :he 'fileencodings' - yes, plural, by @Dan Fitch, but I haven't tested it as I aready moved away from Win7 by that time.

4 Answers4

4

Windows uses UTF-16 internally as Unicode encoding, and this file looks like it also uses UTF-16, but vim interprets it with an 8-bit charset (probably latin1).

Try :set encoding=utf-16 in vim to select the right encoding.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • Strange that it didn't help, the symptoms look just like it should help. Unfortunately, as I don't have Windows and PowerShell, I can't help you further. – Philipp Wendler Jun 29 '12 at 12:06
3

I had the same issue. Using a cmd shell (or Console2) and running a command such as this:

prompt> myBatFile.bat > myOutputFile.log

I would get a bunch of ^@ symbols, which I could get rid of using a macro that just ran the following command:

:s/\r\(\n\)/\1/g

This was acceptable for me, but annoying (I believe that it was a combination of having the different unix vs. Dos-style carriage returns and having no end-of-line at the end of the file). To make matters worse, I started using PowerShell once I moved to Windows 7. Once this happened, not only did I get ^@ symbols, but I also had ^M sprinkled in there along with a ÿþ at the beginning of the file (I think that was a byte order mark that gVIM was not able to interpret properly).

At any rate, I changed how I called my bat file so that it followed the PowerShell way of redirecting and now have no issues reading the file in gVIM:

prompt> myBatFile.bat | Out-File myOutputFile.log -encoding UTF8

Hope that helps.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
1

In powershell try to redirect in a file using

set-content c:\file.txt -encoding Unicode # or BigEndianUnicode

you can also use out-file in the same manner

CB.
  • 58,865
  • 9
  • 159
  • 159
  • Thanks Christian. Will try that on Monday and let you know. – LAFK 4Monica_banAI_modStrike Jun 30 '12 at 09:45
  • `mvn -version | Out-File test -encoding Unicode vim test` Binary zeroes still present. Unfortunately. Same for set-content. – LAFK 4Monica_banAI_modStrike Jul 27 '12 at 20:27
  • 1
    Since the default encoding used by `Out-File` is Unicode (`Get-Help Out-File -Parameter Encoding`), it would make more sense to specify a non-default value like UTF8 or even ASCII if you wanted to use a legacy program like `vim` that does not recognize modern encodings. – Emperor XLII Aug 11 '12 at 13:44
  • Calling Vim a legacy program fits no definition of legacy program that I know. :-) :-P Will try that out and post results. If you wish this to be accepted (which will happen if this works) then post it as an answer. – LAFK 4Monica_banAI_modStrike Oct 08 '12 at 12:41
  • Your answer works, Emperor - my thanks! If you post this as an answer, I'll accept it. Running `dir | OutFile -Encoding UTF8 test` creates a file that appears in Vim without binary zeroes. Same happens if I run with Encoding to OEM, ASCII, and two others I've tested. – LAFK 4Monica_banAI_modStrike Oct 10 '12 at 10:18
1

You shouldn't have to do anything special with Powershell's output. You can change your vim configuration so it can deal with the correct encodings.

Look into :he fileencodings (note the trailing s) -- this setting lets you choose what encodings vim will try. By default, Vim 7.x will use ucs-bom and try to decode the byte order mark, but your vimrc may be setting a different list; make sure it isn't starting with something like latin1. By default, on my x64 Windows 7 machine, Powershell command output piped to a file is read by vim just fine.

Either setting fileencodings to ucs-bom or utf-16 should fix your problem.

Dan Fitch
  • 2,480
  • 2
  • 23
  • 39
  • This worked for me. The key was the ucs-bom (I had utf-8,iso8859-1,ibm850 for various reasons, but the ucs-bom in front of all of those solved the same issue for me). – Jason Down Aug 26 '13 at 20:33