Vim : how to index a plain text file?

前端 未结 3 1116
孤街浪徒
孤街浪徒 2020-12-18 06:00

Is it possible to index a plain text file (a book) in vim such as :

1. This line contains the words : London, Berlin, Paris
2. In this line, I write about :          


        
3条回答
  •  感情败类
    2020-12-18 06:37

    I took the liberty of writing the following function, based on using the :g/STRING/# command to get the matches. I read the results of this command into a list, and then process it to return a list of matching line numbers:

    function! IndexByWord( this_word )
        redir => result
        sil! exe ':g/' . a:this_word . '/#'
        redir END
        let tmp_list = split(strtrans(result),"\\^\@ *")
        let res_list = []
        call map(tmp_list, 'add(res_list,matchstr(v:val,"^[0-9]*"))')
        let res = a:this_word . ' : ' . string(res_list)
        let res = substitute(res, "[\\[\\]\\']", "", "g")
        echo res
    endfunction
    

    So you could call this function on all the words you wish (or write a script to do so) and direct the output to a file. Not very elegant, perhaps, but nicely self-contained.

    Hope this helps, rather than hinders.

提交回复
热议问题