[PATCH 1/2] Update the vimscript code in ":h hex-editing"

68 views
Skip to first unread message

Dragan Simic

unread,
Aug 8, 2023, 4:47:33 AM8/8/23
to vim...@vim.org

This version saves and restores the view position while saving the
buffer, to keep the current view unchanged after doing ":w", which
previously caused the window to scroll back to the very beginning of
the buffer. I believe it's needles to say how annoying and actually
counterproductive that was.

This version also eliminates the "Press ENTER or type command to
continue" message, which was previously displayed after opening larger
binary files. The use of "silent" and "redraw!" commands is also
tailored specifically to avoid screen flickering, e.g. when doing
":w", caused by the buffer being filtered by an external command.

While there, reformat the code to make it more readable, and use longer
versions of the commands, to make it slightly more consistent and more
understandable to newcomers.
---
runtime/doc/tips.txt | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)

0001-Update-the-vimscript-code-in-h-hex-editing.patch

Dragan Simic

unread,
Aug 8, 2023, 4:49:24 AM8/8/23
to vim...@vim.org

Thanks to the input file's dual nature, so to speak, using xxd(1)
to filter and edit binary files makes restoring the cursor position
broken. Fix that by ignoring the appropriate file type in the code
that restores the cursor position.

While there, refactor the code a bit to save a few CPU cycles and
to keep the line lengths in check, and use longer versions of some
of the commands, to make it slightly more consistent and more
understandable to newcomers.
---
runtime/doc/usr_05.txt | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

0002-Update-the-vimscript-code-in-h-restore-cursor.patch

Christian Brabandt

unread,
Aug 8, 2023, 2:11:38 PM8/8/23
to vim...@googlegroups.com
> diff --git a/runtime/doc/tips.txt b/runtime/doc/tips.txt
> index ea8d538bf..79b1574e2 100644
> --- a/runtime/doc/tips.txt
> +++ b/runtime/doc/tips.txt
> @@ -431,14 +431,26 @@ comma-separated list of extension(s) you find yourself wanting to edit: >
>
> " vim -b : edit binary using xxd-format!
> augroup Binary
> - au!
> - au BufReadPre *.bin let &bin=1
> - au BufReadPost *.bin if &bin | %!xxd
> - au BufReadPost *.bin set ft=xxd | endif
> - au BufWritePre *.bin if &bin | %!xxd -r
> - au BufWritePre *.bin endif
> - au BufWritePost *.bin if &bin | %!xxd
> - au BufWritePost *.bin set nomod | endif
> + autocmd!
> + autocmd BufReadPre *.bin set binary
> + autocmd BufReadPost *.bin
> + \ if &binary
> + \ | execute "silent %!xxd -c 32"
> + \ | set filetype=xxd
> + \ | redraw
> + \ | endif
> + autocmd BufWritePre *.bin
> + \ if &binary
> + \ | let s:view = winsaveview()
> + \ | execute "silent %!xxd -r -c 32"
> + \ | endif
> + autocmd BufWritePost *.bin
> + \ if &binary
> + \ | execute "silent %!xxd -c 32"
> + \ | set nomodified
> + \ | call winrestview(s:view)
> + \ | redraw
> + \ | endif
> augroup END
>
> ==============================================================================

Thanks, I think using the long command form makes sense.



Best,
Christian
--
angeln:
stundenlang geduldig an einem Fleck lauern, bis man nichts gefangen hat.

Christian Brabandt

unread,
Aug 8, 2023, 2:15:56 PM8/8/23
to vim...@googlegroups.com

On Di, 08 Aug 2023, 'Dragan Simic' via vim_dev wrote:

> diff --git a/runtime/doc/usr_05.txt b/runtime/doc/usr_05.txt
> index bc68e61b8..acc0f0c6b 100644
> --- a/runtime/doc/usr_05.txt
> +++ b/runtime/doc/usr_05.txt
> @@ -308,10 +308,14 @@ This switches on three very clever mechanisms:
>
>
> *restore-cursor* *last-position-jump* >
> - autocmd BufReadPost *
> - \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
> - \ | exe "normal! g`\""
> - \ | endif
> + augroup RestoreCursor
> + autocmd!
> + autocmd BufReadPost *
> + \ let line = line("'\"")
> + \ | if line >= 1 && line <= line("$") && &ft !~# 'commit' && &ft !=# 'xxd'
> + \ | execute "normal! g`\""
> + \ | endif
> + augroup END

How about this version instead? This should make it easier to add more
filetypes to it.

> + \ | if line >= 1 && line <= line("$") && index(['commit', 'xxd'], &ft) = -1
> + \ | execute "normal! g`\""
> + \ | endif


Shall we also change this in runtime/defaults.vim accordingly?


Best,
Christian
--
Es hat in der ganzen Geschichte noch keinen Krieg zwischen zwei Ländern
gegeben, in deren beider Hauptstädte sich McDonald's Filialen befinden.
-- Frank Lüdecke (Zum Krieg in Afghanistan 2002)

Dragan Simic

unread,
Aug 8, 2023, 2:29:33 PM8/8/23
to vim...@vim.org, Christian Brabandt
This looks great, but I'm afraid it won't work as expected. AFAICT, the
"&ft !~# 'commit'" part is there as a negative regexp match to catch the
"gitcommit" file type and any other similar file type names.

> Shall we also change this in runtime/defaults.vim accordingly?

Perhaps it would be a good idea.


