I would like to turn a list like this
these are
custom regex expressions 4
syntax? syntax
weird one 3
there are many many many many words in this line 3
into this
these are
custom {4}regex {4}expressions
syntax? syntax
weird {3}one
there {3}are {3}many {3}many {3}many {3}many {3}words {3}in {3}this {3}line
I know how to get rid of the trailing number ending
:%s/ \d\+$//
I know how to replace spaces
:s/ / {3}/g
but how do I combine these two? The second should only apply to lines that actually ended in a number, and use that number.
In my head this problem formed a "substitution in a substitution" thing, like this:
:%s/\(.\+\) \(\d\+\)$/\1.replace(' ', ' {\2}')/
But there is likely a better, VIM, way to solve this. Maybe my conceptualised solution is not supported.
Please how can I perform such operations?
:g/\d$/s/ /\=' {' . matchstr(getline('.'), '\d\+$') . '}'/g
This use a:g
command to match only lines ending with w number and applies a substitution command using an expression composed of' {
i.e the space and the first bracket concatenated withmatchstr(getline('.'), '\d\+$')
which gets the number at the end of the line. You'll probably need to clean up the remaining trailing digits with:%s/\d\+$//
:%s/\(.\+\) \(\d\+\)$/\=substitute(submatch(1), ' ', ' {' . submatch(2) . '}', 'g')/