是否可以在 vim 中索引纯文本文件(一本书),例如:
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
并有这个结果索引:
Berlin : 1
London : 1, 2
New-York : 2, ..., 100
Paris : 1, ..., 100
并且,如果可能的话,标记方法是什么?
我已经读过关于 ctags 的文章,但它似乎是专门针对特定语言的(说实话,对于我的需求来说有点矫枉过正)
最佳答案
我冒昧地编写了以下函数,基于使用 :g/STRING/#
命令来获取匹配项。我将这个命令的结果读入一个列表,然后对其进行处理以返回匹配行号的列表:
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
所以你可以在所有你想要的词上调用这个函数(或编写一个脚本来这样做)并将输出定向到一个文件。也许不是很优雅,但很好地独立。
希望这会有所帮助,而不是阻碍。
关于 Vim : how to index a plain text file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5869997/