I have the following txt file that was edited reshuffled etc. So the numbers are not correct anymore.
#1. Sometext <andmore>/
/mm
#2. Next block/
1. option x/
3. option s/
#. character <description>/
.option e/
.option d/
...
#104. last entry/
1. option x/
2. option p/
3. option s/
1. option e/
2. option d/
How in vim can I renumber the #[number]. and the internal block numbers [1]. even if they are empty. So that I get:
#1. Sometext <andmore>/
/mm
#2. Next block/
1. option x/
2. option s/
#3. character <description>/
1. option e/
2. option d/
...
#105. last entry/
1. option x/
2. option p/
3. option s/
4. option e/
5. option d/
I had a old vim function that was supposed to recount the "#[number]." parts ...
fun CountUp()
let ret = g:i
let g:i = g:i + 1
return ret
endf
that should be called by
:let i = 1 | %s:sS:\="#" . CountUp() . ". ":g
... but this isn´t even starting. Has something to do with the "sS" which I do not understand.
That sS
is a pattern, literally the string sS
, that is obviously not found in your buffer. There is no reason whatsoever to expect it to do anything in this specific context, let alone anything useful.
You should start by crafting a pattern that is actually relevant to the use case, something like:
^\s*\zs#\d\+\.
See :help \zs
.
Note that this will work globally for the #N
headers, but not for the sub-items. Not because of the pattern (which could be easily modified to handle both), but because:
CountUp()
function nor the whole command handle recursiveness,#N
case anyway.You can do the following, though, once for each block of sub-items:
Visually select the sub-items.
Run the following command:
:let i = 1 | '<,'>s/^\s*\zs\d\+/\=CountUp() . "."
Note that your "starting point" has some serious formatting issues as well, and there is even one block with missing numbers, which will complicate things further.
In conclusion, the whole thing is actually relatively complex and needs a non-trivial amount of work to handle more and more corner case. This will need more than a one-liner.