Question

Say I have a text file with footnotes formatted like so:

Some text about cats[^1].
Some text about fish.
Some more text, this time about dogs[^2].
Something about rats[^3] and something else about birds[^4].


[^1]: Footnote about cats
[^2]: Footnote about dogs
[^3]: Footnote about rats
[^4]: Footnote about birds

What would be the most efficient way of incrementing the numbers if I wanted to add a new footnote for "fish". In other words, what would be the most efficient way to get to this:

Some text about cats[^1].
Some text about fish[^2].
Some more text, this time about dogs[^3].
Something about rats[^4] and something else about birds[^5].


[^1]: Footnote about cats
[^2]: Footnote about fish
[^3]: Footnote about dogs
[^4]: Footnote about rats
[^5]: Footnote about birds

I am looking for a solution that I can apply to a very large text file with perhaps hundreds of footnotes.

I have only been able to do this manually so far and am trying to find how I can make the most of vim to achieve something like this.

Was it helpful?

Solution

It is possible to increment all refs in selected region with this command:

:'<,'>sno/[^\(\d\+\)]/\='[^'.(submatch(1)+1).']'/g

Where sno (:help :snomagic) allows to drop backslashes before brackets.

  1. Add reference.
  2. Select all the text after reference (excluding footnotes). You can set a mark just before footnotes to do this easily.
  3. Increment refs in selection.
  4. Add footnote.
  5. Select all footnotes below.
  6. Increment refs in selection.

screencast

OTHER TIPS

I'm assuming you're using pandoc, so one option is to use inline footnotes or named footnotes as stated in the pandoc wiki.

One way you could accomplish this in vim is with a macro. For instance:

Put in the new footnote...

/fish<cr>ea[^2]<esc>

Create a new macro to increment the remaining footnotes (assuming you know how many there are).

qq/\[^\d\]<cr><c-a>q2@q

Update the footnotes...

nYp<c-a>fccfish<esc>3@q

Add the footnote using a temporary footnote reference, e.g.,

Some text about cats[^1].
Some text about fish[^fish].
Some more text, this time about dogs[^2].
Something about rats[^3] and something else about birds[^4].

[^fish]: Footnote about fish
[^1]: Footnote about cats
[^2]: Footnote about dogs
[^3]: Footnote about rats
[^4]: Footnote about birds

Then pipe the whole thing through pandoc: :%!pandoc -t markdown --standalone, which will, among other things, nicely renumber all the footnote references, and put all the footnotes in order at the end of the document.

I don't have a great suggestion for incrementing your numbers, but I have a pretty nice workflow for Markdown footnotes and vim.

Basically, I use my fork of vim-markdownfootnotes to enter footnotes as easily and quickly as possible. If the order of the footnote numbers gets messed up because of my habit of inserting footnotes to early parts of the text, I run my sort-markdown-footnotes script and everything is back in order.

Works like a charm.

I did not test it with hundreds of footnotes though...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top