Ernie Rael
unread,Mar 29, 2022, 9:27:51 PM3/29/22Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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')