Vim : how to index a plain text file?

前端 未结 3 1121
孤街浪徒
孤街浪徒 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:24

    Here is a revised version of the function posted by Prince Goulash. This version takes a list of words as input and returns a formatted and alphabetized string of the result:

    function! IndexByWord( wordlist )
        let temp_dict = {}
        for word in a:wordlist
            redir => result
            sil! exe ':g/' . word . '/#'
            redir END
            let tmp_list = split(strtrans(result),"\\^\@ *")
            let res_list = []
            call map(tmp_list, 'add(res_list,str2nr(matchstr(v:val,"^[0-9]*")))')
            let temp_dict[word]  = res_list
        endfor
        let result_list = []
        for key in sort(keys(temp_dict))
            call add(result_list, key . ' : ' . string(temp_dict[key])[1:-2])
        endfor
        return join(result_list, "\n")
    endfunction
    

    One way to call it would be:

    echo IndexByWord(['word1', 'word2', 'word3', etc])
    

    There should be no problem with having a long list of words, although in that case you would probably want to use a list variable and getting the results would of course take more time. For example:

    let my_word_list = ['word1', 'word2', . . . 'word1000']
    echo IndexByWord(my_word_list)
    

提交回复
热议问题