view edit binaries with vim

Source: Internet
Author: User

view edit binaries with vim
vim makes it easy to edit binary files, which the individual thinks is better than the binary editing methods of Emacs. the editing of the binary files in Vim is the first to dump the file into the binary text form by the external program xxd, then it can edit the file according to the usual editing method, and then convert it to the original form with Xxd after the editing is done.

Can be divided into the following steps:

(1) First edit this file in binary mode: vim-b datafile

(2) Now use XXD to convert this file into 16 binary::%!xxd

The text looks like this:

0000000:1f8b 0808 39d7 173b 0203 7474 002b 4e49 .... 9..;.. TT.+NI0000010:4B2C 8660 eb9c ECAC c462 eb94 345e 2e30 K,.. 4^.00000020:373b 2731 0b22 0ca6 c1a2 d669 1035 39d9 7; ' 1. "..... i.59.

Now you can read and edit the text as you wish. Vim treats this information as plain text. Modifying the hexadecimal section does not result in a change in the part of the character that can be displayed, or vice versa.

(3) Finally, use the following command to convert it back::%!xxd-r

Only 16 of the modifications in the binary section will be used. Changes to the right can display text portions that are ignored.

XXD is a Linux command,vim can be passed "!" To invoke the external command, which is the function of hex dump or vice versa.

Vim Edit binary (vim manual) *23.4* binary file

You can use Vim to edit the binary file. Vim is not designed for this purpose, so there are some limitations. But you can read a file, change a character, and then save it to disk. The result is that only one character is changed for your file, and the others are exactly the same as the original one.

To ensure that vim does not use its clever tricks in the wrong place, start Vim with the "-B" parameter:

Vim-b datafile

This parameter sets the ' binary ' option. Its role is to exclude all unintended side effects. For example, ' TextWidth ' is set to zero so that text lines are not allowed to be typeset. Also, files are read in Unix file format.

Binary mode can be used to modify a program's message messages. Be careful not to insert or delete any characters, which can cause the program to run into problems. Use the "R" command to enter the replacement mode.

Many of the characters in the file are not displayed. Display their values in HEX format:

: Set Display=uhex

Alternatively, you can use the command "GA" to display the character values under the cursor. When the cursor is on a <Esc> character, the output of the command looks like this:

<^[>  , Hex 1b,  

There may not be so many line breaks in the file. You can turn off the ' wrap ' option to get an overview of the effects:

        : Set nowrap
Byte position:

To find the current byte position in your file, use this command:

G Ctrl-g

Its output is lengthy:

    

The last two digits are the current byte position in the file and the total number of file bytes. This has taken into account the effect of the ' fileformat ' option causing different line break bytes.

To move to a specified byte in a file, use the "Go" command. For example, to move to byte 2345:

2345go
Using Xxd:
A true binary editor is a two-way display of text: binary and hexadecimal format. You can achieve this effect by converting the program "Xxd" in vim. The program is released with Vim. 

First edit this file in binary mode:

Vim-b datafile

Now use XXD to convert this file into 16 binary:

        :%!xxd

The text looks like this:

        0000000:1f8b 0808 39d7 173b 0203 7474 002b 4e49 ...  . 9..;.. Tt.+ni         0000010:4b2c 8660 eb9c ECAC c462 eb94 345e 2e30  K,. ' ..... b.. 4^.0         0000020:373b 2731 0b22 0ca6 c1a2 d669 1035 39d9  

Now you can read and edit the text as you wish. Vim treats this information as plain text. Modifying the hexadecimal section does not result in a change in the part of the character that can be displayed, or vice versa.

Finally, use the following command to convert it back:

:%!xxd-r

Only 16 of the modifications in the binary section will be used. Changes to the right can display text portions that are ignored.

view edit binaries with vim

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.