Function: hexl-mode

hexl-mode is an autoloaded, interactive and byte-compiled function defined in hexl.el.gz.

Signature

(hexl-mode &optional ARG)

Documentation

A mode for editing binary files in hex dump format.

This is not an ordinary major mode; it alters some aspects of the current mode's behavior, but not all; also, you can exit Hexl mode and return to the previous mode using C-c C-c (hexl-mode-exit).

This function automatically converts a buffer into the hexl format using the function hexlify-buffer.

Each line in the buffer has an "address" (displayed in hexadecimal) representing the offset into the file that the characters on this line are at and 16 characters from the file (displayed as hexadecimal values grouped every hexl-bits bits, and as their ASCII values).

If any of the characters (displayed as ASCII characters) are unprintable (control or meta characters) they will be replaced by periods.

If hexl-mode is invoked with an argument the buffer is assumed to be in hexl format.

A sample format:

  HEX ADDR: 0011 2233 4455 6677 8899 aabb ccdd eeff ASCII-TEXT
  -------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------
  00000000: 5468 6973 2069 7320 6865 786c 2d6d 6f64 This is hexl-mod
  00000010: 652e 2020 4561 6368 206c 696e 6520 7265 e. Each line re
  00000020: 7072 6573 656e 7473 2031 3620 6279 7465 presents 16 byte
  00000030: 7320 6173 2068 6578 6164 6563 696d 616c s as hexadecimal
  00000040: 2041 5343 4949 0a61 6e64 2070 7269 6e74 ASCII.and print
  00000050: 6162 6c65 2041 5343 4949 2063 6861 7261 able ASCII chara
  00000060: 6374 6572 732e 2020 416e 7920 636f 6e74 cters. Any cont
  00000070: 726f 6c20 6f72 206e 6f6e 2d41 5343 4949 rol or non-ASCII
  00000080: 2063 6861 7261 6374 6572 730a 6172 6520 characters.are
  00000090: 6469 7370 6c61 7965 6420 6173 2070 6572 displayed as per
  000000a0: 696f 6473 2069 6e20 7468 6520 7072 696e iods in the prin
  000000b0: 7461 626c 6520 6368 6172 6163 7465 7220 table character
  000000c0: 7265 6769 6f6e 2e0a region..

Movement is as simple as movement in a normal Emacs text buffer. Most cursor movement bindings are the same: use C-b (hexl-backward-char), C-f (hexl-forward-char), C-n (hexl-next-line), and C-p (hexl-previous-line) to move the cursor left, right, down, and up.

Advanced cursor movement commands (ala C-a (hexl-beginning-of-line), C-e (hexl-end-of-line), M-< (hexl-beginning-of-buffer), and M-> (hexl-end-of-buffer)) are also supported.

There are several ways to change text in hexl mode:

Self-inserting characters are bound to hexl-self-insert so you can simply type the character and it will insert itself (actually overstrike) into the buffer. However, inserting non-ASCII characters requires caution: the buffer's coding-system should correspond to the encoding on disk, and multibyte characters should be inserted with cursor on the first byte of a multibyte sequence whose length is identical to the length of the multibyte sequence to be inserted, otherwise this could produce invalid multibyte sequences. Non-ASCII characters in ISO-2022 encodings should preferably inserted byte by byte, to avoid problems caused by the designation sequences before the actual characters.

C-q (hexl-quoted-insert) followed by another keystroke allows you to insert the key even if it isn't bound to self-insert. An octal number can be supplied in place of another key to insert the octal number's ASCII representation.

C-M-x (hexl-insert-hex-char) will insert a given hexadecimal value into the buffer at the current address.

C-M-o (hexl-insert-octal-char) will insert a given octal value into the buffer at the current address.

C-M-d (hexl-insert-decimal-char) will insert a given decimal value into the buffer at the current address..

C-c C-c (hexl-mode-exit) will exit hexl-mode.

Note: saving the file with any of the usual Emacs commands will actually convert it back to binary format while saving.

You can use M-x hexl-find-file (hexl-find-file) to visit a file in Hexl mode.

C-h b (describe-bindings) for advanced commands.

