3

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.

11
  • I cannot reproduce on Vim 8.0.0056, Ubuntu 16.04. I get the expected result.
    – muru
    Commented Feb 17, 2017 at 12:49
  • @muru I'm using VIM 7.4 (2013 Aug 10, compiled Jan 2 2014 19:39:59) on Ubuntu 14.04.
    – Ruslan
    Commented Feb 17, 2017 at 12:51
  • Good old Ubuntu and its hopelessly out of date repositories (we can't be rushing into crazy new versions of Vim now, can we?)
    – user859
    Commented Feb 17, 2017 at 13:10
  • 2
    Try loading the file in binary mode Commented Feb 18, 2017 at 19:57
  • 2
    @ChristianBrabandt OK, this does work. So even without "loading a file", i.e. just starting vim -b, pasting the text and doing %!xxd -r and %!xxd -g1 makes it work. You might want to turn this into an answer.
    – Ruslan
    Commented Feb 19, 2017 at 5:08

1 Answer 1

3

Edit: notice after posting there's already this answer in the comments.

Run vim in binary mode as vim -b asterix. I think the problem has something to do with conversions caused by encoding/fileencoding settings.

If your file is already loaded, you can switch to binary mode without leaving vim. Save the file, then do :e ++bin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.