Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim : how to index a plain text file?

Tags:

vim

ctags

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)

like image 216
ThG Avatar asked Oct 11 '22 04:10

ThG


1 Answers

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.

like image 65
Prince Goulash Avatar answered Oct 16 '22 11:10

Prince Goulash