Is there a way to use Jupyter Notebooks without modes? - jupyter-notebook

I am using jupyter-lab, and was wondering if there is a plugin that allows me to work without switching between command and edit modes?
I'm constantly cutting, pasting, and undoing cells because I accidentally switch into Command mode when I mean to be typing the characters 'x', 'y', or 'z' in Edit mode.
Some context: the concept of modes is integral to Jupyter notebooks. You are either in Command mode (Esc) or Edit mode (Enter), but I would like to use Jupyter Notebooks with a single set of shortcuts, as if I was editing a single document. #nomodes.

Aside from the default shortcuts, there is actually very little that's mode-specific in JupyterLab. Most commands that are run in command mode can be run without first exiting edit mode.
We can get pretty close to modeless editing in Jupyter by using a shortcut that's independent of the current mode. There are instructions on how to customize the keyboard shortcuts here.
As an example, the default shortcut for the cut-cell command is x. This could be replaced with the shortcut Accel Ctrl X. That can be used in either mode without interfering with editing. Just add the following into the User Preferences panel:
{"shortcuts": [
{
"command": "notebook:cut-cell",
"disabled": true,
"keys": ["X"],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:cut-cell",
"keys": ["Accel Ctrl X"],
"selector": "body"},
}
]}
Each default command mode shortcut should be modified as follows:
Prefix a universal modifier to the shortcut (e.g. Accel Ctrl).
Accel is just the Jupyter term for the Super/Command/Windows key on your keyboard.
Maintain the same shortcut keys for edit mode. The key insight is to use a "selector": "body". That selector can target both modes.
As with any default shortcut override, make sure to mark the original as "disabled: true".
Alternatively you could duplicate the same shortcut twice:
one for edit mode with "selector": ".jp-Notebook.jp-mod-editMode".
one for command mode with "selector": ".jp-Notebook:focus".
The default shortcuts for notebook:run-cell are set up this way actually.
But I have not run into trouble with using "body" as the selector.
Just a note: this is a fairly tedious and error-prone exercise. There are dozens of default shortcuts that need to be modified.

Related

How to avoid Jupyter cell-ids from changing all the time and thereby spamming the VCS diffs?

