##// END OF EJS Templates
Merge pull request #12894 from Carreau/suggest-xonsh...
Matthias Bussonnier -
r26460:a70fa7fd merge
parent child Browse files
Show More
@@ -1,198 +1,214 b''
1 .. _ipython_as_shell:
1 .. _ipython_as_shell:
2
2
3 .. note::
4
5 This page has been kept for historical reason. You most likely want to use
6 `Xonsh <https://xon.sh/>`__ instead of this.
7
8
3 =========================
9 =========================
4 IPython as a system shell
10 IPython as a system shell
5 =========================
11 =========================
6
12
7
13
8
14
9 Overview
15 Overview
10 ========
16 ========
11
17
12 It is possible to adapt IPython for system shell usage. In the past, IPython
18 It is possible to adapt IPython for system shell usage. In the past, IPython
13 shipped a special 'sh' profile for this purpose, but it had been quarantined
19 shipped a special 'sh' profile for this purpose, but it had been quarantined
14 since 0.11 release, and in 1.0 it was removed altogether. Nevertheless, much
20 since 0.11 release, and in 1.0 it was removed altogether. Nevertheless, much
15 of this section relies on machinery which does not require a custom profile.
21 of this section relies on machinery which does not require a custom profile.
16
22
17 You can set up your own 'sh' :ref:`profile <Profiles>` to be different from
23 You can set up your own 'sh' :ref:`profile <Profiles>` to be different from
18 the default profile such that:
24 the default profile such that:
19
25
20 * Prompt shows the current directory (see `Prompt customization`_)
26 * Prompt shows the current directory (see `Prompt customization`_)
21 * Make system commands directly available (in alias table) by running the
27 * Make system commands directly available (in alias table) by running the
22 ``%rehashx`` magic. If you install new programs along your PATH, you might
28 ``%rehashx`` magic. If you install new programs along your PATH, you might
23 want to run ``%rehashx`` to update the alias table
29 want to run ``%rehashx`` to update the alias table
24 * turn ``%autocall`` to full mode
30 * turn ``%autocall`` to full mode
25
31
26
32
27 Environment variables
33 Environment variables
28 =====================
34 =====================
29
35
30 Rather than manipulating os.environ directly, you may like to use the magic
36 Rather than manipulating os.environ directly, you may like to use the magic
31 `%env` command. With no arguments, this displays all environment variables
37 `%env` command. With no arguments, this displays all environment variables
32 and values. To get the value of a specific variable, use `%env var`. To set
38 and values. To get the value of a specific variable, use `%env var`. To set
33 the value of a specific variable, use `%env foo bar`, `%env foo=bar`. By
39 the value of a specific variable, use `%env foo bar`, `%env foo=bar`. By
34 default values are considered to be strings so quoting them is unnecessary.
40 default values are considered to be strings so quoting them is unnecessary.
35 However, Python variables are expanded as usual in the magic command, so
41 However, Python variables are expanded as usual in the magic command, so
36 `%env foo=$bar` means "set the environment variable foo to the value of the
42 `%env foo=$bar` means "set the environment variable foo to the value of the
37 Python variable `bar`".
43 Python variable `bar`".
38
44
39 Aliases
45 Aliases
40 =======
46 =======
41
47
42 Once you run ``%rehashx``, all of your $PATH has been loaded as IPython aliases,
48 Once you run ``%rehashx``, all of your $PATH has been loaded as IPython aliases,
43 so you should be able to type any normal system command and have it executed.
49 so you should be able to type any normal system command and have it executed.
44 See ``%alias?`` and ``%unalias?`` for details on the alias facilities. See also
50 See ``%alias?`` and ``%unalias?`` for details on the alias facilities. See also
45 ``%rehashx?`` for details on the mechanism used to load $PATH.
51 ``%rehashx?`` for details on the mechanism used to load $PATH.
46
52
53 .. warning::
54
55 See info at the top of the page. You most likely want to use
56 `Xonsh <https://xon.sh/>`__ instead of this.
47
57
48 Directory management
58 Directory management
49 ====================
59 ====================
50
60
51 Since each command passed by IPython to the underlying system is executed
61 Since each command passed by IPython to the underlying system is executed
52 in a subshell which exits immediately, you can NOT use !cd to navigate
62 in a subshell which exits immediately, you can NOT use !cd to navigate
53 the filesystem.
63 the filesystem.
54
64
55 IPython provides its own builtin ``%cd`` magic command to move in the
65 IPython provides its own builtin ``%cd`` magic command to move in the
56 filesystem (the % is not required with automagic on). It also maintains
66 filesystem (the % is not required with automagic on). It also maintains
57 a list of visited directories (use ``%dhist`` to see it) and allows direct
67 a list of visited directories (use ``%dhist`` to see it) and allows direct
58 switching to any of them. Type ``cd?`` for more details.
68 switching to any of them. Type ``cd?`` for more details.
59
69
60 ``%pushd``, ``%popd`` and ``%dirs`` are provided for directory stack handling.
70 ``%pushd``, ``%popd`` and ``%dirs`` are provided for directory stack handling.
61
71
62
72
63 Prompt customization
73 Prompt customization
64 ====================
74 ====================
65
75
66 See :ref:`custom_prompts`.
76 See :ref:`custom_prompts`.
67
77
68
78
69 .. _string_lists:
79 .. _string_lists:
70
80
71 String lists
81 String lists
72 ============
82 ============
73
83
74 String lists (IPython.utils.text.SList) are handy way to process output
84 String lists (IPython.utils.text.SList) are handy way to process output
75 from system commands. They are produced by ``var = !cmd`` syntax.
85 from system commands. They are produced by ``var = !cmd`` syntax.
76
86
77 First, we acquire the output of 'ls -l'::
87 First, we acquire the output of 'ls -l'::
78
88
79 [Q:doc/examples]|2> lines = !ls -l
89 [Q:doc/examples]|2> lines = !ls -l
80 ==
90 ==
81 ['total 23',
91 ['total 23',
82 '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py',
92 '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py',
83 '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py',
93 '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py',
84 '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py',
94 '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py',
85 '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py',
95 '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py',
86 '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py',
96 '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py',
87 '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py',
97 '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py',
88 '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc']
98 '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc']
89
99
90 Now, let's take a look at the contents of 'lines' (the first number is
100 Now, let's take a look at the contents of 'lines' (the first number is
91 the list element number)::
101 the list element number)::
92
102
93 [Q:doc/examples]|3> lines
103 [Q:doc/examples]|3> lines
94 <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
104 <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
95
105
96 0: total 23
106 0: total 23
97 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
107 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
98 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py
108 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py
99 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py
109 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py
100 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
110 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
101 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
111 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
102 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
112 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
103 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
113 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
104
114
105 Now, let's filter out the 'embed' lines::
115 Now, let's filter out the 'embed' lines::
106
116
107 [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1)
117 [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1)
108 [Q:doc/examples]|5> l2
118 [Q:doc/examples]|5> l2
109 <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
119 <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
110
120
111 0: total 23
121 0: total 23
112 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
122 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
113 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
123 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
114 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
124 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
115 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
125 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
116 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
126 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
117
127
118 Now, we want strings having just file names and permissions::
128 Now, we want strings having just file names and permissions::
119
129
120 [Q:doc/examples]|6> l2.fields(8,0)
130 [Q:doc/examples]|6> l2.fields(8,0)
121 <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
131 <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
122
132
123 0: total
133 0: total
124 1: example-demo.py -rw-rw-rw-
134 1: example-demo.py -rw-rw-rw-
125 2: example-gnuplot.py -rwxrwxrwx
135 2: example-gnuplot.py -rwxrwxrwx
126 3: extension.py -rwxrwxrwx
136 3: extension.py -rwxrwxrwx
127 4: seteditor.py -rwxrwxrwx
137 4: seteditor.py -rwxrwxrwx
128 5: seteditor.pyc -rwxrwxrwx
138 5: seteditor.pyc -rwxrwxrwx
129
139
130 Note how the line with 'total' does not raise IndexError.
140 Note how the line with 'total' does not raise IndexError.
131
141
132 If you want to split these (yielding lists), call fields() without
142 If you want to split these (yielding lists), call fields() without
133 arguments::
143 arguments::
134
144
135 [Q:doc/examples]|7> _.fields()
145 [Q:doc/examples]|7> _.fields()
136 <7>
146 <7>
137 [['total'],
147 [['total'],
138 ['example-demo.py', '-rw-rw-rw-'],
148 ['example-demo.py', '-rw-rw-rw-'],
139 ['example-gnuplot.py', '-rwxrwxrwx'],
149 ['example-gnuplot.py', '-rwxrwxrwx'],
140 ['extension.py', '-rwxrwxrwx'],
150 ['extension.py', '-rwxrwxrwx'],
141 ['seteditor.py', '-rwxrwxrwx'],
151 ['seteditor.py', '-rwxrwxrwx'],
142 ['seteditor.pyc', '-rwxrwxrwx']]
152 ['seteditor.pyc', '-rwxrwxrwx']]
143
153
144 If you want to pass these separated with spaces to a command (typical
154 If you want to pass these separated with spaces to a command (typical
145 for lists if files), use the .s property::
155 for lists if files), use the .s property::
146
156
147
157
148 [Q:doc/examples]|13> files = l2.fields(8).s
158 [Q:doc/examples]|13> files = l2.fields(8).s
149 [Q:doc/examples]|14> files
159 [Q:doc/examples]|14> files
150 <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc'
160 <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc'
151 [Q:doc/examples]|15> ls $files
161 [Q:doc/examples]|15> ls $files
152 example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc
162 example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc
153
163
154 SLists are inherited from normal Python lists, so every list method is
164 SLists are inherited from normal Python lists, so every list method is
155 available::
165 available::
156
166
157 [Q:doc/examples]|21> lines.append('hey')
167 [Q:doc/examples]|21> lines.append('hey')
158
168
159
169
160 Real world example: remove all files outside version control
170 Real world example: remove all files outside version control
161 ------------------------------------------------------------
171 ------------------------------------------------------------
162
172
163 First, capture output of "hg status"::
173 First, capture output of "hg status"::
164
174
165 [Q:/ipython]|28> out = !hg status
175 [Q:/ipython]|28> out = !hg status
166 ==
176 ==
167 ['M IPython\\extensions\\ipy_kitcfg.py',
177 ['M IPython\\extensions\\ipy_kitcfg.py',
168 'M IPython\\extensions\\ipy_rehashdir.py',
178 'M IPython\\extensions\\ipy_rehashdir.py',
169 ...
179 ...
170 '? build\\lib\\IPython\\Debugger.py',
180 '? build\\lib\\IPython\\Debugger.py',
171 '? build\\lib\\IPython\\extensions\\InterpreterExec.py',
181 '? build\\lib\\IPython\\extensions\\InterpreterExec.py',
172 '? build\\lib\\IPython\\extensions\\InterpreterPasteInput.py',
182 '? build\\lib\\IPython\\extensions\\InterpreterPasteInput.py',
173 ...
183 ...
174
184
175 (lines starting with ? are not under version control).
185 (lines starting with ? are not under version control).
176
186
177 ::
187 ::
178
188
179 [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1)
189 [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1)
180 [Q:/ipython]|36> junk
190 [Q:/ipython]|36> junk
181 <36> SList (.p, .n, .l, .s, .grep(), .fields() availab
191 <36> SList (.p, .n, .l, .s, .grep(), .fields() availab
182 ...
192 ...
183 10: build\bdist.win32\winexe\temp\_ctypes.py
193 10: build\bdist.win32\winexe\temp\_ctypes.py
184 11: build\bdist.win32\winexe\temp\_hashlib.py
194 11: build\bdist.win32\winexe\temp\_hashlib.py
185 12: build\bdist.win32\winexe\temp\_socket.py
195 12: build\bdist.win32\winexe\temp\_socket.py
186
196
187 Now we can just remove these files by doing 'rm $junk.s'.
197 Now we can just remove these files by doing 'rm $junk.s'.
188
198
189 The .n, .s, .p properties
199 The .n, .s, .p properties
190 -------------------------
200 -------------------------
191
201
192 Properties of `SList <https://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.text.html?highlight=SList#IPython.utils.text.SList>`_ wrapper
202 Properties of `SList <https://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.text.html?highlight=SList#IPython.utils.text.SList>`_ wrapper
193 provide a convenient ways to use contained text in different formats:
203 provide a convenient ways to use contained text in different formats:
194
204
195 * ``.n`` returns (original) string with lines separated by a newline
205 * ``.n`` returns (original) string with lines separated by a newline
196 * ``.s`` returns string with lines separated by single space (for
206 * ``.s`` returns string with lines separated by single space (for
197 convenient passing to system commands)
207 convenient passing to system commands)
198 * ``.p`` returns list of "path" objects from detected file names
208 * ``.p`` returns list of "path" objects from detected file names
209
210 .. error::
211
212 You went too far scroll back up. You most likely want to use
213 `Xonsh <https://xon.sh/>`__ instead of this.
214
General Comments 0
You need to be logged in to leave comments. Login now