VIM series: binary file editing

Source: Internet
Author: User
VIM series: Editing a binary file vim is generally used for text editing. for example, you can write code to edit a binary file. However, in Linux, some binary files, such as binary data files, can be used in a certain range... VIM series: Editing a binary file vim is generally used for text editing. for example, you can write code to edit a binary file. However, in Linux, some binary files, such as binary data files, can also be viewed and modified using vim within a certain range. When viewing and locating a binary file, make sure that Vim does not use the clever tips in the wrong way. when starting Vim, add the "-B" parameter: 1vim-B datafile sets the 'binary 'option. Its role is to exclude all unexpected side effects. For example, 'textidth' is set to zero, so that text lines are not typographical. In addition, all files are read in Unix format. Binary mode can be used to modify the message of a program. Be careful not to insert or delete any characters, which will cause program running problems. Use the "R" command to enter the replacement mode. Many characters in the file are not displayed. Use the Hex format to display their values: 1: set display = uhex. In addition, you can also use the command "ga" to display the character values under the cursor. When the cursor is placed on a character, the output of the command looks like this: 27, Hex 1b, and Octal 033 may not have so many line breaks. You can turn off the 'wrapp' option to get a general view of the effect: set nowrap to find your current byte location in the file, please use this command: g CTRL-G its output is very lengthy: col 9-16 of 9-16; Line 277 of 330; Word 1806 of 2058; Byte 10580 of 12206 the last two digits are the current Byte position and total number of bytes in the file. This has taken into account the effect of the 'fileformat' option on line breaks. To move a specified byte to a file, run the "go" command. For example, to move to byte 2345: 2345go, use XXD to use a real binary editor to display text in two ways: binary and hexadecimal formats. You can achieve this through the conversion program "xxd" in Vim. This program is released along with Vim. First, edit the file in binary mode: vim-B datafile. now, use xxd to convert the file to hexadecimal: %! Xxd 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 7; '1. "..... I .59. now you can read and edit the text as you like. Vim treats such information as plain text. Changing the hexadecimal part does not change the printable character part, and vice versa. Finally, use the following command to convert it back: %! Xxd-r is used only when the hexadecimal part is modified. The modification of the text area displayed on the right is negligible. PS: of course, there are multiple better tools available for viewing binary files in Linux. commonly used tools such as: od tool: dump files in octal and other formatsxxd: make a hexdump or do the reverse. hexdump tool: ASCII, decimal, hexadecimal, octal dump.
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.