Vim:如何索引纯文本文件?

发布于 2024-11-05 09:29:56 字数 432 浏览 5 评论 0原文

是否可以在 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 的内容,但它似乎专用于特定语言(说实话,对于我的需求来说有点矫枉过正)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一生独一 2024-11-12 09:29:56

我冒昧地编写了以下函数,基于使用 :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

因此,您可以对您想要的所有单词调用此函数(或编写一个脚本来执行此操作)并将输出定向到一个文件。也许不是很优雅,但非常独立。

希望这会有所帮助,而不是阻碍。

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.

暖心男生 2024-11-12 09:29:56

这是由 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)

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

One way to call it would be:

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)
赠佳期 2024-11-12 09:29:56

看看 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

我会看看是否也可以完成其余的步骤

Have a look at ptx, perhaps

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

Will output something like this, 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'll see if I can the rest of the steps too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文