Skip to content

Instantly share code, notes, and snippets.

@rsvp
Last active February 2, 2017 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsvp/ce3d64633713830fbea406f55adcd5e5 to your computer and use it in GitHub Desktop.
Save rsvp/ce3d64633713830fbea406f55adcd5e5 to your computer and use it in GitHub Desktop.
IPython v5 config file with custom PROMPTS [deprecating PromptManager], simulating readline vi-mode
#!/usr/bin/env python
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=python : per PEP 0263
# python 2.7.13 under Linux Ubuntu 14.04 Date : 2017-02-02
# IPython 5.1.0 from Anaconda distribution
#
# _______________| ipython_config.py : custom config file
#
'''
CHANGE LOG Latest config version, see https://git.io/ipython_config.py
2017-02-02 Beautify "program" layout, cosmetic color changes:
this script is for configuration only, NOT startup.
Use os.sep as directory separator for CROSS-PLATFORM.
2017-02-01 Add UTC timestamp to Out prompt.
2017-01-30 In prompt: newline indent current_directory _iN >>>
Out prompt: indent _N timestamp_utc newline
Purpose is readability using the underscore abbreviations
which can serve as variables at the command line,
plus the results can be edited for doctests.
2017-01-29 Modify for IPython v5+, so deprecate PromptManager, and
rewrite prompts using more tedious script and Pygments.
Insanity to configure PROMPTS starting from v5 of IPython, must see:
https://github.com/ipython/ipython/issues/9388
Adapted from @takluyver gist, circa July 2016:
https://gist.github.com/takluyver/85b33db0836cdcc4baf252fd81937fa7
2014-07-18 First version.
'''
from IPython.terminal.prompts import Prompts, Token
from datetime import datetime
import os
c = get_config()
app = c.InteractiveShellApp
# Use the following at any point in a config file to load a sub config
# and merge it into the current one:
load_subconfig('ipython_config.py', profile='default')
# Thus the scheme here is to OVERLAY OVER DEFAULT config.
# ========================================================================
class MyPrompts(Prompts):
def in_prompt_tokens(self, cli=None):
truncdir = os.getcwd().split(os.sep)[-3:]
# truncated list of last parts of the current working directory
return [
(Token, u'\n '),
(Token.Prompt, os.sep.join(truncdir)),
(Token.Prompt, u' _i'),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, u' >>> '),
]
def continuation_prompt_tokens(self, cli=None, width=None):
if width is None:
width = self._width()
return [
(Token.Prompt, (' ' * (width - 2)) + u'│ '),
]
def out_prompt_tokens(self):
utc_nano = str(datetime.utcnow())
# 2017-02-01 13:46:27.260500
utc = utc_nano.split('.')[0]
# 2017-02-01 13:46:27
return [
(Token.OutPrompt, u' _'),
(Token.OutPromptNum, str(self.shell.execution_count)),
(Token.Prompt, u' UTC '),
(Token.Prompt, utc),
(Token, u' \n'),
]
# Use IPython v5+ new API:
c.TerminalInteractiveShell.prompts_class = MyPrompts
# Alternative: get "--classic" style PROMPTS by executing, "%doctest_mode"
# ========================================================================
# Original 2014 settings...
c.InteractiveShell.separate_in = ''
c.InteractiveShell.separate_out = ''
c.InteractiveShell.separate_out2 = ''
# 2017-01-29 Try v5 COLORS:
c.InteractiveShell.colors = 'linux'
# 'linux' is optimised for dark backgrounds.
# c.InteractiveShell.highlighting_style = 'native'
# 'native' and 'paraiso-dark' are some compatible Pygments color schemes.
# 2014-07-18 gvim will not work with terminal.
c.TerminalInteractiveShell.editor = "vim"
# 2017-01-29 IPython 5.2 will invoke $EDITOR by F2 key. @takluyver
# 2017-01-29 .inputrc for readline ineffective as of IPython 5.0, fix:
c.TerminalInteractiveShell.editing_mode = 'vi'
# "MULTILINE EDITING [v5 new feature] means that if you type e.g.
# for a in rage(5):<enter>
# you can press up-arrow and go back to the line above,
# without having to cancel the input."
c.PrefilterManager.multi_line_specials = True
# 2014-07-18 For modified modules AUTORELOAD
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
# See also use of %reload and %dreload
# _____ ADDITIONAL "lines"
#
# Once you run %rehashx, all of your $PATH has been loaded as IPython
# aliases, so you should be able to type any normal system command and have
# it executed. See %alias? and %unalias? for details on the alias facilities.
lines = """
%rehashx
"""
app.exec_lines.append(lines)
# ===================================================== GRAVEYARD ===============
# _____ PromptManager DEPRECATED as of IPython v5
#
# # Prompt includes user@host and IPython profile name \Y1
# c.PromptManager.in_template = r'{color.LightGreen}\u@\h{color.LightBlue}[{color.LightCyan}\Y1{color.LightBlue}]{color.Green}|\#> '
# # Prompt includes user \u and pwd \w also with beginning newline:
# # or \Y1 for current directory:
# c.PromptManager.in_template = r'\n{color.LightBlue} \u: {color.LightCyan}\Y1{color.LightBlue} {color.Green}[\#> '
#
# c.PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
#
# c.PromptManager.out_template = r'<\#> '
#
# c.PromptManager.justify = True
# #
# # 2017-01-29 PromptManager is deprecated as of IPython 5.0,
# # so use instead: TerminalInteractiveShell.prompts_class
@rsvp
Copy link
Author

rsvp commented Jan 30, 2017

Genesis of the above arises from ipython/ipython#9388
"prompt_toolkit ignores .inputrc"

IPython v5+ deprecated calls to readline facilities of Linux, Mac, and Windows --
and developed its own cross-platform solution which feature instant
syntax highlighting with Pygments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment