2017-11-06 16 views
1

신인을 작동하지 않는 행을 대체 vim 스크립트 :는 다음 작업을 수행 매핑을 작성하려고 VimL에

foo 
|----> cursor 
bar 
baz 

lr2jbazfoo을 repalce한다.

" 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>[email protected] 

대신 나는

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

내가 아무 소용이 execute "normal! ..." 명령의 다른 순열을 시도했습니다 얻을. 누구든지 오류를 발견 할 수 있습니까?

normal 명령을 테스트 할 때 모든 것이 제대로 작동하고 'y이라는 표시가 있다는 점에 유의해야합니다.

+0

이 보이는 그 직후에 그것을 제거하십시오. 첫 번째'dd'는 마크를 죽인다. – xaizek

+0

[wiki] (http://vim.wikia.com/wiki/Using_marks)에서'']'는 이전에 변경되었거나 홱 잡아 당겨 진 텍스트의 시작/끝 부분에 _jump입니다. 나는 그것을 모션의 최종 위치를 잡기 위해 사용 해왔다. 그러나 모션의 끝은 "이전에 변경되었거나 텍스트가 잡혀 있지 않습니다"... – yangmillstheory

+0

변경 연산자보다 많은 범위를 저장하므로 사용법이 정확합니다. – xaizek

답변

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>[email protected] 
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>[email protected] 

더 도움말을 참조하십시오 : 당신이 표시처럼

:h :d 
:h :m 
:h :range 
+0

감사. 사실 내 기능에 커다란 결함 (상향 동작을 지원하지 않음)이 있다는 것을 깨달았습니다. – yangmillstheory