2012-12-02 1 views
2

두 개의 텍스트 파일이 있습니다. 현재 파일이 하나이고 현재 다른 파일에 행 번호 목록이 있습니다. 내가 뭘하고 싶은 줄 번호가 후자와 일치하는 첫 번째 파일의 라인을 강조하는 것입니다.vim 외부 파일의 행 번호를 사용하여 행을 강조 표시합니다.

예컨대 :

을 File1 :

I like eggs 
I like meat 
I don't like eggplant 
My mom likes chocolate 
I like chocolate too 

있는 File2 :이 예에서

2 
4 

그 라인을 강조해야합니다

I like meat 
My mom likes chocolate 

감사!

답변

4

readfile()을 사용하면 줄 번호를 읽고 해당 줄 번호와 일치하는 정규식 (예 : \%42l)으로 변환 할 수 있습니다. 강조 표시는 :match 또는 matchadd()을 통해 수행 할 수 있습니다.

다음은이 모든 사용자 정의 :MatchLinesFromFile 명령에 응축입니다 :

":MatchLinesFromFile {file} 
"   Read line numbers from {file} and highlight all those 
"   lines in the current window. 
":MatchLinesFromFile Remove the highlighting of line numbers. 
" 
function! s:MatchLinesFromFile(filespec) 
    if exists('w:matchLinesId') 
     silent! call matchdelete(w:matchLinesId) 
     unlet w:matchLinesId 
    endif 
    if empty(a:filespec) 
     return 
    endif 

    try 
     let l:lnums = 
     \ filter(
     \ map(
     \  readfile(a:filespec), 
     \  'matchstr(v:val, "\\d\\+")' 
     \ ), 
     \ '! empty(v:val)' 
     \) 

     let l:pattern = join(
     \ map(l:lnums, '"\\%" . v:val . "l"'), 
     \ '\|') 

     let w:matchLinesId = matchadd('MatchLines', l:pattern) 
    catch /^Vim\%((\a\+)\)\=:E/ 
     " v:exception contains what is normally in v:errmsg, but with extra 
     " exception source info prepended, which we cut away. 
     let v:errmsg = substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', '') 
     echohl ErrorMsg 
     echomsg v:errmsg 
     echohl None 
    endtry 
endfunction 
command! -bar -nargs=? -complete=file MatchLinesFromFile call <SID>MatchLinesFromFile(<q-args>) 

highlight def link MatchLines Search 
+0

매우 nive.Thanks! – user1871021

관련 문제