Skip to content

Instantly share code, notes, and snippets.

@kylepollina
Last active February 2, 2021 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylepollina/078cd33bc8c53b56026d2f91fee16125 to your computer and use it in GitHub Desktop.
Save kylepollina/078cd33bc8c53b56026d2f91fee16125 to your computer and use it in GitHub Desktop.
My Custom IPython Config
# .config/ipython/profile_default/ipython_config.py
######################
""" IPython Config """
######################
c = get_config()
c.Completer.use_jedi = False
c.TerminalInteractiveShell.editing_mode = 'vi'
c.TerminalIPythonApp.display_banner = True
c.InteractiveShellApp.exec_lines = [
'import pandas as pd',
'import json'
]
c.InteractiveShell.highlighting_style = 'solarized-light'
c.InteractiveShell.autoindent = True
c.InteractiveShell.editor = 'nvim'
c.InteractiveShell.xmode = 'Context'
c.AliasManager.user_aliases = [
('la', 'ls -al'),
('vim', 'nvim'),
('lz', 'lazygit'),
('rg', 'rg'),
('fzf', 'fzf'),
('z', 'zsh')
]
#####################
""" Custom Prompt """
#####################
import os
from IPython.terminal.prompts import Prompts, Token
from IPython import get_ipython
class CustomPrompt(Prompts):
def __init__(self, shell):
self.shell = shell
def in_prompt_tokens(self, cli=None):
line_num = self.shell.execution_count
cwd = os.getcwd()
cwd = cwd.replace(os.path.expanduser('~'), '~')
if len(cwd) > 50:
cwd = '.../' + '/'.join(cwd.split('/')[-3:])
return [
(Token.In, '['),
(Token.In.Number, str(line_num)),
(Token.In, ']'),
(Token.Name.Class, '─' * (os.get_terminal_size().columns - len(cwd) - len(str(line_num)) - 4)),
(Token.Name.Symbol, ' ' + cwd + '\n'),
(Token.Prompt, '>>> ',),
]
def continuation_prompt_tokens(self, cli=None, width=None):
return [(Token.Symbol, '... '), ]
def out_prompt_tokens(self, cli=None):
return [(Token.Prompt, ''), ]
c.TerminalInteractiveShell.prompts_class = CustomPrompt
c.TerminalInteractiveShell.separate_in = '' # Disable newline after every input
# .config/ipython/profile_default/startup/vim.py
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.input_mode = InputMode.NAVIGATION
if getattr(ip, 'pt_app', None):
registry = ip.pt_app.key_bindings
# Add binding to make the sequence 'fj' exit vim-insert mode
# and go into vim-normal mode
registry.add_binding(
u'f',u'j',
filter=(HasFocus(DEFAULT_BUFFER) & ViInsertMode())
)(switch_to_navigation_mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment