Is there a neat way to reverse the string inside a certain type of brackets in vim, for instance
{1,2,3,4,5}
=>
{5,4,3,2,1}
?
Source: Reverse all lines and Power of g at vim wikia. -r The -r option causes the input to be displayed in reverse order, by line. Check: man tar for more details. Show activity on this post.
Sorting text in Vim is easy! Select the text, then press :, type sort, then hit enter! It'll sort the whole document by default, but you can enter a range too. Explanation on how to sort text in vim:
Hence, you first move the first line to the top, then the second above the first, then the third above the second, etc. If you do not want to reverse the entire file, you can supply the range of lines to reverse to the command and adjust the destination accordingly.
With minor tweaking, using an actual pattern for the :g command and a range on the :m command (which could contain searches, etc.) this simple command becomes extremely versatile for moving large blocks of text and placing in reverse order. -- Fritzophrenic 14:32, January 8, 2010 (UTC)
How about this command?
:%s/{\zs\\(.\\{-}\\)\ze}/\= join(reverse(split(submatch(0), '\zs')), '')/g
The idea is to utilize the append
feature of register by using upper-case register name.
To achieve this, you have to do some preparations:
"a
register: :let @a=""
{,1,2,3,4,5}
}
in normal modeOk, here we go:
"AdF,
, and press .
to repeat until all elements are deleted."aP
to paste the reversed sequence, you will get: {,5,4,3,2,1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With