modifying a few characters in a buffer (sanity check)

12 views
Skip to first unread message

Ernie Rael

unread,
Mar 29, 2022, 9:27:51 PM3/29/22
to vim...@googlegroups.com
I've done little vim9script programing (and no vimscript).

I want to replace a part of a buffer line. I thought to do "getline()",
slicing, "setline()", no joy; I couldn't see any way to manipulate the
string directly.Anything better I couldn't find? I'm still getting used
to the synthesis of vim9script and vim commands

In the following, F1() seems expensive, F2() feels uncomfortable (for
example 'x' command might go to the next line). For performance is F2()
better (assuming there's some checking).

Any comments/suggestions appreciated

-ernie

vim9script
# This line gets modified
# This line gets modified

def F1(lino: number, pos1: number, pos2: number, newtext: string)
    var t1 = getline(lino)->str2list()
    t1->remove(pos1, pos2)
    t1->extend(str2list(newtext), pos1)->list2str()->setline(lino)

enddef

def F2(lino: number, pos1: number, pos2: number, newtext: string)
    cursor(lino, pos1 + 1)
    execute 'normal ' .. (pos2 - pos1 + 1) .. 'x'
    execute 'normal i' .. newtext
enddef

F1(2, 12, 15, 'BOO')
F2(3, 12, 15, 'FOO')


Ernie Rael

unread,
Mar 29, 2022, 9:34:23 PM3/29/22
to vim...@googlegroups.com
IGNORE. I got stuck trying to do an assignment: "a[ : ] = xx". slicing
works great.
-ernie

bfrg

unread,
Mar 30, 2022, 5:45:54 PM3/30/22
to vim...@googlegroups.com
This has already been requested in the past, see
https://github.com/vim/vim/issues/5632.
> --
> --
> 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/773e9647-93d1-b339-18f6-783e3316e484%40raelity.com.
Reply all
Reply to author
Forward
0 new messages