Hacker News new | past | comments | ask | show | jobs | submit login

And `%!command` to replace the whole file. That's how editing hexadecimal in vim works. `%!xxd` to turn it, then `%!xxd -r` to turn it back.

Another useful one is `%!jq .` to pretty print a json file.




I use this often. I usually enter `:%!xxd -g1` to show each byte value separately instead of showing two bytes together.

    $ printf "\x00\x01\x02\x03" | xxd
    0000000: 0001 0203
    $ printf "\x00\x01\x02\x03" | xxd -g1
    0000000: 00 01 02 03
While editing binary files in Vim, one needs to be careful about setting the binary option before editing a binary file or opening the binary file with `-b` option.

For example,

    printf "\x00\x01\x02\x03" > a.bin
    vim a.bin
    :%!xxd -g1
would display

    0000000: 00 01 02 03 0a                                   .....
(The `printf` and `vim` commands are entered in the shell. The `:%!xxd` command is entered in Vim.)

In the above example, a newline has been added by Vim where none existed in the binary file. In fact, if you then run `%!xxd -r` followed by `:w`, this additional newline would be written back to the file. Here is the right way to preserve the binary data in a binary file.

    printf "\x00\x01\x02\x03" > a.bin
    vim -b a.bin
    :%!xxd -g1
would display

    0000000: 00 01 02 03                                      ....
Only the four bytes in the file appear now. Alternatively, one may enter `:set bin` in Vim before editing a binary file.


You can also use a range e.g. `5,10!column -t` would align columns for lines 5 to 10.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: