Is it possible to chain key binding commands in sublime text 2? - keyboard-shortcuts

There are times in Sublime Text when I want to reveal the current file in the side bar and then navigate around the folder structure.
This can be achieved using the commands reveal_in_side_bar and focus_side_bar however they have to be bound to two separate key combinations so I have to do 2 keyboard combinations to achieve my goal when ideally I'd like just one (I'm lazy).
Is there any way to bind multiple commands to a single key combination? e.g. something like this:
{
"keys": ["alt+shift+l"],
"commands": ["reveal_in_side_bar", "focus_side_bar"]
},
Solution
Based on #artem-ivanyk's and #d_rail's answers
1) Tools → New Plugin
import sublime, sublime_plugin
class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("reveal_in_side_bar")
self.window.run_command("focus_side_bar")
Save as RevealInSideBarAndFocus.py
2) Sublime Text 2 → Preferences → Key Bindings — User
Bind it to shortcut:
{ "keys": ["alt+shift+l"], "command": "reveal_in_side_bar_and_focus" }

Although the question is a year old, this might help people that are still looking for an answer.
Recently, a new package was developed by jisaacks, called Chain of command. It has the primary task to do exactly what you request, to chain several commands at once.
The package can be found here:
https://github.com/jisaacks/ChainOfCommand
An example of the working can be found below.
Let's say you wanted a key binding to duplicate the current file. You could set this key binding:
{
"keys": ["super+shift+option+d"],
"command": "chain",
"args": {
"commands": [
["select_all"],
["copy"],
["new_file"],
["paste"],
["save"]
]
}
}
This would select all the text, copy it, create a new file, paste the text, then open the save file dialog.
Source: https://sublime.wbond.net/packages/Chain%20of%20Command.

Updating #Artem Ivanyk's answer. I do not know what changed in Sublime, but that solution did not work for me, but I got this to work:
import sublime, sublime_plugin
class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("reveal_in_side_bar")
self.window.run_command("focus_side_bar")
.
{ "keys": ["ctrl+shift+8"], "command": "reveal_in_side_bar_and_focus" }
Btw, I'm using Build 2220

Stumbled upon similar problem. When trying to record macros, which involved „Save“ command, console threw at me „Unknown macros command save“ message.
Worked my way around with elementary plugin.
1) Tools → New Plugin
import sublime, sublime_plugin
class MyChainedActionsCommand():
def run(self):
self.view.run_command("reveal_in_side_bar")
self.view.run_command("focus_side_bar")
You need to use upper camel case notation for the class name. ST2 exposes this class for the command name with „Command“ suffix removed and the rest converted into the lowercase-underscore notation. I.e. in this example MyChainedActionsCommand could be run in sublime's console typing: view.run_command("my_chained_actions")
2) Sublime Text 2 → Preferences → Key Bindings — User
Bind it to shortcut:
{ "keys": ["alt+shift+l"], "command": "my_chained_actions" }
Heed commas.

Take a look at this gist.
I've been trying to implement this in a long time and found this by accident.
Don't forget to read the "documentation" provided. I kept trying to make this work, until I reallized I was not passing the "context" key.

