Viewing a line in hexadecimal from within the vi editor

If I want to view a line as hexadecimal characters within the vi editor, I can use vi's capability to call an external program with the ! character to see the hexadecimal equivalent of the characters in a line. E.g., if I have a line such as the following that I know has some nonprinting characters before the single quote and alphabetic characters for 'current_theme'I may want to see what nonprinting characters are on the line.

                                'current_theme' => array(
                                        'label' => $txt['theme_current_settings'],

If vi isn't showing me the line number for lines, I can first have it display the line numbers with :set number. I can then use :address_range!xxd to switch to a hex display of the data on the line. E.g., while attempting to resolve an issue of extra or omitted non-printing characters in a line while upgrading CometChat on a system, I wanted to view the contents of a line where there was an issue as hexadecimal characters, so I could see how many tabs and other characters were also present on the line. After moving to the relevant line, line 222, in vi, I issued the vi command :222!xxd. I then saw the following, which revealed there were 4 tab characters, hexadecimal 09, before 'current_theme on the line. To determine what the hexadecimal character codes represent, consult an ASCII table.

 222 0000000: 0909 0909 2763 7572 7265 6e74 5f74 6865  ....'current_the
 223 0000010: 6d65 2720 3d3e 2061 7272 6179 280a       me' => array(.
 224                                         'label' => $txt['theme_current_s        ettings'],

I could also just use the period character to represent the current line, so didn't actually have to display the line numbers. E.g., I could have used :.!xxd to have xxd display the current line in hexadecimal.

0000000: 0909 0909 2763 7572 7265 6e74 5f74 6865  ....'current_the
0000010: 6d65 2720 3d3e 2061 7272 6179 280a       me' => array(.
                                        'label' => $txt['theme_current_settings'],

I can use :edit! to go back to the plain ASCII display - edit! will cause the file to be reloaded from disk. You will lose any modifications you have made, if you haven't saved the file. If you did save the file, you would be saving the lines as they were displayed on the screen, i.e., with the characters represented by hexadecimal numbers, which is likely not what you want.

References:

  1. Using vi as a hex editor
    Written on: April 21, 2009
    By: Kev (YorkshireKev)
    Kev's Site
  2. Piping Vim Buffer Through Unix Filters (! and !! Commands)
    Last modified: August 5, 2013
    Softpanorama

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px