Vim:如何索引纯文本文件? [英] Vim : how to index a plain text file?
问题描述
是否可以在vim中索引纯文本文件(一本书),例如:
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
并得到这个结果索引:
Berlin : 1
London : 1, 2
New-York : 2, ..., 100
Paris : 1, ..., 100
而且,如果可能的话,标记方法是什么?我读过关于 ctags 的文章,但它似乎专用于特定语言(说实话,对于我的需求来说有点矫枉过正)
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)
推荐答案
这里是 Goulash 王子发布的函数的修订版.此版本将单词列表作为输入并返回结果的格式化和字母顺序字符串:
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
一种调用方式是:
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)
这篇关于Vim:如何索引纯文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!