Select Page

Introduction

No pro programmer is unaware of the fact that all the day to day coding they carry out in any given programming language, are essentially being converted into numbers by the computer, known as “Binary”. They are a composition of 0s and 1s arranged in specific sequences that the computer is able to understand and interpret. Who doesn’t know that? Well even a grade-schooler does, probably! 

But at times, a programmer may need to fiddle with the binary codes directly, by means of using a Hex Editor. Wait, you’re saying I didn’t tell you what hex means? My apologies!

In the following section, we’ll learn about hex in brief, for those who don’t know what it is, and I’ll show you how to use your text editor of choice (which of course is Vim – don’t lie to me) for editing the hex code without using a separate hex editor.


What is HEX?

As you already know, binaries are long strings of 0s and 1s that represent data the computers is able to understand. Meanwhile, humans are not as systematic as computers are in terms of reading numbers. Which is why, the dominant of mammals (humans) have designed a rather effortless way of converting these long strings into a more compact, and readable format, called the HEX.

Hex is a Greek word that stands for “six”. Decimals are the 10 digit numerals from 0 to 9, as commonly used in Mathematics. Hence the term, Hexadecimal, which altogether means “16”.

Hex is the shortened form of “Hexadecimal“, and is also known as Base 16. It is a type of numerical system that includes the English alphabets A to F, which represent the numbers from 10 to 15 respectively, after the Decimal numbers from 0 to 9. Sounds, pretty complicated, doesn’t it? Well let’s try converting some binaries to get a clearer view.

BinaryHexadecimal
0001 0001 0001111
0101 0101 0101555
1010A
1010 1100AC
1010 1010 1010AAA
1010 1011 1111 1111ABFF

The long strings of binary are first divided into 4-digits, which can be converted to a single hexadecimal digit, as seen on the 3rd row. This way, long strings of binary can be converted to the short hexadecimals, which is rather more convenient and easy to read. If at some point, you need to handle a large binary file that contains millions if not billions of digits that represent data, you will need a hex editor that is capable of representing the binary and allows you manipulate it using hexadecimals.

A Hex Editor is a special type of text editor that allows easy analysis, interpretation and modification of binary code, without having to look at their convention of 0s and 1s. There are many different editors that are capable of doing so, but it’s not probably what you’re here for.

Get ready the be the bearer of some good news, because you can edit HEX right from the comfort of your favourite text editor, Vim!


Why use Vim for editing HEX?

Easy questions always have easy answers. If you’re prominent at using Vim, you probably already carry out all your coding stuff using the Vim Editor all by itself, be it HTML or be it Assembly. For Vim users, being habituated to the “Mode-Shift Editing” scheme of Vim makes any other code editor seem like a toddler’s toy. Editing HEX is of no difference. You’d surely not want to use a separate form of editor for this one either. For which case, Vim has a built in command called xxd, as well as some third-party plugins to help you with hex if you need a broader and more appealing approach.

The next section will give you an in-depth insight on how you can use Vim as a Hex Editor. It’s time to get your hands dirty!


How to use Vim as a HEX Editor

Using the Command line

For starters, there’s an external shell command called xxd that can be used from the Vim Command-line mode to convert a binary file directly inside of Vim. Once you’ve opened up a binary file (or any other code file), use the following command to convert the binary into a hex representation (remember to take a backup of the file to avoid data loss):

:%!xxd

Take for instance the following C Language source file I’ve copied from my DWM Installation. It is not a binary file, but the procedure is the same for binary files too. I’m using this file to better demonstrate how editing the hex code would actually change the content of the code, as editing a binary file directly will not give any quick insight on the change.


You can now edit the HEX code present in the converted file however you want. Make note of the text-preview on the right side of all the HEX code. Altering that text will not make any changes to the actual code.

If you’re done tweaking, you can convert back to the original file format using the following command:

:%!xxd -r

Try looking at the following gif if you’re unable to understand the process:

Notice that the text inside the actual code file changes as I insert a different set of hexadecimals. This way, you can easily read and manipulate hex code right from the comfort of using Vim.


You can even highlight the seemingly plain color of it to a more approachable one, by using:

:set ft=xxd

However, this process may still seem like a bit tedious for many, as it involves manual conversion of the code to hex using an external shell command. You can map a keybinding with the given commands, to make things easier. But it still lacks a few features you would usually get in a conventional hex editor. For this very reason, the Vim community has come up with plugins you can use to get stuff done, which come with more automation and accessibility.


Using a plugin

There are quite a few plugins for editing HEX, but I personally prefer using hexmode by Fidian. It is simple and automates the conversion of hex with as little effort as possible. if you need more features, you may want to check out hexman.vim.

To install the Hexmode plugin, use your Plugin manager of choice, or if you’re using Vim version 8 or above, and don’t want to use a plugin manager, follow these steps inside your terminal shell:

$ cd ~
$ mkdir -p .vim/pack/vendor/start/
$ cd ~/.vim/pack/vendor/start/
$ git clone https://github.com/fidian/hexmode.git

If you’ve successfully installed it, you’ll get a new flag you can use when starting vim. If you use the following command from a terminal shell, any file you specify (including image files) will open up in the hex representation. Why not give it a try?

$ vim -b <filename>

Take the following as an example:

This plugin uses the same xxd shell command under the hood, but makes it a bit more approachable as compared to using xxd manually. Also notice that the color is automatically highlighted and you don’t need to use the :set ft=xxd command.


We’re not done with the plugin just yet! There might be times when you’d want to edit the hex code while you’re already editing a file, and then switch back to the text mode once you’re done. We won’t need to use the xxd command now, as we already have the plugin installed. Use the following command inside of Vim command-line mode to toggle between HEX and normal text.

:Hexmode

You can also map the command to a keyboard shortcut for convenience, if you prefer that. Inside of you ~/.vimrc file, append the following line of code:

noremap <C-h> :Hexmode<CR>

Now simply pressing Ctrl + h in your keyboard will allow you to toggle between HEX and text mode. Change the keybinding to anything you like, but make sure to use the correct syntax.


Our journey doesn’t end here, though. We’ve got one more important thing to cover! In case you want to open certain file types like .bin or .exe in HEX mode by default without having to pass the -b flag when starting vim, add the following line of code to your ~/.vimrc file:

let g:hexmode_patterns = '*.bin,*.exe,*.dat,*.o'  " --> Add any other extension you wish

I’ve got mine set up for opening files with .c extension to give you a proof that it actually works.

Conclusion

I’m hopeful that, you can now finally use your favourite editor Vim, to edit hex files without needing a seperate hex editor. You can carry out all the editing functionalities of vim in hex mode, which should give you and edge over those using a different editor for the task. Yes, those editors are specifically designed for manipulating hex files, and some go even further in terms of data analysis. But as long as you don’t care about all of that, and simply like using Vim, this is probably the best way you could get your HEX stuff done!