Sublime Forum

#{} syntax in ruby

#1

It would be nice to be able to have #{} automatically inserted for you when you put this inside of a quoted ruby string… Eg:

If you had the text “myVariable” highlighted in a string “myVariable” and hit #, you would get this: “#{myVariable}”
if you had the text “myVariable” highlighted in a string “myVariable and other things”, you would get this “#{myVariable} and other things”
If you had no text highlighted in a string, but your cursor was inbetween quotes, it would insert the brackets too… example string “” => “#{}” (with the cursor inbetween the {}'s)

0 Likes

#2

Of course I read the post “submitting user requests” after submitting a user request. oops!

0 Likes

#3

This is enabled in the dev build: http://www.sublimetext.com/dev

However, if you don’t want to step up to the dev build, you can use this keymapping: [code] { “keys”: “#”], “command”: “insert_snippet”, “args”: {“contents”: “#{${1:$SELECTION}}$0”}, “context”:

    { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
    {
    "operand": "string.quoted.double.ruby",
    "operator": "equal",
    "match_all": true,
    "key": "selector"
  }
]   

}[/code] (Place in your Preferences > Keybindings - User)

EDIT: added @castles change.

0 Likes

#4
"args": {"contents": "#{${1:$SELECTION}}$0"}
0 Likes

#5

@castles, Glad you’ve got my back :smile:

0 Likes

#6

haha, np dude :smile:

0 Likes

#7

I like the way the feature above is described. But…

…Does anyone know how to disable the curly braces appearing when I simply type the # symbol? I use Rails, and with their routing syntax and Stimulus JS syntax, auto-inserting curly braces inside a string is really annoying, but I don’t want to entirely disable auto_matching.

I’m using Sublime 3 on OSX.

0 Likes

#8

You could add the following line to your User/Default.sublime-keymap file. The additional - source.ruby.rails in the selector excludes the binding for Ruby on Rails syntax.

	// disable old key binding
	{ "keys": ["#"], "command": ""},
	// assign new key binding
	{ "keys": ["#"], "command": "insert_snippet", "args": {"contents": "#{${0:$SELECTION}}"}, "context":
		[
			{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{
				"operand": "(string.quoted.double.ruby | string.interpolated.ruby) - string source - source.ruby.rails",
				"operator": "equal",
				"match_all": true,
				"key": "selector"
			}
		]
	},
0 Likes