Setting buffer contents by byte offset

26 views
Skip to first unread message

Paul Jolly

unread,
Jul 11, 2019, 3:55:47 AM7/11/19
to Vim Dev Mailing List
Hi all,

I've tried various permutations of search terms, but so far turned up a blank.

Is there a function/similar that will allow me to set buffer contents
between a start and end byte offset?

If there isn't, I have a "backup" in the form of a :substitute like:

%s/\%3l\%1c\_.*\%4l\%1c/hello\r/g

(because I'm also deriving the line and col whilst deriving the byte
offset) but I suspect the byte-offset based approach will be
considerable more efficient if available.

Thanks,


Paul

Tony Mechelynck

unread,
Jul 11, 2019, 4:32:30 AM7/11/19
to vim_dev
see
:help go
:help v_c

You could "go" to your starting point, set Visual mode, go to your
ending point, and then "c"hange or "s"ubstitute.

Best regards,
Tony.

Paul Jolly

unread,
Jul 11, 2019, 4:45:30 AM7/11/19
to Vim Dev Mailing List
Thanks, Tony.

Your response has helped me understand that my substitute solution
will not work here either.

Reason that neither solution will work is that it requires moving the
cursor, and that's not at all desirable in this case.

Effectively what I'm after here is a byte-offset based equivalents of
bufsetline(), appendbufline() and deletebufline().


Paul
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/CAJkCKXu6%2BMi7Bs3CaHrkiz4ejcZef96Gq18XJcvdgMa%2BGEk0zQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Ingo Karkat

unread,
Jul 11, 2019, 7:00:39 AM7/11/19
to vim...@googlegroups.com, Paul Jolly
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 11-Jul-2019 10:45 +0200, Paul Jolly wrote:

> Thanks, Tony.
>
> Your response has helped me understand that my substitute solution
> will not work here either.
>
> Reason that neither solution will work is that it requires moving
> the cursor, and that's not at all desirable in this case.

I would not generally rule out cursor moves; within a function, Vim
doesn't update the screen, and you can save and restore the original
view via:

let l:save_view = winsaveview()
" Your moves here
call winrestview(l:save_view)

Many plugins use this.

> Effectively what I'm after here is a byte-offset based equivalents
> of bufsetline(), appendbufline() and deletebufline().

Given that Vim is a text editor, and text is organized in lines, I
don't think there's enough demand for such variants, especially since
workarounds should exist (see above).

- -- regards, ingo

> On Thu, 11 Jul 2019 at 09:32, Tony Mechelynck
> <antoine.m...@gmail.com> wrote:
>>
>> On Thu, Jul 11, 2019 at 9:55 AM Paul Jolly <pa...@myitcv.io>
>> wrote:
>>>
>>> Hi all,
>>>
>>> I've tried various permutations of search terms, but so far
>>> turned up a blank.
>>>
>>> Is there a function/similar that will allow me to set buffer
>>> contents between a start and end byte offset?
>>>
>>> If there isn't, I have a "backup" in the form of a :substitute
>>> like:
>>>
>>> %s/\%3l\%1c\_.*\%4l\%1c/hello\r/g
>>>
>>> (because I'm also deriving the line and col whilst deriving the
>>> byte offset) but I suspect the byte-offset based approach will
>>> be considerable more efficient if available.
>>
>> see :help go :help v_c
>>
>> You could "go" to your starting point, set Visual mode, go to
>> your ending point, and then "c"hange or "s"ubstitute.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJdJxbFAAoJEA7ziXlAzQ/vg8gH/RkwflvA7kH7gXkteAsqVrez
Ar7o0i3NGHJCpIwuStII5fg3s+IGqDrbNsZYtSs9W1srzdC6TnkMuLza3cbeVoh0
SE/+oQyhTaWdcblDgfb/xAgRwW2nYGw8ag6OSIkEAkjgnW38pr7Tk4q3M1eRjdra
d1pVj0Fhd9OocRq/gq03NCE7ob19y1cCBMDvBXiu0BXT5b8G5M5AWZ6xsRt83mMv
NTFO9Rq9LwrvqHJk9u0FhFitXhe5n+FR1N8Ckg8YSS16WloiCIyMm3BLTcbxcYYG
rMcELFjOopUsziG+g09nEFf0Y/A/pl+oTqC2HbeqREb8Dfbw4yzUMuGEzkwCo+k=
=SrdA
-----END PGP SIGNATURE-----

Paul Jolly

unread,
Jul 11, 2019, 7:10:15 AM7/11/19
to Ingo Karkat, Vim Dev Mailing List
> > Reason that neither solution will work is that it requires moving
> > the cursor, and that's not at all desirable in this case.
>
> I would not generally rule out cursor moves; within a function, Vim
> doesn't update the screen, and you can save and restore the original
> view via:
>
> let l:save_view = winsaveview()
> " Your moves here
> call winrestview(l:save_view)
>
> Many plugins use this.

Thank you, that's a good point.

I'll give this a shot and report back with any issues.
Reply all
Reply to author
Forward
0 new messages