File Dependent Key Bindings - keyboard-shortcuts

I have added following piece of code to Preferences > Key Bindings - User configruation file in my Sublime Text 3:
{ "keys": ["ctrl+b"], "command": "insert_snippet", "args": {"contents": "<strong>${0:$SELECTION}</strong>"} },
As a result, when user presses Ctrl+B in editor, current selection is surrounded with HTML tags <strong> and <strong>.
Is there anyway, I can make this setting file-type depended? I.e. if user is working in *.txt or *.md file, then pressing Ctrl+B in editor should surround selection with Markdown bold tag (**) and, when editing any other type of file (in general or *.html files in particular), then to surround with HTML tags, as in above example.
Is this possible in Sublime Text 3?

This can be done with the context parameter:
"context": [
{
"key": "selector",
"operator": "equal",
"operand": "source.php"
}
]
selector Returns the name of the current scope.
operator Type of test to perform against key‘s value. Defaults to equal.
operand The result returned by key is tested against this value.
More help see: http://docs.sublimetext.info/en/latest/reference/key_bindings.html
Examples
For recognizing Ctrl+B in HTML only
// bold snippet for html
{
"keys": ["ctrl+b"],
"command": "insert_snippet",
"args": {"contents": "<strong>${0:$SELECTION}</strong>"},
"context": [
{"key": "selector", "operator": "equal", "operand": "text.html.basic"}
]
},
For recognizing Ctrl+B in Markdown and plain text:
// bold snippet for markdown and plain text
{
"keys": ["ctrl+b"],
"command": "insert_snippet",
"args": {"contents": "**${0:$SELECTION}**"},
"context": [
{"key": "selector", "operator": "equal", "operand": "(text.html.markdown, text.plain)"}
]
},

Related

How to prevent JupyterLab from overwriting my settings?

My JupyterLab version is 3.5.0. I want the shortcuts to be similar with vscode, and customize them at the following path (on Mac)
~/.jupyter/lab/user-settings/#jupyterlab/shortcuts-extension/shortcuts.jupyterlab-settings
but each time reopen JupyterLab, this file shortcuts.jupyterlab-settings will be overwritten, and the disabled shortcuts will be added back automatically.
For example, the disabled key
"command": "notebook:change-cell-to-markdown",
"keys": [
"M"
],
"selector": ".jp-Notebook:focus",
"disabled": true
},
will be replaced by
{
"args": {},
"command": "notebook:change-cell-to-markdown",
"keys": [
"M"
],
"selector": ".jp-Notebook:focus"
},
The following screenshot shows the differences: the LHS is my settings, and the RHS is the file shortcuts.jupyterlab-settings overwritten by JupyterLab.
How to prevent this behaviour?

How to use IntelliJ's keymap for SublimeText 3

I'm very used to IntelliJ IDEA's family keybinding (PyCharm, WebStorm, RubyMine...).
How can I configure Sumblime Text 3 to use those shortcuts?
I've been googling and I can't find anything! But I don't think I'm the only one needing this. Thank you :)
Sublime Text 3 - User Key Bindings (IntelliJ IDEA Style) - Windows
[
{ "keys": ["ctrl+y"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Line.sublime-macro"} },
{ "keys": ["ctrl+d"], "command": "duplicate_line" },
{ "keys": ["shift+alt+up"], "command": "swap_line_up" },
{ "keys": ["shift+alt+down"], "command": "swap_line_down" }
]
found the answer here.

Why doesn't custom shortcut (to clear output in Jupyter Lab) work?

Following the instruction to create custom shortcuts in Jupyter Lab, I've got the hide and show work but not the one to clear cell output (the first command entry). Does anyone know what the issue is? Thank you!
{
"shortcuts": [
{
"command": "notebook:clear-cell-outputs",
"keys": [
"Ctrl L"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:hide-cell-outputs",
"keys": [
"O"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:show-cell-outputs",
"keys": [
"Shift O"
],
"selector": ".jp-Notebook:focus"
}
]
}
The correct command to use is notebook:clear-cell-output (no "s" at the end). The complete command list can be found here.

Sublime Text 3 - apply shortcut only to specific file types

I am using Sublime Text 3, and I installed JSFormat to format my .js files and configured the key binding like this:
{ "keys": ["ctrl+shift+f"], "command": "js_format" }
Now, I also want to be able to format my .css and .html files, so I found this shortcut:
{ "keys": ["ctrl+shift+f"], "command": "reindent" , "args": { "single_line": false } }
I want to use js_format for my .js files and use reindent for my .css and .html files.
Is it possible to specify a file type per shortcut?
Update
This apparently no longer works in Sublime Text 4.
Update
I've since discovered that this is a duplicate of Sublime Text 3: how to bind a shortcut to a specific file extension?
Original Answer
Add a context:
{
"keys": ["ctrl+shift+f"],
"command": "js_format",
"context": [
{
"key": "selector",
"operator": "equal",
"operand": "source.js"
}
]
}
The important part is setting operand to source.js. You can replace js with whatever file extension you want. You can also specify additional sources with commas. For example, this would cause a command to apply to all .html and .css files:
{ "key": "selector", "operator": "equal", "operand": "source.html, source.css" }
See the unofficial documentation on key bindings.

is there a wikipedia api call that can retrieve restriction status of the article?

Otherwise I must do querySelector on the page content to find if there is a some kind of padlock and by try and error check what (id or class) is unique to that icon.
Other source to find is this info is to go on information page by adding $action=info to the url params. But then another problem comes in that the protection status is written in that's particular wiki language.
Using the API is the right way to do it, but you need to use action=query. The padlocks icons are inconsistent across wikis, and most wikis probably don't even have them.
If you use the right parameters for your API query, you should be getting the results you're looking for.
Example for the English Wikipedia:
https://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&inprop=protection&titles=Elton%20John gives you this result:
{
"batchcomplete": "",
"query": {
"pages": {
"5052197": {
"pageid": 5052197,
"ns": 0,
"title": "Elton John",
"contentmodel": "wikitext",
"pagelanguage": "en",
"touched": "2015-10-02T03:49:24Z",
"lastrevid": 683730854,
"length": 115931,
"protection": [
{
"type": "edit",
"level": "autoconfirmed",
"expiry": "infinity"
},
{
"type": "move",
"level": "sysop",
"expiry": "infinity"
}
],
"restrictiontypes": [
"edit",
"move"
]
}
}
}
}
Here the protection array tells you that only sysops can move the page, and only autoconfirmed users can edit it.
If you make a similar query on another wiki, say the French Wikipedia: https://fr.wikipedia.org/w/api.php?action=query&prop=info&format=json&inprop=protection&titles=Malia%20Obama , you get this in response (trimmed):
"protection": [
{
"type": "edit",
"level": "sysop",
"expiry": "infinity"
},
{
"type": "move",
"level": "sysop",
"expiry": "infinity"
}
],
"restrictiontypes": [
"edit",
"move"
]
In this case, sysops are the only one who can move and edit the page.