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 : New-York, London, Berlin
...
100. And, to conclude, my last comments about : New-York, Paris
and have this resulting index :
Berlin : 1
London : 1, 2
New-York : 2, ..., 100
Paris : 1, ..., 100
and, if it is possible, what is the tagging method ? I have read about ctags, but it seems to be dedicated to开发者_开发百科 specific languages (and to say the truth, a bit overkill for my needs)
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.
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)
Have a look at ptx, perhaps
:%!cut -d: -f2 | ptx -Ar
Will output something like this, when unmodified:
:1: London, Berlin, Paris
:2: New-York, London, Berlin
:1: London, Berlin, Paris
:2: New-York, London, Berlin
:2: New-York, London, Berlin
:4: New-York, Paris
:1: London, Berlin, Paris
:4: New-York, Paris
:2: New- York, London, Berlin
:4: New- York, Paris
I'll see if I can the rest of the steps too
精彩评论