Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

ctags - Vim : how to index a plain text file?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...