Probably introduced at or before Emacs version 19.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/hexl.el.gz
;;;###autoload
(defun hexl-mode (&optional arg)
  "A mode for editing binary files in hex dump format.
\\<hexl-mode-map>This is not an ordinary major mode; it alters some aspects
of the current mode's behavior, but not all; also, you can exit
Hexl mode and return to the previous mode using \\[hexl-mode-exit].

This function automatically converts a buffer into the hexl format
using the function `hexlify-buffer'.

Each line in the buffer has an \"address\" (displayed in hexadecimal)
representing the offset into the file that the characters on this line
are at and 16 characters from the file (displayed as hexadecimal
values grouped every `hexl-bits' bits, and as their ASCII values).

If any of the characters (displayed as ASCII characters) are
unprintable (control or meta characters) they will be replaced by
periods.

If `hexl-mode' is invoked with an argument the buffer is assumed to be
in hexl format.

A sample format:

  HEX ADDR: 0011 2233 4455 6677 8899 aabb ccdd eeff     ASCII-TEXT
  --------  ---- ---- ---- ---- ---- ---- ---- ----  ----------------
  00000000: 5468 6973 2069 7320 6865 786c 2d6d 6f64  This is hexl-mod
  00000010: 652e 2020 4561 6368 206c 696e 6520 7265  e.  Each line re
  00000020: 7072 6573 656e 7473 2031 3620 6279 7465  presents 16 byte
  00000030: 7320 6173 2068 6578 6164 6563 696d 616c  s as hexadecimal
  00000040: 2041 5343 4949 0a61 6e64 2070 7269 6e74   ASCII.and print
  00000050: 6162 6c65 2041 5343 4949 2063 6861 7261  able ASCII chara
  00000060: 6374 6572 732e 2020 416e 7920 636f 6e74  cters.  Any cont
  00000070: 726f 6c20 6f72 206e 6f6e 2d41 5343 4949  rol or non-ASCII
  00000080: 2063 6861 7261 6374 6572 730a 6172 6520   characters.are
  00000090: 6469 7370 6c61 7965 6420 6173 2070 6572  displayed as per
  000000a0: 696f 6473 2069 6e20 7468 6520 7072 696e  iods in the prin
  000000b0: 7461 626c 6520 6368 6172 6163 7465 7220  table character
  000000c0: 7265 6769 6f6e 2e0a                      region..

Movement is as simple as movement in a normal Emacs text buffer.
Most cursor movement bindings are the same: use \\[hexl-backward-char], \\[hexl-forward-char], \\[hexl-next-line], and \\[hexl-previous-line]
to move the cursor left, right, down, and up.

Advanced cursor movement commands (ala \\[hexl-beginning-of-line], \\[hexl-end-of-line], \\[hexl-beginning-of-buffer], and \\[hexl-end-of-buffer]) are
also supported.

There are several ways to change text in hexl mode:

Self-inserting characters are bound to `hexl-self-insert' so you
can simply type the character and it will insert itself (actually
overstrike) into the buffer.  However, inserting non-ASCII characters
requires caution: the buffer's coding-system should correspond to
the encoding on disk, and multibyte characters should be inserted
with cursor on the first byte of a multibyte sequence whose length
is identical to the length of the multibyte sequence to be inserted,
otherwise this could produce invalid multibyte sequences.  Non-ASCII
characters in ISO-2022 encodings should preferably inserted byte by
byte, to avoid problems caused by the designation sequences before
the actual characters.

\\[hexl-quoted-insert] followed by another keystroke allows you to insert the key even if
it isn't bound to self-insert.  An octal number can be supplied in place
of another key to insert the octal number's ASCII representation.

\\[hexl-insert-hex-char] will insert a given hexadecimal value
into the buffer at the current address.

\\[hexl-insert-octal-char] will insert a given octal value
into the buffer at the current address.

\\[hexl-insert-decimal-char] will insert a given decimal value
into the buffer at the current address..

\\[hexl-mode-exit] will exit `hexl-mode'.

Note: saving the file with any of the usual Emacs commands
will actually convert it back to binary format while saving.

You can use \\[hexl-find-file] to visit a file in Hexl mode.

\\[describe-bindings] for advanced commands."
  (interactive "p")
  (unless (eq major-mode 'hexl-mode)
    (let ((modified (buffer-modified-p))
	  (inhibit-read-only t)
          (point-offset (bufferpos-to-filepos (point) 'exact)))
      ;; If `hexl-mode' is invoked with an argument the buffer is assumed to
      ;; be in hexl format.
      (when (memq arg '(1 nil))
        (hexlify-buffer)
        (restore-buffer-modified-p modified))
      (setq hexl-max-address
            (+ (* (/ (1- (buffer-size)) (hexl-line-displen)) 16) 15))
      (condition-case nil
	  (hexl-goto-address point-offset)
	(error nil)))

    (let ((max-address hexl-max-address))
      (major-mode-suspend)
      (setq hexl-max-address max-address))

    (use-local-map hexl-mode-map)

    (setq-local mode-name "Hexl")
    (setq-local isearch-search-fun-function #'hexl-isearch-search-function)
    (setq-local major-mode 'hexl-mode)

    ;; (set-syntax-table (standard-syntax-table))

    (add-hook 'write-contents-functions #'hexl-save-buffer nil t)

    (setq-local require-final-newline nil)


    (setq-local font-lock-defaults '(hexl-font-lock-keywords t))
    (setq-local font-lock-extra-managed-props '(display))

    (setq-local revert-buffer-function #'hexl-revert-buffer-function)
    (add-hook 'change-major-mode-hook #'hexl-maybe-dehexlify-buffer nil t)

    ;; Set a callback function for eldoc.
    (add-hook 'eldoc-documentation-functions
              #'hexl-print-current-point-info nil t)
    (eldoc-add-command-completions "hexl-")
    (eldoc-remove-command "hexl-save-buffer"
			  "hexl-current-address")

    (if hexl-follow-ascii (hexl-follow-ascii-mode 1)))
  (run-mode-hooks 'hexl-mode-hook))