vimtricks.wiki Concise Vim tricks, one at a time.

How do I edit a binary file in hex mode using Vim?

Answer

:%!xxd

Explanation

Vim can serve as a hex editor by piping buffer contents through xxd, a hex dump utility that ships with Vim. This lets you inspect and modify individual bytes in binary files like executables, images, or data formats directly within Vim.

How it works

  • :%!xxd pipes the entire buffer through xxd, converting binary content to a readable hex dump
  • Edit the hex values in the left column (the ASCII representation on the right is ignored when converting back)
  • :%!xxd -r converts the hex dump back to binary
  • Save with :w to write the modified binary

Example

" Open a binary file
vim -b data.bin

" Convert to hex view
:%!xxd

The buffer now shows:

00000000: 4865 6c6c 6f20 576f 726c 640a 5468 6973  Hello World.This
00000010: 2069 7320 6120 7465 7374 0a              is a test.
" Edit the hex values, then convert back
:%!xxd -r

" Save the binary
:w

Tips

  • Always open binary files with vim -b or :set binary to prevent Vim from adding a trailing newline or converting line endings
  • Only edit the hex columns on the left — changes to the ASCII preview on the right side are ignored by xxd -r
  • Use :set display=uhex to display unprintable characters as hex in normal mode without the full hex dump view
  • You can visually select a range and pipe just that range through xxd for partial hex viewing

Next

How do I return to normal mode from absolutely any mode in Vim?