Last Updated: February 25, 2016
·
5.772K
· nicolaslazartekaqui

Vim - Regex to change hash rockets to new hash syntax in Ruby

Change this way

:param => 'value'

for this way

param: 'value'

open .rb file in vim and do

%s/:\([^=,'"]*\) =>/\1:/g

if you want use the interactive mode, add c in the end of command

%s/:\([^=,'"]*\) =>/\1:/gc

case you want the inverse, try

:%s/\(\w*\): \([':]\)/:\1 => \2/gc

8 Responses
Add your response

Technically, these aren't arguments. They're values for hash keys. Also, this isn't Rails specific. It's Ruby. Rails just happens to use these hashes as the data type for its params. The more appropriate title would be, "Vim regex to change hash rockets to new hash syntax in Ruby."

That said, this is helpful. Thanks.

over 1 year ago ·

@thenickcox thkz, i agree with this title is so much clearer :)

over 1 year ago ·

What about {:'a' => 'b'} ? ;)

over 1 year ago ·

@woto {:'a' => 'b'} === {:a => 'b'} === {a: 'b'}

over 1 year ago ·

I mean {:'a' => 'b'} {'a': 'b'} - invalid

Also there is situation when it can't be rewritten in this style, for example {:'!@#' => 'b'}. That's why its command can't be used in batch replacements.

over 1 year ago ·

@woto i understood, i updated the tip to ignore this cases
thkz (:

over 1 year ago ·

Is there a regexp syntax to do just the inverse?

over 1 year ago ·

@kikito I think this solves

:%s/\(\w*\): \([':]\)/:\1 => \2/gc
over 1 year ago ·