Vim: how to index a plain text file?

Is it possible to index a simple text file (book) in vim, for example:

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 get this result:

 Berlin : 1 London : 1, 2 New-York : 2, ..., 100 Paris : 1, ..., 100 

and, if possible, what is the label method? I read about ctags, but it seems to be dedicated to specific languages ​​(and, frankly, outwitted a bit for my needs)

+4
vim ctags
source share
3 answers

I took the liberty of writing the following function based on the use of the command :g/STRING/# to get matches. I read the results of this command in a list and then process it to return a list of the corresponding 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 

That way, you can call this function on all the words you want (or write a script to do this) and direct the output to a file. Not very elegant, perhaps, but perfectly self-sufficient.

Hope this helps, not interfere.

+2
source share

Here is a revised version of the feature published by Prince Gulash. This version takes a list of words as input and returns a formatted and alphabetical string of 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 

One way to invoke this:

 echo IndexByWord(['word1', 'word2', 'word3', etc]) 

There should not be a problem with a long list of words, although in this case you probably want to use a list variable, and getting the results, of course, takes longer. For example:

 let my_word_list = ['word1', 'word2', . . . 'word1000'] echo IndexByWord(my_word_list) 
+2
source share

Look at ptx, maybe

 :%!cut -d: -f2 | ptx -Ar 

Something like this will be output when unmodified:

 :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 

I will also see if I can take the rest of the steps

0
source share

All Articles