##// END OF EJS Templates
Add some documentation on how to configure prompts.
Matthias Bussonnier -
Show More
@@ -0,0 +1,26 b''
1 """This is an example that shows how to create new prompts for IPython
2 """
3
4 from IPython.terminal.prompts import Prompts, Token
5 import os
6
7 class MyPrompt(Prompts):
8
9 def in_prompt_tokens(self, cli=None):
10 return [(Token, os.getcwd()),
11 (Token.Prompt, '>>>')]
12
13 def load_ipython_extension(shell):
14 new_prompts = MyPrompt(shell)
15 new_prompts.old_prompts = shell.prompts
16 shell.prompts = new_prompts
17
18 def unload_ipython_extension(shell):
19 if not hasattr(shell.prompts, 'old_prompts'):
20 print("cannot unload")
21 else:
22 shell.prompts = shell.prompts.old_prompts
23
24
25
26
@@ -2,52 +2,6 b''
2 Specific config details
2 Specific config details
3 =======================
3 =======================
4
4
5 Prompts
6 =======
7
8 In the terminal, the format of the input and output prompts can be
9 customised. This does not currently affect other frontends.
10
11 The following codes in the prompt string will be substituted into the
12 prompt string:
13
14 ====== =================================== =====================================================
15 Short Long Notes
16 ====== =================================== =====================================================
17 %n,\\# {color.number}{count}{color.prompt} history counter with bolding
18 \\N {count} history counter without bolding
19 \\D {dots} series of dots the same width as the history counter
20 \\T {time} current time
21 \\w {cwd} current working directory
22 \\W {cwd_last} basename of CWD
23 \\Xn {cwd_x[n]} Show the last n terms of the CWD. n=0 means show all.
24 \\Yn {cwd_y[n]} Like \Xn, but show '~' for $HOME
25 \\h hostname, up to the first '.'
26 \\H full hostname
27 \\u username (from the $USER environment variable)
28 \\v IPython version
29 \\$ root symbol ("$" for normal user or "#" for root)
30 ``\\`` escaped '\\'
31 \\n newline
32 \\r carriage return
33 n/a {color.<Name>} set terminal colour - see below for list of names
34 ====== =================================== =====================================================
35
36 Available colour names are: Black, BlinkBlack, BlinkBlue, BlinkCyan,
37 BlinkGreen, BlinkLightGray, BlinkPurple, BlinkRed, BlinkYellow, Blue,
38 Brown, Cyan, DarkGray, Green, LightBlue, LightCyan, LightGray, LightGreen,
39 LightPurple, LightRed, Purple, Red, White, Yellow. The selected colour
40 scheme also defines the names *prompt* and *number*. Finally, the name
41 *normal* resets the terminal to its default colour.
42
43 So, this config::
44
45 c.PromptManager.in_template = "{color.LightGreen}{time}{color.Yellow} \u{color.normal}>>>"
46
47 will produce input prompts with the time in light green, your username
48 in yellow, and a ``>>>`` prompt in the default terminal colour.
49
50
51 .. _termcolour:
5 .. _termcolour:
52
6
53 Terminal Colors
7 Terminal Colors
@@ -84,27 +38,10 b' These have shown problems:'
84 extensions. Once Gary's readline library is installed, the normal
38 extensions. Once Gary's readline library is installed, the normal
85 WinXP/2k command prompt works perfectly.
39 WinXP/2k command prompt works perfectly.
86
40
87 Currently the following color schemes are available:
88
89 * NoColor: uses no color escapes at all (all escapes are empty '' ''
90 strings). This 'scheme' is thus fully safe to use in any terminal.
91 * Linux: works well in Linux console type environments: dark
92 background with light fonts. It uses bright colors for
93 information, so it is difficult to read if you have a light
94 colored background.
95 * LightBG: the basic colors are similar to those in the Linux scheme
96 but darker. It is easy to read in terminals with light backgrounds.
97
98 IPython uses colors for two main groups of things: prompts and
41 IPython uses colors for two main groups of things: prompts and
99 tracebacks which are directly printed to the terminal, and the object
42 tracebacks which are directly printed to the terminal, and the object
100 introspection system which passes large sets of data through a pager.
43 introspection system which passes large sets of data through a pager.
101
44
102 If you are seeing garbage sequences in your terminal and no colour, you
103 may need to disable colours: run ``%colors NoColor`` inside IPython, or
104 add this to a config file::
105
106 c.InteractiveShell.colors = 'NoColor'
107
108 Colors in the pager
45 Colors in the pager
109 -------------------
46 -------------------
110
47
@@ -73,11 +73,6 b' Example config file'
73 c.InteractiveShell.editor = 'nano'
73 c.InteractiveShell.editor = 'nano'
74 c.InteractiveShell.xmode = 'Context'
74 c.InteractiveShell.xmode = 'Context'
75
75
76 c.PromptManager.in_template = 'In [\#]: '
77 c.PromptManager.in2_template = ' .\D.: '
78 c.PromptManager.out_template = 'Out[\#]: '
79 c.PromptManager.justify = True
80
81 c.PrefilterManager.multi_line_specials = True
76 c.PrefilterManager.multi_line_specials = True
82
77
83 c.AliasManager.user_aliases = [
78 c.AliasManager.user_aliases = [
@@ -63,20 +63,46 b' switching to any of them. Type ``cd?`` for more details.'
63 Prompt customization
63 Prompt customization
64 ====================
64 ====================
65
65
66 Here are some prompt configurations you can try out interactively by using the
66 Starting at IPython 5.0 the prompt customisation is done by subclassing :class:`IPython.terminal.prompts.Prompt`.
67 ``%config`` magic::
67
68
68 The custom ``Prompt`` receive the current IPython shell instance as first
69 %config PromptManager.in_template = r'{color.LightGreen}\u@\h{color.LightBlue}[{color.LightCyan}\Y1{color.LightBlue}]{color.Green}|\#> '
69 argument, which by default is stored as the ``shell`` attribute of the object.
70 %config PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
70 The class can implement optional methods for each of the available prompt types:
71 %config PromptManager.out_template = r'<\#> '
71
72
72 - ``in_prompt_tokens(self, cli=None)``, input prompt , default to ``In [1]``
73
73 - ``continuation_prompt_tokens(self, cli=None, width=None)``, continuation prompt for multi lines (default `...:`)
74 You can change the prompt configuration to your liking permanently by editing
74 - ``rewrite_prompt_tokens(self)``
75 ``ipython_config.py``::
75 - ``out_prompt_tokens(self)``
76
76
77 c.PromptManager.in_template = r'{color.LightGreen}\u@\h{color.LightBlue}[{color.LightCyan}\Y1{color.LightBlue}]{color.Green}|\#> '
77 Each of these methods should return a list of `(TokenType, Token)` pairs. See documentation of `prompt_toolkit` and/or `Pygments`.
78 c.PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
78
79 c.PromptManager.out_template = r'<\#> '
79 Here is an example of Prompt class that will insert the current working directory in front of a prompt:
80
81
82 .. codeblock:: python
83
84 from IPython.terminal.prompts import Prompts, Token
85 import os
86
87 class MyPrompt(Prompts):
88
89 def in_prompt_tokens(self, cli=None):
90 return [(Token, os.getcwd()),
91 (Token.Prompt, ' >>>')]
92
93 To set the new prompt, assign it to the `prompts` attribute of the IPython shell:
94
95 .. codeblock:: python
96
97 In[2]: ip = get_ipython()
98 ...: ip.prompts = MyPrompt(ip)
99
100 ~/ >>> # it works
101
102
103 See ``IPython/example/utils/cwd_prompt.py`` for an example of how to write an
104 extensions that customise prompts.
105
80
106
81 Read more about the :ref:`configuration system <config_overview>` for details
107 Read more about the :ref:`configuration system <config_overview>` for details
82 on how to find ``ipython_config.py``.
108 on how to find ``ipython_config.py``.
General Comments 0
You need to be logged in to leave comments. Login now