Sunday, June 28, 2020

Re: An interesting little poser

I've made the following macro that works for the given example, assuming
1 line of footnote.

Run the following (copy and paste)

:map <F2> G?\[\d\+\]<CR>va[y/<C-r>0<CR>dd0DnF[hr^llPdt]

Press F2 to run this macro. You can also hold the key
and will get a message when it's done.

Explanation:
G - to to the bottom of the file
? - start reverse search (search will look up)
\[\d\+\] - regex - nonzero digits inside square brackets
<CR> - enter to finish entering regex
va[ - select square brackets and digits inside
y - yank them
/ - search (this one will wrap)
<C-r>0 - insert content of register 0, which holds last yank
allowing us to search for the [digits] pattern
<CR> - enter to finish entering search
dd - delete line with [digits]; we are now on footnote line
0 - go to the beginning of the line
D - delete to the end of line, without newline, so when we paste it
won't be with newline
n - go to the the place where the footnote is referenced
F[ - search backward for [
h - 1 left
r^ - replace space with ^
I've noticed the space is removed in example.
ll - go to the first digit, skipping ^[
P - paste footnote before the digits
dt] - delete the digits, without ]

You can enter this manually and to see what's happening.
I think it's much easier to analyze and understand than a function with
weird regexes, and it shows you how the result looks on one-by-one basis.
You might need to manually clean some whitespace at the end after it's finished.

Regards,
mat

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200628213704.fxuqlpazd4xbb3cw%40debian.

No comments: