vimscript替换线无法正常工作

时间:2017-11-06 21:53:17

标签: vim

VimL的新手尝试编写执行以下操作的映射:

foo
|----> cursor
bar
baz

lr2j应使用foo重新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@

相反,我得到

Error detected while processing function <SNR>108_LineReplace: line 5: E20: Mark not set

我尝试过execute "normal! ..."命令的不同排列无效。有人能发现错误吗?

我应该注意,当我测试normal命令时,一切正常并且标记'y存在。

2 个答案:

答案 0 :(得分:2)

:move:delete简单地用于:

" 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@

如需更多帮助,请参阅:

:h :d
:h :m
:h :range

答案 1 :(得分:0)

@xaizek是对的;正确的方法是存储动作中的标记:

" 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@
相关问题