I have the following file named asterix
:
0000000: 15 00 1f c1 13 31 49 80 02 64 01 08 42 48 ef 28
0000010: 2d cd 0c 02 00 07 f1 e9 1b 04 63 30 c7 08 20
When I open this file in vim, I do %!xxd -r
, then %!xxd -g1
. I get this:
0000000: 15 00 1f d0 b0 13 31 49 e2 94 80 02 64 01 08 42 ...1I...d..B
0000010: 48 d0 9e 28 2d d0 bc 0c 02 00 07 d0 af d0 98 1b H..(-...
0000020: 04 63 30 d0 b3 08 20 0a .c0... .
But if in bash
I do xxd -r < asterix | xxd -g1
, I get the expected result:
0000000: 15 00 1f c1 13 31 49 80 02 64 01 08 42 48 ef 28 ...1I..d..BH.(
0000010: 2d cd 0c 02 00 07 f1 e9 1b 04 63 30 c7 08 20 -...c0..
I've tried doing e ++enc=c
in vim to prevent possible interference of encoding, but this doesn't seem to change anything.
So what's going on? How do I make vim work correctly with binary data (up to newline at the end, of course)?
I'm using VIM 7.4 (2013 Aug 10, compiled Jan 2 2014 19:39:59) on Ubuntu 14.04. When I run vim with env -i vim asterix
, it works as expected. But if I run it as usually, but logging in as a different user (the one without custom vim config, by e.g. su -
), the above described behavior persists.
I've now noticed that env -i
works because it resets locale. My default locale is this:
LANG=en_US
LC_CTYPE=ru_RU.UTF-8
LC_NUMERIC="en_US"
LC_TIME=en_GB.UTF-8
LC_COLLATE="en_US"
LC_MONETARY="en_US"
LC_MESSAGES="en_US"
LC_PAPER="en_US"
LC_NAME="en_US"
LC_ADDRESS="en_US"
LC_TELEPHONE="en_US"
LC_MEASUREMENT="en_US"
LC_IDENTIFICATION="en_US"
LC_ALL=
Setting LC_CTYPE=C
, I get Vim to work correctly (it appends 0a
to the end, but it's OK). Still I'd like to know why locale interferes with calling xxd -r
.
vim -b
, pasting the text and doing%!xxd -r
and%!xxd -g1
makes it work. You might want to turn this into an answer.