Code Blog

Displaying 1-1 of 1 result.
2016/02/15 Programming

I used to be an Emacs user, but switched to use Sublime Text the last few years. In any case I never got too familiar with the vim editor. However, vim editor is still quite useful as a terminal tool for editing as its pretty much always available. This time I needed to do some hex edit of a binary file but needed to look up some commands how to actually use it, see notes below.

Hex edit in Vi / Vim

Change into hex edit mode :%!xxd
Revert from hex mode :%!xxd -r
Global search and replace of string1 to string2 :g/string1/s//string2/g 

I noticed though that after turning into hex edit mode, the regexp will match all characters, so need to pay attention if one wants to match a sequence of 3 hex chars, as "1B04 05" will not match "1B 0405".

Btw, the difference between vi and vim is that vi is the standard pre-installed editor on Unix systems (first release dating back to 1976!), and vim (short for vi improved) is a cloned version with more functions than vi (originally written for the Amiga!).

Insert before the cursor (insert mode) i
Exit insert mode Esc
Delete from cursor to end of the line D
Write and quit :wq
Quit and don't save :q!

For more commands for example go to Vim Cheat Sheet (external link)

Run "vitutor" or "vimtutor" in the terminal will start vi/vim and load a beginners guide tutorial into the editor.