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
Written by Nícolas Lazarte Kaqui
Related protips
8 Responses
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.
@thenickcox thkz, i agree with this title is so much clearer :)
What about {:'a' => 'b'} ? ;)
@woto {:'a' => 'b'} === {:a => 'b'} === {a: 'b'}
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.
@woto i understood, i updated the tip to ignore this cases
thkz (:
Is there a regexp syntax to do just the inverse?
@kikito I think this solves
:%s/\(\w*\): \([':]\)/:\1 => \2/gc