Sublime Forum

command_mode key bind

#1

How do I bind a key combo to command_mode?

{ "keys": "alt+z"], "command": "setting", "args": {"command_mode": true} }
0 Likes

#2

You use a context:

[code] { “keys”: “alt+z”], “command”: “reindent”, “context”:

		{ "key": "setting.command_mode", "operator": "equal", "operand": true }
	]
}[/code]

Of course, for this to ever work, you need to set command_mode to true first.

0 Likes

#3

Thats what im trying to work out… how do i turn command_mode on? with out doing it via the console whats the key combo… and can i bind one?

0 Likes

#4

Oh… There used to be a “set” command in Sublime 1. I’m not sure whether it’s in for Sublime 2 yet, but I can’t find it. Short of writing a plugin for it, I don’t see any other way.

Plugin:

[code]import sublime, sublime_plugin

class ToggleCommandModeCommand(sublime_plugin.TextCommand):
def run(self, edit):
status = bool(self.view.settings().get(‘command_mode’))
self.view.settings().set(‘command_mode’, not status)[/code]

In a .sublime-keymap:

{ "keys": "j", "j"], "command": "toggle_command_mode" } ]

0 Likes

#5

Thank you very much that worked very well!

Now to bind some keys to command mode?
has any one built VIM like key bindings?

0 Likes

#6
{ "keys": "d"], "command": "left_delete", "context":{ "key": "setting.command_mode", "operator": "equal", "operand": true }] },
{ "keys": "d", "d"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Line.sublime-macro"}, "context":{ "key": "setting.command_mode", "operator": "equal", "operand": true }] }

Why does the second “d”, “d” wipe out the first or make it behave odd?

0 Likes

#7

I believe Sublime resolves ambiguous key bindings with a timeout, so that d will presumable trigger only after a short while (so Sublime knows you didn’t mean d,d).

0 Likes

#8

ok… im trying to make it work like vims key binds. Can you set how long the time out should wait?

0 Likes

#9

Not feasible right now, as far as I know. I hope a more powerful keybindings system will be added in the future, though.

But you could approach this differently if you’re willing to put up with some frustration:

Forget about command mode and open an input_panel instead. Type the command there and parse that. I won’t work well for motions, and it’s far from ideal, but it’s something at least.

On the other hand, simple motions should work well with the existing support for command mode. hjkl, HJKL, b, e, a, E, A, o, O, G, gg, etc…

0 Likes

#10

Ok thanks, yeah would be cool if it could copy Vims edit mode.

But command mode is cool might make up some short cuts to go in there see how it goes

0 Likes

#11

I don’t believe vim has any key bindings as ambiguous as this. At least here, ‘d’ always accepts a follow-up motion, you’d use ‘x’ if you wanted to delete a single character with one keypress.

You can use the set_setting or toggle_setting commands to bind keys to settings: you can see examples in Main.sublime-menu

0 Likes