Using Vim to Display a File in Hex

There are many ways to open/view a file in a hexadecimal representation. One way I can think of right now is to:

  1. Run hexdump -C <filename> > output.txt to open the file and save it onto a file called output.txt. You can then open this output.txt file separately and view it.
  2. Open the file in Vim directly as hexadecimal.

Let’s say you want option 2 cause it’ll save you some time (and not create a separate file). To do this, let’s create simple .txt file called hello_world.txt that contains “Hello World!”:

➜ cat hello_world.txt
Hello World!
➜ hexdump -C hello_world.txt
00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 0a |Hello World!.|

Vim has a pretty cool feature that allows you to run external shell command without ever exiting it. Now if you open the file in Vim and type :%!hexdump -C, you’ll get this:

What this does is to pipe all the contents of the currently opened file into hexdump, and then replace it with the output of hexdump -C. Realize that this is exactly the same output as what you see using the hexdump command on a terminal.

This works for binary files as well. I’ll give you another example. I’ve created a C++ source file called hello_world.cpp that does this:

#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;

Now I’ll compile this into binary:

➜ g++ hello_world.cpp -o hello_world
➜ ./hello_world
Hello World!

If you attempt to open this hello_world executable/binary file, you’ll get this:

Output of the hello_world executable/binary file

That’s not very useful! I can’t what’s going on here! However, if you type :%!hexdump -C:

Front of hello_world executable/binary file in hexadecimal
End of hello_world executable/binary file in hexadecimal

That’s it! You’ve learned how to use Vim to open files in hexadecimal mode! Running

hexdump -C hello_world

should have similar output as what you see above.