Question

I have numbers in the shape of (a/b) where a and b are integers. I would like to replace them with something like rat(a,b). Is that possible?

Was it helpful?

Solution 2

Vim is good at Search and Replace:

:%s/(\(\d\+\)\/\(\d\+\))/rat(\1,\2)/g

Too much backslashs! Yet another command:

:%s@\v\((\d+)/(\d+)\)@rat(\1,\2)@g
  • We can use @ to separate patterns instead of /.
  • The very magic \v makes life easier.

I suggest you type :help :s to learn more.

OTHER TIPS

I would do:

%s#\v(\d+)/(\d+)#rat(\1,\2)#g

The easier way to do this is probably to create a macro and replay it as many times as you need to.

  • press on qa then make the search & replace stuff
  • press q when done
  • then play the macro with @a

If you know regex, then you can go to Search patterns http://vim.wikia.com/wiki/Search_patterns

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top