Sublime Forum

Find menu: Keyboard shortcut to enable or disable Regular expressions?

#1

Sublime can use regular expressions in any of it’s Find and Replace menus.
The keyboard shortcut CMD+ALT+R can be used to toggle Regular Expressions on and off:

10

However, is there a keyboard shortcut to ensure that Regular Expressions is always on or always off, instead of toggling it?

E.g.
I could press a shortcut to always make sure it is on and it will stay on even if it is already on.

0 Likes

#2

I don’t think that’s currently possible. As far as I know, there is no way to detect the current state of the regex button (whether it’s active or not). If there was, one could easily write an EventListener to do your task.

2 Likes

#4

one possibility is to force it on when opening the panel or reshow the panel with it set to a specific state, but I’m not sure if there is a context for knowing the current state - maybe it would be possible to make one by querying the syntax setting on the find input box to see if it is plain text or regex…

2 Likes

#5

Thanks @kingkeith. I stand corrected. I had forgotten one could set individual values in the args of the show_panel command.
To answer the original question then (hopefully), here is a possible solution with a keybinding that does it (with full credits to @kingkeith)

{
	"keys": ["ctrl+h"],
	"command": "show_panel",
	"args": {
		"panel": "replace",
		"reverse": false,
		"regex": true
	},
},

Place this keybinding in any of your User keybinding files (This overrides the default ctrl + h that opens the replace panel or you can have any key binding you want). This will make sure that the regex button is always turned on when the replace panel is opened even if you turn it off in a previous usage & close the panel. The state of the other buttons should stay the same as the last usage.
(This will only work if you press only ctrl + h (or your defined binding) & not if you select it from Find -> Replace from main menu as that is tied to the original show_panel command without the regex flag to true)

1 Like

#6

Thanks. This is the steps I tried (I am on Mac OSX):

  1. I click on Sublime Text > Preferences > Keybindings.
  2. This opens up a file called Default (OSX).sublime-keymap. The file is blank.
  3. I paste in the code in @UltraInstinct05 post.
  4. It displays the error message:

Error trying to parse file: Unexpected trailing characters in Packages/User/Default (OSX).sublime-keymap:9:2

  1. I think the issue, is the trailing command at the end of the code in the post, so I delete that comma and the error message goes away.
  2. Now when I press CTRL+h it just deletes the character before the curser.
  3. If I press CMD+F to open the find menu and then press CTRL+h nothing happens.

Any ideas? Thanks!
And also thanks for all the help everyone has given so far.

0 Likes

#7

I don’t have a mac but looking at the Default (OSX).sublime-keymap, the replace command is bound to super + alt + f. Does changing to this key combination help in anyway ?

1 Like

#8

Do you mean to put the following into Sublime Text > Preferences > Keybindings:

{
	"keys": ["super + alt + f"],
	"command": "show_panel",
	"args": {
		"panel": "replace",
		"reverse": false,
		"regex": true
	},
}
0 Likes

#9

Yes. You said that ctrl + h doesn’t work so I just suggested change it to super + alt + f to see if that works.

1 Like

#10

@UltraInstinct05 Thanks, for your help. I tried “super + alt + f”, but it doesn’t work. I also tried “ctrl + alt + p” but that doesn’t work either. Any ideas? Thanks!

Just to clarify, I press CMD+F to open the find menu and then press “ctrl + alt + p”, but nothing happens. Thanks for any help you can offer and also thanks for all your continued help as well. :slight_smile:

0 Likes

#11

Can you please run sublime.log_commands(True) and sublime.log_input(True) in the console to see what command gets triggered when you press the keybindings you have tried so far.

1 Like

#12

Thanks, I set my keybidings to Ctrl + Alt + 9.
I then pressed Ctrl + ` to open the console. Then I pressed Ctrl + Alt + 9. Here is what the console showed:

key evt: control+alt+9
key evt: control+alt+9
chr evt: 9 (0x39)

Thanks for all your continued help.

0 Likes

#13

Have you set sublime.log_commands(True) ? If so then for some reason, the command doesn’t seem to be triggering. Can you try reverting to a freshly installed state and then try setting the key binding and see if it works ? On my end, it works perfectly with ctrl + alt + 9

1 Like

#14

Thanks, I got it working.

This is the code that I used in Sublime Text > Preferences > Keybindings.

[
{
    "keys": ["ctrl+r"],
    "command": "show_panel",
    "args": {
        "panel": "replace",
        "reverse": false,
        "regex": true
    },
}
]

It needed the square brackets! Doh! Thanks for all your help :slight_smile:

0 Likes

#15

Aah, okay yes. The key bindings should be in an array because you can have or define more than one. Anyways glad you got it working.

2 Likes

#16

Is there some way to make this toggleable instead of just enabling regex forever?

0 Likes

#17

If you want to toggle the state of the Regex option, the default keybinding for it is alt + r. It will work when a find panel with the Regex option is opened.

1 Like

#18

It’s this command toggle_regex that toggles the regex flag in the Find / Replace / Find in All panels.

You can find it in e.g. Default (OSX).sublime-keymap

{ "keys": ["super+alt+r"], "command": "toggle_regex", "context":
  [
    { "key": "setting.is_widget", "operator": "equal", "operand": true }
  ]
},

For more related info see [Solved] Keyboard shortcut - Toggle wrap and highlight matches in find dialog

0 Likes