Vim:如何索引纯文本文件?
是否可以在vim中索引纯文本文件(一本书),例如:Vim:如何索引纯文本文件?,vim,ctags,Vim,Ctags,是否可以在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-Yo
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
如果可能的话,标记方法是什么?
我读过关于CTAG的书,但它似乎专门用于特定的语言(说实话,对我的需求来说有点过分了)也许可以看看ptx
:%!cut -d: -f2 | ptx -Ar
未修改时,将输出如下内容:
: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
我将查看是否可以完成其余步骤我根据使用
: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
因此,您可以对所有希望使用的单词调用此函数(或编写脚本),并将输出定向到文件。也许不是很优雅,但很好地自给自足
希望这对你有所帮助,而不是阻碍。这是一个修正版的函数,由Prince Goulash发布。此版本将单词列表作为输入,并返回结果的格式化和字母顺序字符串:
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])
有一个长的单词列表应该没有问题,尽管在这种情况下,您可能希望使用一个列表变量,获得结果当然需要更多的时间。例如:
let my_word_list = ['word1', 'word2', . . . 'word1000']
echo IndexByWord(my_word_list)
非常好,它工作得很好,但只针对一个单词(例如:call IndexByWord(“柏林”)。我必须如何让它针对一系列单词(例如:call IndexByWord(“柏林”、“伦敦”、“纽约”、“巴黎”)工作?那么这个数组的大小限制是多少?@ThG:我冒昧地添加了一个单独的帖子来修改G王子的函数,以满足您的要求。(当然,G王子的帖子应该是您的“答案”;我的修改很小。)它成功了!非常感谢你和Prince Goulash。顺便说一句,我可以为一个文本创建许多索引:一个用于名称(IndexByNames),一个用于主题(IndexByTopics),等等…将它们捆绑在脚本中,并将它们附加到文本末尾…感谢阿加因汉克斯进行了这些修改-我很高兴你能够遵循我的代码!和@Prince Goulash:我似乎在吹嘘我的能力:我如何在索引的文件末尾重定向IndexByWord的结果?(就像在一本真正的书中,有一个真正的索引)。提前谢谢