0

I use vim regularily for all my needs. On very rare occasions I need to open binary files via a hex editor look, and for that I start vim on the file and then run it through xxd via the command: %!xxd

My question is, how can I have my command line open a file directly in this manner, if the option exists? something like typing: gvimbin <file> and then it opens in the right manner.

Edit: to be clear, I am looking for a complete solution that allows running vim exec commands on startup.

ygoncho
  • 367
  • 1
  • 2
  • 12
  • Run it through xxd first and pipe the output to vim? `xxd file | vim -`. You can put that in a script or function. – Felix Kling Jan 23 '14 at 08:25
  • Thanks for your answer, but this doesn't work (gvim won't accept text to the editor as input, only file names) and also doesn't solve my general need which is to start vim with an exec command. Thanks anyways though :) – ygoncho Jan 23 '14 at 08:30
  • Mmh: [Can I redirect output of a program to gvim?](http://stackoverflow.com/questions/856372/can-i-redirect-output-of-a-program-to-gvim). But yeah, I don't know if you can run an external command on startup. But if the command is used only to transform the content of a file, then I don't see a reason why not to execute the external command first and pass the output to vim. – Felix Kling Jan 23 '14 at 08:33
  • You're right, sorry, didn't see that dash in the end. – ygoncho Jan 23 '14 at 09:01

2 Answers2

2

You can execute commands after Vim startup by passing them via -c:

$ gvim -c '%!xxd' file.bin

This can even be simplified so that when Vim is started in binary mode (-b argument), it'll automatically convert the file. Put the following into your ~/.vimrc:

if &binary
    %!xxd
endif

and start with:

$ gvim -b file.bin

Also have a look at the hexman.vim - Simpler Hex viewing and editing plugin; it makes it easier to deal with hexdumps.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

Like Felix say:

xxd <file> | vim -

You can put this into script for example vimxxd:

#!/bin/sh
xxd $1 | vim -

and use like: vimxxd file.txt

marioosh
  • 27,328
  • 49
  • 143
  • 192