kwno0w
Last Updated: February 25, 2016
·
4.645K
· 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

9705

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 ·
9706

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

over 1 year ago ·
9715

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

over 1 year ago ·
9729

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

over 1 year ago ·
9731

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 ·
9732

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

over 1 year ago ·
12602

Is there a regexp syntax to do just the inverse?

over 1 year ago ·
12606

@kikito I think this solves

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