本文介绍了Vim:向当前文件名添加自动字母和/或数字索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现 Niklas Luhmann 的松散版本Zettelkasten在 Vim 中.他的方法的核心是继续当前的笔记片段注意或 Brahch 关闭它,引入一个稍微不同的主题或概念.在音符名称中,字母表示分支和数字表示延续.像这样:

I want to implement a loose version of Niklas Luhmann'sZettelkastenin Vim. At the core of his method are note snippets that Continue the currentnote or Brahch off from it, introducing a slightly different topic orconcept. In the note name, letters indicate branches and numeralsindicate continuations. Like so:

note100
note101
    note101a        # branches off from note100 (related topic)
    note101b        # also branches off from note100 (related topic)
        note101b01  # continues note101b (same topic)
        note101b02  # also continues note101b (same topic)
    note101c
note102

要在 Vim 中实现这一点,我需要新文件自动枚举为延续"或当前缓冲区中音符的分支".作为在 Vimscript 中进行第一个真正"步骤的非编码人员,这就是我使用分支注释功能的地方:

To implement this in Vim, I need new filenames that are automatically enumerated either as a "continuation" ora "branch" of the note in current buffer. As a non-coder making first "real" steps in Vimscript, this is where I'm at with the Branching Note function:

function! ZettelkastenNewBranchingNote()
    let b:current_note_name = expand('%:t:r')
    let b:new_branching_note = call(BranchingFunctionThatReturnsNewNoteName)
    silent execute 'edit' b:new_branching_note
    echomsg 'New branching note ' b:new_branching_note 'created.'
endfunction

BranchingFunctionThatReturnsNewNoteName() 应该采用b:current_note_name 并使用自动字母(!)索引(按字母顺序向上计数).我怎么能做到这一点?

The BranchingFunctionThatReturnsNewNoteName() should takeb:current_note_name and extend it with automatic alphabetical(!)index (counting alphabetically upwards). How could I accomplish this?

另外,对于我的新续注功能:我怎么能用数字表示从当前文件名的最后一个数字部分向上计数?(例如 100a01 > 100a02.)

Also, for my New Continued Note function: how could I numericallycount upwards from the last numeric part of the current file name? (E.g. 100a01 > 100a02.)

感谢您的建议!

(有点相关,此处建议使用 Nexus 插件,但我更愿意保留我的脚本自包含.)

(Somewhat relatedly, herethe Nexus plugin is suggested, but I'd prefer to keep my scriptself-contained.)

推荐答案

您提供了大量上下文(这很好),但对所需算法的了解却很少.对我来说,它看起来像这样:如果当前文件以字母结尾,则增加它,否则(它是一个数字),附加一个 a 以开始字母顺序.

You provide a great deal of context (which is great), but are light on the needed algorithm. To me, it looks like this: If the current file ends with a letter, increase it, else (it's a number), append an a to start the alphabetical sequence.

使用正则表达式在 Vim 中完成检查;\a[A-Za-z] 的简写形式(你也可以写 [[:alpha:]];是的,它是那个灵活),并且 $ 锚定到名字的末尾:

Checks are done in Vim with regular expressions; \a is a short form for [A-Za-z] (you could also write [[:alpha:]]; yes it's that flexible), and $ anchors it to the end of the name:

if b:current_note_name =~ '\a$'
    ...

matchstr()提取最后一个字符.

    let lastAlpha = matchstr(b:current_note_name, '\a$')
    if lastAlpha ==? 'z'
        " TODO: Handle overflow
    endif

要增加"一个字母字符,请先将其转换为数字,然后再增加:

To "increase" an alphabetical character, convert it first to a number, increase, then back:

    let newAlpha = nr2char(char2nr(lastAlpha) + 1)

要替换,请使用 substitute(),再次使用相同的正则表达式.

To replace, you use substitute(), again with the same regexp.

    let b:new_branching_note = substitute(b:current_note_name, '\a$', newAlpha, '')

追加很简单:

else
    let b:new_branching_note = b:current_note_name . 'a'
endif

这篇关于Vim:向当前文件名添加自动字母和/或数字索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:33