Jupyter lab shortcuts - jupyter

I've been using Jupyter Notebooks for a couple of years now. I've just headed over to Jupyter Lab, but I've found the lack of shortcuts to be a burden.
For example, I noticed that I can search for commands in the left hand palette. But I can't seem to easily bind them to a keyboard shortcut. Is this even possible?
For example, I want to collapse the current cell output with "O" and collapse all code cells with "Shift O".

This question is answered on GitHub here. You can also look here for the correct command names to enter in your keyboard shortcut user overrides because they are not always the same as what is shown in the Commands side-bar.
The following are some that I use:
{
"shortcuts": [
{
"command": "notebook:hide-cell-outputs",
"keys": [
"O"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:show-cell-outputs",
"keys": [
"O",
"O"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:hide-all-cell-outputs",
"keys": [
"Ctrl L"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:hide-all-cell-code",
"keys": [
"Shift O"
],
"selector": ".jp-Notebook:focus"
}
]
}
which allows you to hide a cell output by pressing O once and showing the cell output by pressing O twice. The last one collapses all cell code with Shift + O as you requested.

On keyboards shortcuts of advance settings this code works fine for moving cells up and down
{
// Move cell up
"shortcuts": [
{
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-up",
"keys": [
"Alt ArrowUp"
]
},
// Move cell down
{
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-down",
"keys": [
"Alt ArrowDown"
]
}
]
}

I use these settings to bind the actions to move a cell up/down to Ctrl + Up/Down:
{
// Move cell up
"notebook:move-cell-up": {
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-up",
"keys": [
"Ctrl ArrowUp"
]
},
// Move cell down
"notebook:move-cell-down": {
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-down",
"keys": [
"Ctrl ArrowDown"
]
}
}

pX0r and plalanne's answers above combined worked for me with minor modification for Mac.
I hope this step-by-step iteration is helpful for someone like me who's a baby programmer. To summarize:
Open Advanced Settings Editor under the Settings tab, or command , in Mac.
Navigate to Keyboard Shortcuts. You should see the screen plalanne answered with.
Use pX0r's codes, however making one change in the key binding as Ctrl Arrowup is reserved in Mac to view all running applications (if you have it set up that way). Similarly, Shift Arrowup is for selecting multiple cells. As a result, I opted for Alt Arrowup. Notice the key on your Mac keyboard says alt/option. You have to refer to it as Alt to work. There you have it. Copy the codes below to User Overrides which is the right pane.
Re-open your notebook and test if it works as intended.
You can customize more keys in this fashion as long as it is defined here on GitHub. For the most part, all that you need are the command IDs starting line 72.
{
// Move cell up
"notebook:move-cell-up": {
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-up",
"keys": [
"Alt ArrowUp"
]
},
// Move cell down
"notebook:move-cell-down": {
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-down",
"keys": [
"Alt ArrowDown"
]
}
}

You should edit the settings file in Settings/Keyboard Shortcuts. Here :
There you can specify any custom shortcut that you would like!

If you cannot save the "User Preferences" settings and get a syntax error
[additional property error] command is not a valid property
you have probably missed to nest within the "shortcuts" list, as described here. Additionally, to override an old setting you do the following, using Activate Next Tab and Activate Previous Tab as examples:
{
"shortcuts": [
{
"command": "application:activate-next-tab",
"keys": [
"Ctrl Shift ]"
],
"selector": "body",
"disabled": true // disable old setting
},
{
"command": "application:activate-previous-tab",
"keys": [
"Ctrl Shift ["
],
"selector": "body",
"disabled": true // disable old setting
},
{
"command": "application:activate-next-tab",
"keys": [
"Ctrl 1" // enable new shortcut key
],
"selector": "body"
},
{
"command": "application:activate-previous-tab",
"keys": [
"Ctrl 2" // enable new shortcut key
],
"selector": "body"
}
]
}
Now you can click save and refresh your browser for the new setttings to take effect.

Related

Add a menu context to search specific strings.(How to use workbench.action.findInFiles?)

I want to add a menu context option to search specific query strings.
This query string is concatenated by constant string (with regex) and selected string.
For example :
The string = "HANDLE*"
When I select "EVENT" in editor and right click menu and click option.
It will jump to search viewlet and perform searching "HANDLE*EVENT" automatically.
According #99575.
Here is my code in package.json:
"contributes": {
"commands": [
{
"command": "testext.hello",
"title": "HELLO"
}
],
"menus": {
"editor/context": [
{
"when": "editorTextFocus",
"command": "workbench.action.findInFiles",
"args": {
"query": "HANDLE*${selectedText}",
"regexp": true
},
"group": "navigation"
}
]
}
The option has add to menu succesfully and click it will jump to search viewlet.
But query string has no pass to search viewlet.
I want to know How to pass the query string correctly?Thanks
Use build-in commands in keybindings.json:
It can add a key shortcut to search string with args.
{
"key": "alt+f",
"command": "search.action.openEditor",
"args": {
"query":"${selectedText}",
"isRegexp": true,
"matchWholeWord":true
}
}
If want to trigger search command from menu should be following extension dev flow.
Call executeCommand with openEditor and args in extension.ts:
vscode.commands.executeCommand('search.action.openEditor',{
query:"${selectedText}",
matchWholeWord:true
});
P.S. workbench.action.findInFiles has some problem with ${selectedText}.
I have reported it. #173653

How do I disable inserting a new empty line when I press Enter when the cursor is in brackets?

Steps to reproduce:
simple code:
if () {}
cursor is between {}
i press Enter
result:
if () {
}
expected result:
if () {
}
I want a empty line not to be inserted.
It may be that it works by default(adds a empty line), and when Alt+Enter it doesn't add a empty line.
I did not find settings in vscode. I didn't find anything on google.
I tried this:
{
"key": "alt+enter",
"command": "type",
"args": {
"text": "\n"
},
"when": "editorTextFocus"
}
Because Alt+Enter does nothing by default.
However, the onEnterRules function used with the editor.autoIndent option detects the addition of the \n character and adds an extra empty line anyway. :(
I want to use editor.autoIndent. But I want to turn off (do not turn on) using the shortcut Alt+Enter.
Worst option: look for an extension that does exactly the same as editor.autoIndent, but has the ability to create a shortcut Alt+Enter to work the way I want.
You can use the extension multi-command and construct a command that does what you want.
Add this to your settings.json (global or workspace)
"multiCommand.commands": {
"multiCommand.lineBreakNoEmptyline": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
}
}
Add this to your keybindings.json:
{
"key": "alt+enter",
"command": "multiCommand.lineBreakNoEmptyline",
"when": "editorTextFocus"
}
Or using the keybinding only method
{
"key": "alt+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
},
"when": "editorTextFocus"
}
I'll show the info from my comment here so that it is clearer. This keybinding:
{
"key": "alt+enter", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
// "lineBreakInsert",
{
"command": "type",
"args": {
"text": "\n"
}
},
"editor.action.clipboardCutAction"
]
},
}
The type command acts differently than a lineBreakInsert command so it is a little easier to then delete that extra line as the cursor is already there. It is just a small improvement, 2 less commands.
Demo:

VS Code scroll up/down moving the cursor

When scrolling down in VSCode. Using arrow keys makes you have the cursor on the bottom. I can use Ctrl + Arrow Down This scrolls the screen and the cursor keeps its position. In this case I need to click on the new line in order to start editing. However I'm looking for a way to scroll and move the cursor. For example if I'm in the middle of the screen I want to scroll and get the cursor to maintain its relative position in the middle.
Has anyone done this?
With the help of the extension Multi Command
Add this setting
"multiCommand.commands": [
{
"command": "multiCommand.up1LineKeepCursor",
"sequence": [
{"command": "editorScroll", "args": {"to": "up", "by": "line" }},
"cursorUp"
]
},
{
"command": "multiCommand.down1LineKeepCursor",
"sequence": [
{"command": "editorScroll", "args": {"to": "down", "by": "line" }},
"cursorDown"
]
}
]
And these keybindings
{
"key": "shift+ctrl+alt+up",
"command": "multiCommand.up1LineKeepCursor",
"when": "editorTextFocus"
},
{
"key": "shift+ctrl+alt+down",
"command": "multiCommand.down1LineKeepCursor",
"when": "editorTextFocus"
}
You can use any key binding you like.
It works good when Word Wrap is OFF.

How to add "show preview" button to VS Code extension?

I have a VS Code extension that analyses custom JSON and YAML files. So in the project's package.json, there is this:
"activationEvents": [
"onLanguage:yaml",
"onLanguage:json",
"onCommand:extension.sidePreview"
],
Whenever someone opens one of these files, I'd like to add a "show preview" icon in the top right corner of the editor:
So I added the corresponding icon resources to the project, and:
"contributes": {
"commands": [
{
"command": "extension.sidePreview",
"title": "Preview file",
"icon": {
"dark": "./resources/open-preview-dark.svg",
"light": "./resources/open-preview-light.svg"
}
}
],
"menus": {
"editor/title": [
{
"command": "extension.sidePreview",
"when": "true"
}
]
},
But this doesn't work... I don't see any icon.
I'd also like to ensure that this button and command are only available when my function isCustomFile in server.ts returns true. Is there a way of doing this?
That's because you added the wrong section under menus.
You are supposed to add editor/title instead.
Reference

How to disable/override the enter key for autocomplete?

In Sublime Text 3, I want to disable the enter key to select an item from the autocomplete drop down, and only allow the tab key to do so.
I found this section in the inbuilt Default (OSX).sublime-keymap file:
{ "keys": ["enter"], "command": "commit_completion", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
]
},
It seems that if I remove this from the config that enter will not select an item in the drop down. Unfortunately it is not recommended to change this file, and only to override it in my User files. I don't think I actually can edit it without modifying the .app contents.
I tried to override it by removing different sections, and also remove everything except "keys": ["enter"], but nothing seems to work.
How would I go about achieving this without modifying the inbuilt Default (OSX).sublime-keymap and only the User/Default (OSX).sublime-keymap file?
I have never used Sublime Text 3, but I don't think the following has changed since Sublime Text 2.
What you want to achieve is actually a standard feature in Sublime Text. You just have to turn it on.
This line from your the code you quoted …
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
… means "only execute the command if the setting called 'auto_complete_commit_on_tab' is set to false". So simply turn on that setting.
In Default/Preferences.sublime-settings:
// By default, auto complete will commit the current completion on enter.
// This setting can be used to make it complete on tab instead.
// Completing on tab is generally a superior option, as it removes
// ambiguity between committing the completion and inserting a newline.
"auto_complete_commit_on_tab": false,
Put "auto_complete_commit_on_tab": true in User/Preferences.sublime-settings. Both mentioned files can be accessed via the Preferences menu.
You can assign it to a non existent command. Try adding the following to User/Default (OSX).sublime-keymap
{ "keys": ["enter"], "command": "noop", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
]
}
Granted if you install/write a plugin that has a command noop you will need to change this command.
Edit
Lydell's solution is better :) Forgot about that setting (though it is in the context so I should have known...). Guess my answer is a more generic "how to disable a keybinding".
tried the solutions given above but they didn't work.
after some work this is what i came up with.
{ "keys": ["enter"], "command": "hide_auto_complete", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
], "command": "insert", "args": {"characters": "\n"}
}