0

What is the best way in Vim to handle abbreviations for a paper? For example:

When working with Artificial Intelligence (AI) there must be .... and furthermore AI enables you to....

And later in the paper exists an appendix:

List of Abbreviations

....
AI - Artificial Intelligence
DoD - Department of Defense
....

The goal would be to write in Vim something simply like:

When working with \acronym{AI} there must be .... and furthermore \acronym{AI} enables you to

And it would correctly define the acronym on first usage and generate the list of acronyms / abbreviations when called, much like a bibliography. Is there a way to accomplish this?

7
  • Can you clarify a bit the expected behavior? ` it would correctly define the Acronym on first usage and generate the list of acronyms when called` So you want to write the list of abbreviations manually, when using the abbreviation you want to have the complete sentence inserted on the first use and then you want to keep just the short form, right? What I don't understand is the the second part where it generate the list of acronyms when called: Isn't the list supposed to be written before you try to use an abbreviation? What is it supposed to generate?
    – statox
    Commented Mar 15, 2022 at 15:22
  • @statox Yep! Exactly right on the first part. It's annoying when adding an acronym early in the paper to check if it was used before. The second part is a nice to have. Many reports, especially those that heavily use abbreviations, will have an appendix (normally after, sometimes before). This appendix defines all the acronyms used in the paper so that you don't have to search for the first usage. For example in this publication jcs.mil/Portals/36/Documents/Doctrine/pubs/jp3_0ch1.pdf on page GL-1 or 205 of the pdf you will see a list.
    – Diesel
    Commented Mar 15, 2022 at 15:38
  • 1
    I understand how such a list would be useful but I don't understand how your code would generate it: If you want your snippets to be able to replace an abbreviation by its full version it means that you need to already have this list, right? Then how would your code generate such a list since it doesn't have the data ?
    – statox
    Commented Mar 15, 2022 at 15:51
  • As for the actual solution maybe you can choose one snippet engine plugin, have a script which would parse your existing list of definitions and replace your text. Making sure the long form is used only on the first occurrence might not be trivial but doable.
    – statox
    Commented Mar 15, 2022 at 15:53
  • 1
    …Use LaTeX? Which has a package that does this well? (I have \usepackage[nopostdot,toc,acronym,nomain,nonumberlist]{glossaries}, {\makeglossaries}, \loadglsentries[acronym]{glossary} in my thesis, where glossary.tex uses \newacronym to define them and I refer to them with, e.g., \gls{acronym})
    – D. Ben Knoble
    Commented Mar 15, 2022 at 21:26

1 Answer 1

1

Indeed, you will need some vimscript code to get what you want.

I wrote this small snippet which does what you are looking for (I think):

function FindAcronyms()
    let view = winsaveview()
    call cursor(1, 1)

    while search('(\zs[A-Z]\+\ze)', 'W')
        " Getting the accronym and its definition
        let line_content = getline('.')
        let accronym = matchstr(line_content[col('.')-1:], '^[A-Z]\+\ze)')
        let number_of_words = len(accronym)
        let definition = matchstr(line_content[:col('.')-2], '\(\w\+\s*\)\{'.number_of_words.'\}\ze($')

        " Verifying that the accronym corresponds to the definition
        let cnt = 0
        let valid = 1
        let words_array = split(definition)
        while cnt < number_of_words
            if tolower(words_array[cnt][0]) != tolower(accronym[cnt])
                let valid = 0
            endif
            let cnt = cnt + 1
        endwhile


        if !valid | continue | endif

        " Verifying that the accronym is not yet present in the document
        if search('^\s*' . accronym . '\s*:', 'n') | continue | endif

        " Adding the definition at the end of the document
        call append(line('$'), accronym . ': ' . definition)
    endwhile

    call winrestview(view)
endfunction

When calling this function, all of the acronyms of the form "[...] here is Some Definition (SD) [...]" will be added to the end of the document

1
  • If anyone else sees this I was simply looking for the glossaries package as @D.BenKnoble pointed out. I didn't know that was a thing.
    – Diesel
    Commented Mar 24, 2022 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.