> Best,
> Christian
> --
> Es hat in der ganzen Geschichte noch keinen Krieg zwischen zwei Ländern
> gegeben, in deren beider Hauptstädte sich McDonald's Filialen befinden.
> -- Frank Lüdecke (Zum Krieg in Afghanistan 2002)
>
> --

Christian Brabandt

unread,
Aug 8, 2023, 2:55:59 PM8/8/23
to vim...@vim.org
Ah yes that is true. I remember when I added it :)

Actually, I remember I had a similar issue where a certain filetype did
not make sense for jumping to that last position, but do no longer
remember which one :/

> > Shall we also change this in runtime/defaults.vim accordingly?
>
> Perhaps it would be a good idea.

Best
Christian
--
Das wahrhaft größte Geschenk ist es, wenn man sein eigenes
Herz verschenken will.
-- Benjamin Stramke

Dragan Simic

unread,
Aug 9, 2023, 4:46:34 AM8/9/23
to vim...@vim.org
Sure, we can add more file types there later, when you encounter them
again.

>> > Shall we also change this in runtime/defaults.vim accordingly?
>>
>> Perhaps it would be a good idea.

Would you like me to extend the patch and resend it, to cover
runtime/defaults.vim as well?

> Best
> Christian
> --
> Das wahrhaft größte Geschenk ist es, wenn man sein eigenes
> Herz verschenken will.
> -- Benjamin Stramke
>
> --

Christian Brabandt

unread,
Aug 9, 2023, 6:13:26 AM8/9/23
to vim...@googlegroups.com, vim...@vim.org

On Mi, 09 Aug 2023, 'Dragan Simic' via vim_dev wrote:

> Would you like me to extend the patch and resend it, to cover
> runtime/defaults.vim as well?

Yes please. Do you mind to create this as a PR please if possible?

Best,
Christian
--
Wenn Affen Klavier spielen können, warum sollten Menschen nicht dazu
singen.
-- John Lennon

Dragan Simic

unread,
Aug 9, 2023, 7:10:35 AM8/9/23
to vim...@vim.org
On 2023-08-09 12:13, Christian Brabandt wrote:
> On Mi, 09 Aug 2023, 'Dragan Simic' via vim_dev wrote:
>
>> Would you like me to extend the patch and resend it, to cover
>> runtime/defaults.vim as well?
>
> Yes please. Do you mind to create this as a PR please if possible?

Thanks, I'll send an updated patch in an hour or so. I'll actually
resend both patches, as there's a small typo in the commit message of
the other patch, so that's also fixed.

I'm unable to create a PR on GitHub right now, so I'd appreciate if we
can get this merged as patches. If I end up with some more patches in
the future, I'll make sure to submit them as PRs.

Christian Brabandt

unread,
Aug 9, 2023, 7:20:24 AM8/9/23
to vim...@googlegroups.com
Okay thanks.

Best,
Christian
--
Die wahre Liberalität ist Anerkennung.
-- Goethe, Maximen und Reflektionen, Nr. 592

Dragan Simic

unread,
Aug 9, 2023, 8:17:50 AM8/9/23
to vim...@vim.org
Ah, I think I know another file type that shouldn't have the cursor
position restored. :) It's "gitrebase", which is, quite similarly to
"gitcommit", rather unlikely to be the same file as the last one edited.

Are you fine with adding "gitrebase" to the ignored file types list?

>>> > Shall we also change this in runtime/defaults.vim accordingly?
>>>
>>> Perhaps it would be a good idea.
>
> Would you like me to extend the patch and resend it, to cover
> runtime/defaults.vim as well?
>
>> Best
>> Christian
>> --
>> Das wahrhaft größte Geschenk ist es, wenn man sein eigenes
>> Herz verschenken will.
>> -- Benjamin Stramke
>>
>> --
>
> --

Christian Brabandt

unread,
Aug 9, 2023, 10:04:19 AM8/9/23
to vim...@googlegroups.com

On Mi, 09 Aug 2023, 'Dragan Simic' via vim_dev wrote:

> Ah, I think I know another file type that shouldn't have the cursor position
> restored. :) It's "gitrebase", which is, quite similarly to "gitcommit",
> rather unlikely to be the same file as the last one edited.
>
> Are you fine with adding "gitrebase" to the ignored file types list?

yes that is fine. It's not the one I remember, I think it possibly was
some (json) logfile or something. I'll find out, when I am back at work
I guess :)

BTW: you do not have to send the message to vim...@vim.org and
vim...@googlegroups.com Both addresses should be redirected to the same
list.


Best
Christian

Dragan Simic

unread,
Aug 9, 2023, 10:10:31 AM8/9/23
to vim...@vim.org
On 2023-08-09 16:04, Christian Brabandt wrote:
> On Mi, 09 Aug 2023, 'Dragan Simic' via vim_dev wrote:
>
>> Ah, I think I know another file type that shouldn't have the cursor
>> position
>> restored. :) It's "gitrebase", which is, quite similarly to
>> "gitcommit",
>> rather unlikely to be the same file as the last one edited.
>>
>> Are you fine with adding "gitrebase" to the ignored file types list?
>
> yes that is fine. It's not the one I remember, I think it possibly was
> some (json) logfile or something. I'll find out, when I am back at work
> I guess :)

Thanks. Looking forward to knowing what's the mysterious file type. :)

> BTW: you do not have to send the message to vim...@vim.org and
> vim...@googlegroups.com Both addresses should be redirected to the
> same
> list.

I see, and I've actually sent my messages to vim...@vim.org only.
Could it be that something strange is happening with the redirection,
causing the (mis)behavior you're observing?
Reply all
Reply to author
Forward
0 new messages