How Does One Set Specific Vim-Bindings in Ipython 5.0.0

How does one set specific vim-bindings in Ipython 5.0.0

You're right. prompt_toolkit ignores .inputrc. There does not seem to be a way to define custom keybindings for the vi mode in the IPython 5.0.0 profile configuration file.

Here's workaround I'm currently using. It's not pretty, but it works for now.

According to the IPython docs, you can specify Keyboard Shortcuts in a startup configuration script.

Instead of rebinding jk to ESC, I'm making a unicode "j" (u'j') followed by a unicode "k" (u'k') inside of VimInsertMode() a shortcut for a prompt_toolkit event that switches to navigation mode.

I created a .ipython/profile_default/startup/keybindings.py with the following code:

from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import HasFocus, ViInsertMode
from prompt_toolkit.key_binding.vi_state import InputMode

ip = get_ipython()

def switch_to_navigation_mode(event):
vi_state = event.cli.vi_state
vi_state.reset(InputMode.NAVIGATION)

if getattr(ip, 'pt_cli'):
registry = ip.pt_cli.application.key_bindings_registry
registry.add_binding(u'j',u'k',
filter=(HasFocus(DEFAULT_BUFFER)
& ViInsertMode()))(switch_to_navigation_mode)

The prompt_toolkit source will help you implement other shortcuts as needed.

How do I use vi keys in ipython under *nix?

In case someone's wandering in here recently, IPython 5.0 switched from readline to prompt_toolkit, so an updated answer to this question is to pass an option:

$ ipython --TerminalInteractiveShell.editing_mode=vi

... or to set it globally in the profile configuration (~/.ipython/profile_default/ipython_config.py; create it with ipython profile create if you don't have it) with:

c.TerminalInteractiveShell.editing_mode = 'vi'

ipython: change PageDown/PageUp to move back/forward through command history

Create script in ~/.ipython/profile_default/startup directory with any name ending with extension .py or .ipy

For example i created history_keybindings.py and put it inside ~/.ipython/profile_default/startup directory

from IPython import get_ipython
from IPython.terminal.shortcuts import previous_history_or_previous_completion, next_history_or_next_completion
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import HasSelection

ip = get_ipython()

registry = None

if (getattr(ip, 'pt_app', None)):
# for IPython versions 7.x
registry = ip.pt_app.key_bindings
elif (getattr(ip, 'pt_cli', None)):
# for IPython versions 5.x
registry = ip.pt_cli.application.key_bindings_registry

if registry:
registry.add_binding(Keys.PageUp, filter=(~HasSelection()))(previous_history_or_previous_completion)
registry.add_binding(Keys.PageDown, filter=(~HasSelection()))(next_history_or_next_completion)

Note: For more info check here

How do I set the driver's python version in spark?

You need to make sure the standalone project you're launching is launched with Python 3. If you are submitting your standalone program through spark-submit then it should work fine, but if you are launching it with python make sure you use python3 to start your app.

Also, make sure you have set your env variables in ./conf/spark-env.sh (if it doesn't exist you can use spark-env.sh.template as a base.)

adding syntax highlighting to Jupyter notebook cell magic

The following code works for SQL when placed in ~/.jupyter/custom/custom.js with notebook 5.x:

require(['notebook/js/codecell'], function(codecell) {
codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
Jupyter.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});
});

Credit goes to Thomas K for this info!



Related Topics



Leave a reply



Submit