Hex editor will not discard data if not saved.
This commit is contained in:
1 parent
bd598f5f0e
commit
39cbfea234
2 files changed
+13
-3
No files matched your search
+1
-1
@@ -199,7 +199,7 @@ Neovim's embedded terminal launches **fish** instead of your login shell, purely
|
||||
|
||||
`<leader>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, `<leader>h` refuses to run it if there are unsaved changes — save (or explicitly discard with `:e!`) first.
|
||||
|
||||
## Formatting and linting
|
||||
|
||||
|
||||
+12
-2
@@ -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')
|
||||
|
||||
Reference in new issue
Block a user