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 :
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)