Reputation: 1065
Newcomer to VimL trying to write a mapping that does the following:
foo
|----> cursor
bar
baz
lr2j
should repalce foo
with baz
.
" replace the current line with a line given by a linewise motion
function! s:LineReplace(type)
if a:type !=# 'line'
return
endif
let saved_register = @@
silent execute "normal! S\<esc>my`]dd'yPjddk^ :delmarks y"
let @@ = saved_register
endfunction
nnoremap lr :set operatorfunc=<SID>LineReplace<cr>g@
Instead I get
Error detected while processing function <SNR>108_LineReplace: line 5: E20: Mark not set
I've tried different permutations of execute "normal! ..."
command to no avail. Can anyone spot the error?
I should note that when I test out the normal
commands everything works fine and the mark 'y
exists.
Upvotes: 1
Views: 200
Reputation: 45117
Use :move
and :delete
to simply things:
" replace the current line with a line given by a linewise motion
function! s:LineReplace(type)
if a:type !=# 'line'
return
endif
']move '[
-delete_
endfunction
nnoremap lr :set operatorfunc=<SID>LineReplace<cr>g@
For more help see:
:h :d
:h :m
:h :range
Upvotes: 2
Reputation: 1065
@xaizek is right; the correct way is to store the mark from the motion:
" replace the current line with a line given by a linewise motion
function! s:LineReplace(type)
if a:type !=# 'line'
return
endif
let lnum = getpos("']")[1]
let saved_register = @@
silent execute "normal S\<esc>my" . lnum . "Gdd'yPjddk^ :delmarks y"
let @@ = saved_register
endfunction
nnoremap lr :set operatorfunc=<SID>LineReplace<cr>g@
Upvotes: 0