As discussed in q/66678305, newer Jupyter versions store in addition to the source code and output of cells an ID for the purpose of e.g. linking to a cell.
However, these IDs aren't stable but often change even when the cell's source code was not touched. As a result, if you have the .ipynb file under version control with e.g. git, the commits end up having lots of rather funny sounding “changed lines” that don't correspond to any actual change made in the commit. Like,
{
"cell_type": "code",
"execution_count": null,
- "id": "respected-breach",
+ "id": "incident-winning",
"metadata": {},
"outputs": [],
Is there a way to prevent this?
Answer for Git on Linux. Probably also works on MacOS, but not Windows.
It is good practice to not VCS the .ipynb files as saved by Jupyter, but instead a filtered version that does not contain all the volatile information. For this purpose, various git hooks are available; the one I'm using is based on https://github.com/toobaz/ipynb_output_filter/blob/master/ipynb_output_filter.py.
Strangely enough, it turns out this script can not be modified to remove the "id" field from cells. Namely, if you try to remove that field in the filtering loop, like with
for field in ("prompt_number", "execution_number", "id"):
if field in cell:
del cell[field]
then the write function from jupyter_nbformat will just put an id back in. It is possible to merely change the id to something constant, but then Jupyter will complain about nonunique ids.
As a hack to circumvent this, I now use this filter with a simple grep to delete the ID:
#!/bin/bash
grep -v '^ *"id": "[a-z\-]*",$'
Store that in e.g. ~/bin/ipynb_output_filter.sh, make it executable (chmod +x ~/bin/ipynb_output_filter.sh) and ensure you have the following ~/.gitattributes file:
*.ipynb filter=dropoutput_ipynb
and in your git config (either global ~/.gitconfig or project)
[core]
attributesfile = ~/.gitattributes
[filter "dropoutput_ipynb"]
clean = ~/bin/ipynb_output_filter.sh
smudge = cat
If you want to use a standard python filter in addition to that, you can invoke it before the grep in ~/bin/ipynb_output_filter.sh, like
#!/bin/bash
~/bin/ipynb_output_filter.py | grep -v '^ *"id": "[a-z\-]*",$'

How can I cause sbt to handle keyboard input properly?

BACKGROUND
When running sbt, I must press a "dead" key after each press of <ENTER>; meaning that pressing <ENTER> appears to do nothing... until I press another key -- any key -- when sbt ignores the follow-up key (i.e., "dead key") but behaves as if <ENTER> had been pressed.
After looking into the issue, I discovered that a system property ("jline.terminal") affects this behavior. This property can be one of {"auto", "unix", "win", "windows", "none", "off", "false", xxx} where xxx refers to a class implementing jline.Terminal.
When I set jline.terminal to be one of {"off", "false", "none"}, the problem with "broken <ENTER>" goes away. However, this mode drops support for the linux Readline library (i.e., up-arrow yields "^[[A" instead of recalling a line from the input history buffer).
So, I have 2 options:
Use "unix" as my jline.terminal and press <ENTER> + at the end of each input line; or
Use "none"|"off"|"false" as my jline.terminal and lose my command history support
How can I have both: a working <ENTER> key AND support for the Readline library?
ENVIRONMENT
OS: Ubuntu 17.10
Java: 1.8.0_161
sbt: 1.1.2

Is it possible to reopen running Sublime command console without hitting ctrl+B again (to prevent a new start)?

I run a code in sublime command console but accidentally pressed ctrl+f to find some text in the code. So the command console is replaced by search box. How can I make command console appear again without hitting ctrl+B as it will start a new code running? The code is run so far, I just want to continue it and need to see the progress is printed (I have the print line in the code) in console?
Just select Tools > Build Results > Show Build Results. You can also add a keybinding for it. E.g.
{
"keys": ["alt+b"],
"command": "show_panel",
"args": { "panel": "output.exec" },
},

vim-mode-plus keymaps for switching between panes not working

I have installed vim-mode-plus package and added the following keymap bindings in the keymap.cson file.
'atom-text-editor.vim-mode-plus.normal-mode':
'ctrl-j': 'focus-pane-or-panel:focus-below'
'ctrl-k': 'focus-pane-or-panel:focus-above'
'ctrl-h': 'focus-pane-or-panel:focus-on-left'
however I am not able to switch between the panes ( I am on Windows)
I am able to switch if I use a built-in keybinding as below:
ctrl-w ctrl-j window:focus-pane-below Vim Mode Plus atom-text-editor.vim-mode-plus:not(.insert-mode)
Above is copied from the Keybindings search results in atom
I don't think you are using the right command names. If you open the Command Palette (CtrlShiftP), you can search "focus" and find the various commands available for what you're trying to do.
Window: Focus Pane Above
Window: Focus Pane Below
Window: Focus Pane On Left
Window: Focus Pane On Right
To turn these into keybind command names, apply this formula:
Lowercase everything
Remove the space after :
Replace remaining spaces with hyphens
This gives the keybind command names:
window:focus-pane-above
window:focus-pane-below
window:focus-pane-on-left
window:focus-pane-on-right
Also notice the working example you point to uses a different selector than you are using. It uses atom-text-editor.vim-mode-plus:not(.insert-mode) rather than atom-text-editor.vim-mode-plus.normal-mode. That may be a better option, so your key bindings would work in modes other than normal mode (such as visual select mode).
I think what you need is:
'atom-text-editor.vim-mode-plus:not(.insert-mode)':
'ctrl-j': 'window:focus-pane-below'
'ctrl-k': 'window:focus-pane-above'
'ctrl-h': 'window:focus-pane-on-left'
And I'm not sure if you also wanted CtrlL to work, e.g.
'ctrl-l': 'window:focus-pane-on-right'

How to pass a line to the console in sublime text 2 editor

I use RStudio for working with R programming language and find the ctrl+enter shortcut to send a line to the console extremely useful in troubleshooting my work.
Now I am using sublimetext2 and I would like to do the same thing in RStudio, send a line to the console.
Is there a way to send the existing line to the console or a SublimeREPL console?
I don't know about the console, but this is possible with SublimeREPL.
As long as you have a REPL and a file of the same language open at the same time, you can send a line (or a selection or file) to your open REPL via the SublimeREPL Source Buffer Keys. By default, Ctrl+, followed by l sends the current line to the REPL, but you can change the hotkey to Ctrl+Enter (in Python only, to protect other languages' default Ctrl+Enter functionality) by adding these lines to the top of your Preferences -> Key Bindings – User file:
{ "keys": ["ctrl+enter"], "command": "repl_transfer_current", "args": {"scope": "lines"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.python", "match_all": true }
]
},
Other available scopes (from Preferences -> Browse Packages -> SublimeREPL/Default (Windows).sublime-keymap) are selection, file, and block (Clojure only). If you want to send a line to your REPL but not parse it immediately, you can add "action":"view_write" to the args object, like so:
{ "keys": ["ctrl+enter"], "command": "repl_transfer_current", "args": {"scope": "lines", "action": "view_write"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.python", "match_all": true }
]
},
See the Unofficial Sublime Text 2 Docs for more information on key bindings.
In the case that the REPL is open in a different tab than your source (rather than a separate view), the source buffer hotkeys will not focus the REPL. I'm sure it's possible to implement some sort of tab-swapping toggle key, but that sounds like a problem for another question.
In addition to setting up your own key bindings, you can simply install Enhanced-R:
In Sublime:
Cmd + Shift + P (to bring up the command palette)
type "Install Package"
Navigate to Enhanced-R
If you are using Sublime for mostly just R, then you can set the default syntax for the whole app. Or you can change it per file (Cmd + Shift + P again, then start typing Syntax Enhanced R)
Then, like you are used to in RStudio, you simply hit Cmd + enter to ship the code to the Console or R.app etc
Sending raw R code to SublimeREPL does work now:
Bring up the Cmd/Ctrl+Shift+P menu
Select R Application Switch
Select SublimeREPL
When you have SublimeREPL active, you'll be able to send raw R to it with Cmd/Ctrl+Enter.
Note that by default, SublimeREPL won't display the code that gets sent in; it'll just show you the output. If you want to also see the code, you can change your user settings:
Navigate to Preferences -> Package settings -> SublimeREPL -> Settings - User
Turn on the show_transferred_text setting.
For example, if you don't have any other settings, your settings should look like this:
{
"show_transferred_text": true
}

Resources