From 39cbfea2348e7ac8d26d4a4540074bdc46a9686a Mon Sep 17 00:00:00 2001 From: AlenikaKoda Date: Fri, 28 Aug 2026 01:31:46 -0300 Subject: [PATCH] Hex editor will not discard data if not saved. --- nvim/README.md | 2 +- nvim/nvim.sh | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/nvim/README.md b/nvim/README.md index c824503..bac90e0 100644 --- a/nvim/README.md +++ b/nvim/README.md @@ -199,7 +199,7 @@ Neovim's embedded terminal launches **fish** instead of your login shell, purely `h` toggles the current buffer between normal and hex-dump view (backed by `xxd`), rather than showing both simultaneously. That's deliberate: the hex dump is just ordinary buffer text under the hood, so editing it gets normal Neovim undo/redo for free, and nothing writes to disk until an explicit `:w` — same as any other file. A plugin doing a true live simultaneous hex+ASCII view was tried first, but it manages bytes with its own logic outside normal buffer editing, which is exactly why it had no undo at all. -The mapping also makes sure the buffer is actually loaded in binary mode before toggling (re-reading it with `++bin` if not) — opening a real binary file without that causes Neovim to misinterpret raw bytes as text before `xxd` even runs, surfacing as a `CONVERSION ERROR` instead of a clean hex dump. +The mapping also makes sure the buffer is actually loaded in binary mode before toggling (re-reading it with `++bin` if not) — opening a real binary file without that causes Neovim to misinterpret raw bytes as text before `xxd` even runs, surfacing as a `CONVERSION ERROR` instead of a clean hex dump. Since that reload discards whatever's currently in the buffer, `h` refuses to run it if there are unsaved changes — save (or explicitly discard with `:e!`) first. ## Formatting and linting diff --git a/nvim/nvim.sh b/nvim/nvim.sh index 9f8fc57..607144b 100755 --- a/nvim/nvim.sh +++ b/nvim/nvim.sh @@ -1144,8 +1144,18 @@ return { -- rather than a clean hex dump, and it happens BEFORE xxd -- ever runs. Re-reading with ++bin loads the file correctly -- from scratch instead of operating on an already-misread - -- buffer. Forced (!) since there's nothing worth preserving - -- from a buffer that was misread as text in the first place. + -- buffer. + -- + -- That reload is forced (!), which discards whatever's + -- currently in the buffer without asking -- refuse to do it + -- if there are unsaved changes rather than silently losing + -- them. Only relevant on this first switch into hex mode: + -- once vim.bo.binary is already true, no reload happens and + -- this check doesn't apply. + if vim.bo.modified then + vim.notify("Buffer has unsaved changes -- save first (or :e! to discard) before opening hex view", vim.log.levels.WARN) + return + end vim.cmd('edit! ++bin %') end vim.cmd('HexToggle')