In hexl-mode how can I make Emacs enter a state where it takes everything I enter as hex input until I tell it to stop? Eg. I want to tap "A", "B", "1", "0", etc., and have this entered as $AB, $10, etc. in the binary file, be able to cursor around, enter more hex values, and so forth, until I'm satisfied.

By default I have to press C-M-x for every hex value I want to enter, which is very counterproductive at least for the type of binary file I'm working with at the moment.


You could try something like (guaranteed 100% untested):

(defvar hexl-hex-insert-mode-map
  (let ((map (make-sparse-keymap)))
    (mapc (lambda (c)
            (define-key map (vector c) 'hexl-hex-self-insert))

(defvar hexl-hex--last-char nil)

(defun hexl-hex-self-insert ()
  "Self-insert \"nibble\"."
  (if (not (and hexl-hex--last-char
                (eq this-command last-command)))
      (setq hexl-hex--last-char last-command-event)
      (string hexl-hex--last-char last-command-event))
    (setq hexl-hex--last-char nil)))

(define-minor-mode hexl-hex-insert-mode
  "Hex insertion mode."
  :lighter " HI")
  • Assuming that works I think this, along with an octal variant, would make fine enhancements to hexl.el – phils Apr 11 '18 at 22:30
  • Seems to work! It feels a bit awkward that nothing prints until you've entered both nibbles. Can this be helped? Example: Enter first nibble: Set and printed; Cursor highlights second nibble. A) User enters second nibble: Set and printed; Cursor moves to next byte. B) If user moves cursor instead of entering second nibble: Cursor simply moves to first nibble of cursor destination. PS! One could also discuss whether general cursor navigation always should move in nibbles. I'm used to this from low level environments, but the current behaviour (moving in bytes) isn't a major nuisance. – forthrin Apr 12 '18 at 6:59
  • @forthrin: There's clearly room for improvement, indeed. I think that changing the code to move by nibbles and update nibbles directly would make it much better. – Stefan Apr 12 '18 at 12:02
  • @Stefan: Glad to hear you agree. It could be a setting (hexl-mode-nibble-navigation). At least now, I can manipulate hex data like a breeze. Would be great if it made it into the core too! – forthrin Apr 12 '18 at 12:18
  • @forthrin: Alright, alright, I just added a nhexl-nibble-edit-mode to my nhexl-mode package (available from GNU ELPA, the new version should appear there soon). – Stefan Apr 12 '18 at 15:40

hexl-insert-hex-string can enter a hex string in a hexl-mode buffer, but the string length cannot be greater than the file size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.