Sublime Text 3 - apply shortcut only to specific file types - keyboard-shortcuts

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.

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?

Windows Terminal Paste Pre-Defined Text via Custom Key Bind

I copy and paste different sets of pre-defined text often, so it would be nice to have a program-level key bind to set this. In the .json file key bindings section, I have tried things like:
{
"command": {
"command": "paste",
"insert": "<custom txt",
"keys": "ctrl+s"
}
}
or
{
"command": {
"command": "paste",
"commandline": "custom txt",
}
"keys": "ctrl+s"
}
.
There appears to be no action set for this in the available Windows Terminal key binding doc.
Send input: Send arbitrary text input to the shell.
Add the following item in the keybindings (or actions) array:
// Press Ctrl+S to send text (defined by the "input" field) to the shell.
// This command is not currently bound in the default settings.
{ "command": { "action": "sendInput", "input": "some custom text" }, "keys": "ctrl+s" }
ⓘ Important
As of Windows Terminal version 1.4, the keybindings array has been
renamed to actions inside the settings.json file. Support for the
keybindings array still exists for backward compatibility, however
the terminal will not automatically rename keybindings to actions
inside your settings.json file.

stylelint on create-react-app #import-normalize throw error

I followed this doc to add CSS reset to my app.
https://create-react-app.dev/docs/adding-css-reset/#indexcss
But it showed this message:
"stylelint": {
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": null
}
How to fix this problem?it is annoying...
To fix this warning you just need to add this line to.vscode/settings.json inside your project (you can create this file if it doesn't already exist):
{
"css.lint.unknownAtRules": "ignore"
}
Source: https://create-react-app.dev/docs/adding-css-reset/#indexcss
For VS Code -
To make the VS Code recognise this custom CSS directive, you can provide custom data for VS Code's CSS Language Service as mentioned here - https://github.com/Microsoft/vscode-css-languageservice/blob/master/docs/customData.md.
Create a CSS custom data set file with the following info. Place it at location .vscode/custom.css-data.json relative to the project root.
{
"version": 1.1,
"properties": [],
"atDirectives": [
{
"name": "#import-normalize",
"description": "bring in normalize.css styles"
}
],
"pseudoClasses": [],
"pseudoElements": []
}
Now, if you don't have already, create a .vscode\settings.json file relative to project root. Add a field with key "css.customData" and value as the path to custom data set. For example,
{
"css.customData": ["./.vscode/custom.css-data.json"]
}
Now, you will no longer get "Unknown at rule" warning. When you hover over "#import-normalize", you will see the description you set for it in custom.css-data.json
#import-normalize is a non-standard at-rule. From the rule's documentation:
This rule considers at-rules defined in the CSS Specifications, up to and including Editor's Drafts, to be known.
However, the rule has an ignoreAtRules secondary option for exactly this use case, where you can list the non-standard imports you are using.
For example, in your package.json:
{
"stylelint": {
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": [true, {
"ignoreAtRules": ["import-normalise"]
}]
}
}
}
Or within your .stylelintrc file:
{
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": [true, {
"ignoreAtRules": ["import-normalise"]
}
}
}

File Dependent Key Bindings

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)"}
]
},

Sublime Text - Remove plugin's keymap shortcuts

In ST3, I want to keep the default key-map for super+alt+v, which is the paste history menu.
However, a plugin that I would like to use overrides those keys in it's default keymap. Can I remove the plugin's shortcut for super+alt+v or the keymap entirely?
This is what I want to keep (from the 'Default (OSX).sublime-keymap')
{ "keys": ["super+option+v"], "command": "paste_from_history" }
The is the perpetrator:
{ "keys": ["super+alt+v"], "command": "text_pastry_insert_text", "args": { "clipboard": true, "separator": "\n" } },
Turns out the Plugin's User settings will override the Plugin's Default.
So I copied { "keys": ["super+option+v"], "command": "paste_from_history" } from the original Default Sublime keymap into Text Pastry/User keymap and it's back. Thanks #Martin for talking it out with me.