You can create a macro to do this. For Sublime Text, macros are essentially just chained commands. You then create a keybinding for that macro. You can create a macro by using Tools > Record Macro, then executing your commands (beware that macros record keystrokes as well, so you'll want to use the commands from the menu bar to not cause conflicts), then Stop Recording, then Save Macro. After you save the macro, you can open it back up in Sublime Text to make sure that it recorded only what you want.

Building on Artem Ivanyk reply, here is a version of ChainedActions that works with arguments. It takes two arguments for actions and args. Both are lists and each command in the list gets executed with the corresponding arguments. This admittedly stupid example inserts two snippets: view.run_command("chained_actions", {"actions":["insert_snippet","insert_snippet"],"args":[{"contents": "($0)"},{"contents": "1($0)"}]})`
import sublime
import sublime_plugin
class ChainedActionsCommand(sublime_plugin.TextCommand):
def run(self, edit, actions, args):
for i, action in enumerate(actions):
self.view.run_command(action, args[i])

I've tried to use the same command but I ended up with a bug that when the file's folder was already unfolded sublime moved my focus sidebar's top, where I can see the open files. To improve this behavior I've wrote a new plugin that ensures it'll behave as I want to, here it is https://github.com/miguelgraz/FocusFileOnSidebar

I am using Sublime text3 build - 3083. It solves the problem just by 'Reveal it in side bar', the focus comes automatically.
I have added a custom keyboard shortcut for 'Reveal in sidebar' by adding the following statement under Preferences->Key Bindings-User :
[
{ "keys": ["ctrl+shift+r"], "command": "reveal_in_side_bar"}
]
The option - 'Reveal in sidebar' was missing for image file types, since the context menu doesn't appear with the right click of the mouse. The custom keyboard shortcut comes handy in this situation.

Starting from Sublime Text Build 4103 the feature is supported natively:
"Added the chain command, which accepts a list of commands to run in its "commands" argument. This allows binding a key to run multiple commands without having to use a macro"
See Changelog on https://www.sublimetext.com/dev

Related

Keyboard shortcuts on Flutter web

I'm adding keyboard shortcuts to a Flutter web application.
I have a form within a custom FocusableActionDetector where the shortcut has form like this:
SingleActivator(LogicalKeyboardKey.digit2)
and action is like:
CustomActivateIntent: CallbackAction<CustomActivateIntent>(
onInvoke: (intent) { provider.value = "2"; },)
In the same form I have a couple of numeric TextFormFields. To permit writing the character "2" I have to put these text fields inside some new FocusableActionDetector, otherwise the previous detector catches the command and the text field loses the "2" character, and this is already quite weird... Moreover, after writing in any of the text fields the form focus detector doesn't work anymore.
I think this could be related to the focus system, which is yet not that clear to me.
Can anyone help find a clean solution?
I found a workaround: the FocusableActionDetector is now preceded by an if statement. The code looks like the following:
// I extract the form to a widget to make it clearer
var searchWidget = SearchWidget();
child: textEditingInProgress
? searchWidget
: FocusableActionDetector(
child: searchWidget,
...,
),
The textEditingInProgress bool is a field in a provider and is controlled by the FocusNodes belonging to the TextControllers.
Still this is not a perfect solution, in particular I'd like to understand why the previous approach was not working.

What's the keyboard shortcut in visual studio code to complete statement like in IntelliJ?

Is exist keyboard shortcut to complete statement in current line in visual studio code, like in IntelliJ?
The complete statement is that Complete Statement, I can enter Shift + Ctrl + Enter to complete current line smartly.
The visual studio code support this function?
Complete Statement will help to some extent.
However, this extension lacks one feature - going inside the block. For example - for a if block if you use the shortcut to autocomplete the statement the cursor will still stay on the same line unlike Jetbrains IDE where it intelligently moves to the correct line.
Those who want to get the same feel as Jetbrains IDE's complete statement feature they can customize the keyboard shortcut like this:
Shortcut 1: Ctrl+ Shift+ Enter (to complete current statement)
Shortcut 2: Ctrl+ Shift (to go to next line)
It's just some extra keystrokes but still you can at least get it done!
You probably looking for IntelliSense feature of VSCode.
If you select the language that you coding with it (at right of bottom bar) and press ctrl + space, auto-complete & suggestion menu (according to your selected language) was shown.
Update: this extension for VSCode is probably what you want.
using macros,and create:
"macros":
{
"end_semicolon": // add ;\n
[
"cursorEnd",
{
"command": "type",
"args": {"text": ";\n"}
},
],
"end_colon": // add :\n\t
[
"cursorEnd",
{
"command": "type",
"args": {"text": ":\n\t"}
},
],
}

Key binding to wrap a selection with an html tag in VSCode

Is there a default key binding in VSCode to wrap a selection in an HTML tag like the keyboard shortcut Shift+alt+W in Visual studio 2015? I could not find anything similar in documentation or in the default keyboard shortcuts that indicates its availability out of the box.
To automate this go to.
File > Preferences > Keyboard Shortcuts
and add this into your keybindings.json (right hand side window)
{
"key": "ctrl+shift+enter",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorTextFocus && !editorReadonly"
}
You can replace ctrl+shift+enter with your own key combination.
you can use this extension:
https://github.com/Microsoft/vscode-htmltagwrap
or you can:
open the Command Palette: Command/Control+Shift+P (⇧⌘P)
type "wrap", then select "Wrap with abbreviation"
type the tag you want and press enter
Or simply search for the HTMLtagwrap extension in VScode, Make selection. Then use Alt + W. Then enter the new tag.

How to make jedit file-dropdown to display absolute path (not filename followed by directory)?

All is in the title.
If a have opened the three files:
/some/relatively/long/path/dir1/file_a
/some/relatively/long/path/dir1/file_b
/some/relatively/long/path/dir2/file_a
The file dropdown contains:
file_a (/some/relatively/long/path/dir1)
file_a (/some/relatively/long/path/dir2)
file_b (/some/relatively/long/path/dir1)
And that bother me because I have to look on the right to differentiate the two file_a, and on the left for the others. This happens a lot to me mostly because I code in python, and thus I often have several __init__.py files opened.
How do I get jedit to display
/some/relatively/long/path/dir1/file_a
/some/relatively/long/path/dir1/file_b
/some/relatively/long/path/dir2/file_a
config:
jedit 5.1.0
java 1.6.0_26
mac osx 10.6
Unfortunately this is not easily possible currently, I just had a look at the source and this is not configurable.
You can:
Submit a Feature Request to make this configurable (good idea in any case)
Create or let create a startup macro that
registers an EBComponent with the EditBus that listens for new EditPanes getting created
retrieve the BufferSwitcher from the EditPane
retrieve the ListCellRenderer from the BufferSwitcher
set a new ListCellRenderer to the BufferSwitcher that first calls the retrieved ListCellRenderer and then additionally sets the text to value.getPath()
Try the Buffer List plugin as to whether it maybe suits your needs
Now follows code that implements the work-part of option two, runnable as BeanShell code which does this manipulation for the current edit pane. The third line is not necessary when done in an EBComponent, this is just that the on-the-fly manipulation is shown immediately.
r = editPane.getBufferSwitcher().getRenderer();
editPane.getBufferSwitcher().setRenderer(
new ListCellRenderer() {
public Component getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) {
rc = r.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
rc.setText(value.getPath());
return rc;
}
});
editPane.repaint();

ClearAll["Global`*"] automatic insert

I don't know where to find and adjust the configuration file to have Mathematica insert ClearAll["Global`*"]
at the beginning of every new notebook. How can I do this, rather than having to type it everytime I open a new notebook?
AFAIK there is no way to change the default new Notebook. But it is possible to add custom keyboard shortcut and/or menu command which will create new Notebook which will contain whatever you need. For example, you can add menu item "New my Notebook" under the "File -> New" submenu and assign Ctrl+Shift+N keyboard shortcut to evaluate it (this modification will only persist during current FrontEndSession) by evaluating the following (taken from here and here):
FrontEndExecute[
FrontEnd`AddMenuCommands[
"New", {MenuItem["My new Notebook",
System`KernelExecute[
CreateDocument[ExpressionCell[Defer#ClearAll["Global`*"], "Input"]]],
FrontEnd`MenuKey["N", FrontEnd`Modifiers -> {"Control", "Shift"}],
System`MenuEvaluator -> Automatic]}]]
Now pressing Ctrl+Shift+N will open new Notebook window with "Input" cell already containing ClearAll["Global`*"]. If you replace FrontEnd`Modifiers -> {"Control", "Shift"} with FrontEnd`Modifiers -> {"Command"}, the keyboard combination will be Alt+N.
Information on how to make this change permanent can be found in this MathGroups post:
You can completely reset the menus using...
FrontEndExecute[FrontEnd`ResetMenusPacket[{Automatic}]]
You'll get some ugly flicker, but that would work. You can also put
the AddMenuCommands function in a front end init.m which can be found
someplace on the path specified in the ConfigurationPath option. In
that case, the init.m file will get executed by the FE as it starts,
not the kernel, and so it won't matter how many kernels you start or
quit.
So you need to create the init.m file in one of the paths given by the ConfigurationPath option:
Options[$FrontEnd, ConfigurationPath][[1, 2]]
{FrontEnd`FileName[{$InstallationDirectory, "Configuration", "FrontEnd"}],
FrontEnd`FileName[{$UserBaseDirectory, "Autoload", _, "Configuration", "FrontEnd"}],
FrontEnd`FileName[{$BaseDirectory, "Autoload", _, "Configuration", "FrontEnd"}],
FrontEnd`FileName[{$InstallationDirectory, "AddOns", "Autoload", _, "Configuration", "FrontEnd"}]}
Some of these paths contain blank (_) which is undocumented but seemingly means any name (I have not not checked this).
Another way to make this change permanent is to edit your MenuSetup.tr file, but it is not recommended.
P.S. I recommend you in future to ask your Mathematica-related questions on dedicated site, where they will receive more attention:
https://mathematica.stackexchange.com/