Reverse string order inside brackets in vim

Is there an optional way to change the string inside a specific type of brackets in vim, for example

{1,2,3,4,5} => {5,4,3,2,1} 

?

+5
source share
3 answers

How about this team?

 :%s/{\zs\\(.\\{-}\\)\ze}/\= join(reverse(split(submatch(0), '\zs')), '')/g 
+7
source

The idea is to use the append function to register using the uppercase case name.

To achieve this, you need to make some preparations:

  • clean "a register:: :let @a=""
  • add a comma before the first element: {,1,2,3,4,5}
  • set the cursor to } in normal mode

Ok, here we go:

  • execute "AdF, and press . to repeat until all items are deleted.
  • execute "aP to insert the reverse sequence, you get: {,5,4,3,2,1}
  • remove first comma
+3
source

Using regex

 :.s/\v(\d+),(\d+),(\d+),(\d+),(\d+)/\5,\4,\3,\2,\1 
0
source

Source: https://habr.com/ru/post/1274422/


All Articles