##// END OF EJS Templates
Update pysh profile
Thomas Kluyver -
Show More
@@ -1,293 +1,294 b''
1 .. _ipython_as_shell:
1 .. _ipython_as_shell:
2
2
3 =========================
3 =========================
4 IPython as a system shell
4 IPython as a system shell
5 =========================
5 =========================
6
6
7 .. warning::
7 .. warning::
8
8
9 As of the 0.11 version of IPython, most of the APIs used by the shell
9 As of the 0.11 version of IPython, most of the APIs used by the shell
10 profile have been changed, so the profile currently does very little
10 profile have been changed, so the profile currently does very little
11 beyond changing the IPython prompt. To help restore the shell
11 beyond changing the IPython prompt. To help restore the shell
12 profile to past functionality described here, the old code is found in
12 profile to past functionality described here, the old code is found in
13 :file:`IPython/deathrow`, which needs to be updated to use the
13 :file:`IPython/deathrow`, which needs to be updated to use the
14 APIs in 0.11.
14 APIs in 0.11.
15
15
16 Overview
16 Overview
17 ========
17 ========
18
18
19 The 'sh' profile optimizes IPython for system shell usage. Apart from
19 The 'sh' profile optimizes IPython for system shell usage. Apart from
20 certain job control functionality that is present in unix (ctrl+z does
20 certain job control functionality that is present in unix (ctrl+z does
21 "suspend"), the sh profile should provide you with most of the
21 "suspend"), the sh profile should provide you with most of the
22 functionality you use daily in system shell, and more. Invoke IPython
22 functionality you use daily in system shell, and more. Invoke IPython
23 in 'sh' profile by doing 'ipython -p sh', or (in win32) by launching
23 in 'sh' profile by doing 'ipython -p sh', or (in win32) by launching
24 the "pysh" shortcut in start menu.
24 the "pysh" shortcut in start menu.
25
25
26 If you want to use the features of sh profile as your defaults (which
26 If you want to use the features of sh profile as your defaults (which
27 might be a good idea if you use other profiles a lot of the time but
27 might be a good idea if you use other profiles a lot of the time but
28 still want the convenience of sh profile), add ``import ipy_profile_sh``
28 still want the convenience of sh profile), add ``import ipy_profile_sh``
29 to your $IPYTHON_DIR/ipy_user_conf.py.
29 to your $IPYTHON_DIR/ipy_user_conf.py.
30
30
31 The 'sh' profile is different from the default profile in that:
31 The 'sh' profile is different from the default profile in that:
32
32
33 * Prompt shows the current directory
33 * Prompt shows the current directory
34 * Spacing between prompts and input is more compact (no padding with
34 * Spacing between prompts and input is more compact (no padding with
35 empty lines). The startup banner is more compact as well.
35 empty lines). The startup banner is more compact as well.
36 * System commands are directly available (in alias table) without
36 * System commands are directly available (in alias table) without
37 requesting %rehashx - however, if you install new programs along
37 requesting %rehashx - however, if you install new programs along
38 your PATH, you might want to run %rehashx to update the persistent
38 your PATH, you might want to run %rehashx to update the persistent
39 alias table
39 alias table
40 * Macros are stored in raw format by default. That is, instead of
40 * Macros are stored in raw format by default. That is, instead of
41 '_ip.system("cat foo"), the macro will contain text 'cat foo')
41 '_ip.system("cat foo"), the macro will contain text 'cat foo')
42 * Autocall is in full mode
42 * Autocall is in full mode
43 * Calling "up" does "cd .."
43 * Calling "up" does "cd .."
44
44
45 The 'sh' profile is different from the now-obsolete (and unavailable)
45 The 'sh' profile is different from the now-obsolete (and unavailable)
46 'pysh' profile in that the ``$$var = command`` and ``$var = command`` syntax is
46 'pysh' profile in that the ``$$var = command`` and ``$var = command`` syntax is
47 not supported anymore. Use ``var = !command`` instead (which is available in all
47 not supported anymore. Use ``var = !command`` instead (which is available in all
48 IPython profiles).
48 IPython profiles).
49
49
50 Aliases
50 Aliases
51 =======
51 =======
52
52
53 All of your $PATH has been loaded as IPython aliases, so you should be
53 All of your $PATH has been loaded as IPython aliases, so you should be
54 able to type any normal system command and have it executed. See
54 able to type any normal system command and have it executed. See
55 %alias? and %unalias? for details on the alias facilities. See also
55 %alias? and %unalias? for details on the alias facilities. See also
56 %rehashx? for details on the mechanism used to load $PATH.
56 %rehashx? for details on the mechanism used to load $PATH.
57
57
58
58
59 Directory management
59 Directory management
60 ====================
60 ====================
61
61
62 Since each command passed by ipython to the underlying system is executed
62 Since each command passed by ipython to the underlying system is executed
63 in a subshell which exits immediately, you can NOT use !cd to navigate
63 in a subshell which exits immediately, you can NOT use !cd to navigate
64 the filesystem.
64 the filesystem.
65
65
66 IPython provides its own builtin '%cd' magic command to move in the
66 IPython provides its own builtin '%cd' magic command to move in the
67 filesystem (the % is not required with automagic on). It also maintains
67 filesystem (the % is not required with automagic on). It also maintains
68 a list of visited directories (use %dhist to see it) and allows direct
68 a list of visited directories (use %dhist to see it) and allows direct
69 switching to any of them. Type 'cd?' for more details.
69 switching to any of them. Type 'cd?' for more details.
70
70
71 %pushd, %popd and %dirs are provided for directory stack handling.
71 %pushd, %popd and %dirs are provided for directory stack handling.
72
72
73
73
74 Enabled extensions
74 Enabled extensions
75 ==================
75 ==================
76
76
77 Some extensions, listed below, are enabled as default in this profile.
77 Some extensions, listed below, are enabled as default in this profile.
78
78
79 envpersist
79 envpersist
80 ----------
80 ----------
81
81
82 %env can be used to "remember" environment variable manipulations. Examples::
82 %env can be used to "remember" environment variable manipulations. Examples::
83
83
84 %env - Show all environment variables
84 %env - Show all environment variables
85 %env VISUAL=jed - set VISUAL to jed
85 %env VISUAL=jed - set VISUAL to jed
86 %env PATH+=;/foo - append ;foo to PATH
86 %env PATH+=;/foo - append ;foo to PATH
87 %env PATH+=;/bar - also append ;bar to PATH
87 %env PATH+=;/bar - also append ;bar to PATH
88 %env PATH-=/wbin; - prepend /wbin; to PATH
88 %env PATH-=/wbin; - prepend /wbin; to PATH
89 %env -d VISUAL - forget VISUAL persistent val
89 %env -d VISUAL - forget VISUAL persistent val
90 %env -p - print all persistent env modifications
90 %env -p - print all persistent env modifications
91
91
92 ipy_which
92 ipy_which
93 ---------
93 ---------
94
94
95 %which magic command. Like 'which' in unix, but knows about ipython aliases.
95 %which magic command. Like 'which' in unix, but knows about ipython aliases.
96
96
97 Example::
97 Example::
98
98
99 [C:/ipython]|14> %which st
99 [C:/ipython]|14> %which st
100 st -> start .
100 st -> start .
101 [C:/ipython]|15> %which d
101 [C:/ipython]|15> %which d
102 d -> dir /w /og /on
102 d -> dir /w /og /on
103 [C:/ipython]|16> %which cp
103 [C:/ipython]|16> %which cp
104 cp -> cp
104 cp -> cp
105 == c:\bin\cp.exe
105 == c:\bin\cp.exe
106 c:\bin\cp.exe
106 c:\bin\cp.exe
107
107
108 ipy_app_completers
108 ipy_app_completers
109 ------------------
109 ------------------
110
110
111 Custom tab completers for some apps like svn, hg, bzr, apt-get. Try 'apt-get install <TAB>' in debian/ubuntu.
111 Custom tab completers for some apps like svn, hg, bzr, apt-get. Try 'apt-get install <TAB>' in debian/ubuntu.
112
112
113 ipy_rehashdir
113 ipy_rehashdir
114 -------------
114 -------------
115
115
116 Allows you to add system command aliases for commands that are not along your path. Let's say that you just installed Putty and want to be able to invoke it without adding it to path, you can create the alias for it with rehashdir::
116 Allows you to add system command aliases for commands that are not along your path. Let's say that you just installed Putty and want to be able to invoke it without adding it to path, you can create the alias for it with rehashdir::
117
117
118 [~]|22> cd c:/opt/PuTTY/
118 [~]|22> cd c:/opt/PuTTY/
119 [c:opt/PuTTY]|23> rehashdir .
119 [c:opt/PuTTY]|23> rehashdir .
120 <23> ['pageant', 'plink', 'pscp', 'psftp', 'putty', 'puttygen', 'unins000']
120 <23> ['pageant', 'plink', 'pscp', 'psftp', 'putty', 'puttygen', 'unins000']
121
121
122 Now, you can execute any of those commams directly::
122 Now, you can execute any of those commams directly::
123
123
124 [c:opt/PuTTY]|24> cd
124 [c:opt/PuTTY]|24> cd
125 [~]|25> putty
125 [~]|25> putty
126
126
127 (the putty window opens).
127 (the putty window opens).
128
128
129 If you want to store the alias so that it will always be available, do '%store putty'. If you want to %store all these aliases persistently, just do it in a for loop::
129 If you want to store the alias so that it will always be available, do '%store putty'. If you want to %store all these aliases persistently, just do it in a for loop::
130
130
131 [~]|27> for a in _23:
131 [~]|27> for a in _23:
132 |..> %store $a
132 |..> %store $a
133 |..>
133 |..>
134 |..>
134 |..>
135 Alias stored: pageant (0, 'c:\\opt\\PuTTY\\pageant.exe')
135 Alias stored: pageant (0, 'c:\\opt\\PuTTY\\pageant.exe')
136 Alias stored: plink (0, 'c:\\opt\\PuTTY\\plink.exe')
136 Alias stored: plink (0, 'c:\\opt\\PuTTY\\plink.exe')
137 Alias stored: pscp (0, 'c:\\opt\\PuTTY\\pscp.exe')
137 Alias stored: pscp (0, 'c:\\opt\\PuTTY\\pscp.exe')
138 Alias stored: psftp (0, 'c:\\opt\\PuTTY\\psftp.exe')
138 Alias stored: psftp (0, 'c:\\opt\\PuTTY\\psftp.exe')
139 ...
139 ...
140
140
141 mglob
141 mglob
142 -----
142 -----
143
143
144 Provide the magic function %mglob, which makes it easier (than the 'find' command) to collect (possibly recursive) file lists. Examples::
144 Provide the magic function %mglob, which makes it easier (than the 'find' command) to collect (possibly recursive) file lists. Examples::
145
145
146 [c:/ipython]|9> mglob *.py
146 [c:/ipython]|9> mglob *.py
147 [c:/ipython]|10> mglob *.py rec:*.txt
147 [c:/ipython]|10> mglob *.py rec:*.txt
148 [c:/ipython]|19> workfiles = %mglob !.svn/ !.hg/ !*_Data/ !*.bak rec:.
148 [c:/ipython]|19> workfiles = %mglob !.svn/ !.hg/ !*_Data/ !*.bak rec:.
149
149
150 Note that the first 2 calls will put the file list in result history (_, _9, _10), and the last one will assign it to 'workfiles'.
150 Note that the first 2 calls will put the file list in result history (_, _9, _10), and the last one will assign it to 'workfiles'.
151
151
152
152
153 Prompt customization
153 Prompt customization
154 ====================
154 ====================
155
155
156 The sh profile uses the following prompt configurations::
156 The sh profile uses the following prompt configurations::
157
157
158 o.PromptManager.in_template= r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#>'
158 c.PromptManager.in_template = r'{color.LightGreen}\u@\h{color.LightBlue}[{color.LightCyan}\Y1{color.LightBlue}]{color.Green}|\#> '
159 o.PromptManager.in2_template= r'\C_Green|\C_LightGreen\D\C_Green>'
159 c.PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
160 c.PromptManager.out_template = r'<\#> '
160
161
161 You can change the prompt configuration to your liking by editing
162 You can change the prompt configuration to your liking by editing
162 ipython_config.py.
163 ipython_config.py.
163
164
164 .. _string_lists:
165 .. _string_lists:
165
166
166 String lists
167 String lists
167 ============
168 ============
168
169
169 String lists (IPython.utils.text.SList) are handy way to process output
170 String lists (IPython.utils.text.SList) are handy way to process output
170 from system commands. They are produced by ``var = !cmd`` syntax.
171 from system commands. They are produced by ``var = !cmd`` syntax.
171
172
172 First, we acquire the output of 'ls -l'::
173 First, we acquire the output of 'ls -l'::
173
174
174 [Q:doc/examples]|2> lines = !ls -l
175 [Q:doc/examples]|2> lines = !ls -l
175 ==
176 ==
176 ['total 23',
177 ['total 23',
177 '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py',
178 '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py',
178 '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py',
179 '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py',
179 '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py',
180 '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py',
180 '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py',
181 '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py',
181 '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py',
182 '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py',
182 '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py',
183 '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py',
183 '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc']
184 '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc']
184
185
185 Now, let's take a look at the contents of 'lines' (the first number is
186 Now, let's take a look at the contents of 'lines' (the first number is
186 the list element number)::
187 the list element number)::
187
188
188 [Q:doc/examples]|3> lines
189 [Q:doc/examples]|3> lines
189 <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
190 <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
190
191
191 0: total 23
192 0: total 23
192 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
193 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
193 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py
194 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py
194 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py
195 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py
195 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
196 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
196 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
197 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
197 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
198 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
198 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
199 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
199
200
200 Now, let's filter out the 'embed' lines::
201 Now, let's filter out the 'embed' lines::
201
202
202 [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1)
203 [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1)
203 [Q:doc/examples]|5> l2
204 [Q:doc/examples]|5> l2
204 <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
205 <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
205
206
206 0: total 23
207 0: total 23
207 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
208 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py
208 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
209 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py
209 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
210 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py
210 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
211 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py
211 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
212 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc
212
213
213 Now, we want strings having just file names and permissions::
214 Now, we want strings having just file names and permissions::
214
215
215 [Q:doc/examples]|6> l2.fields(8,0)
216 [Q:doc/examples]|6> l2.fields(8,0)
216 <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
217 <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value:
217
218
218 0: total
219 0: total
219 1: example-demo.py -rw-rw-rw-
220 1: example-demo.py -rw-rw-rw-
220 2: example-gnuplot.py -rwxrwxrwx
221 2: example-gnuplot.py -rwxrwxrwx
221 3: extension.py -rwxrwxrwx
222 3: extension.py -rwxrwxrwx
222 4: seteditor.py -rwxrwxrwx
223 4: seteditor.py -rwxrwxrwx
223 5: seteditor.pyc -rwxrwxrwx
224 5: seteditor.pyc -rwxrwxrwx
224
225
225 Note how the line with 'total' does not raise IndexError.
226 Note how the line with 'total' does not raise IndexError.
226
227
227 If you want to split these (yielding lists), call fields() without
228 If you want to split these (yielding lists), call fields() without
228 arguments::
229 arguments::
229
230
230 [Q:doc/examples]|7> _.fields()
231 [Q:doc/examples]|7> _.fields()
231 <7>
232 <7>
232 [['total'],
233 [['total'],
233 ['example-demo.py', '-rw-rw-rw-'],
234 ['example-demo.py', '-rw-rw-rw-'],
234 ['example-gnuplot.py', '-rwxrwxrwx'],
235 ['example-gnuplot.py', '-rwxrwxrwx'],
235 ['extension.py', '-rwxrwxrwx'],
236 ['extension.py', '-rwxrwxrwx'],
236 ['seteditor.py', '-rwxrwxrwx'],
237 ['seteditor.py', '-rwxrwxrwx'],
237 ['seteditor.pyc', '-rwxrwxrwx']]
238 ['seteditor.pyc', '-rwxrwxrwx']]
238
239
239 If you want to pass these separated with spaces to a command (typical
240 If you want to pass these separated with spaces to a command (typical
240 for lists if files), use the .s property::
241 for lists if files), use the .s property::
241
242
242
243
243 [Q:doc/examples]|13> files = l2.fields(8).s
244 [Q:doc/examples]|13> files = l2.fields(8).s
244 [Q:doc/examples]|14> files
245 [Q:doc/examples]|14> files
245 <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc'
246 <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc'
246 [Q:doc/examples]|15> ls $files
247 [Q:doc/examples]|15> ls $files
247 example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc
248 example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc
248
249
249 SLists are inherited from normal python lists, so every list method is
250 SLists are inherited from normal python lists, so every list method is
250 available::
251 available::
251
252
252 [Q:doc/examples]|21> lines.append('hey')
253 [Q:doc/examples]|21> lines.append('hey')
253
254
254
255
255 Real world example: remove all files outside version control
256 Real world example: remove all files outside version control
256 ------------------------------------------------------------
257 ------------------------------------------------------------
257
258
258 First, capture output of "hg status"::
259 First, capture output of "hg status"::
259
260
260 [Q:/ipython]|28> out = !hg status
261 [Q:/ipython]|28> out = !hg status
261 ==
262 ==
262 ['M IPython\\extensions\\ipy_kitcfg.py',
263 ['M IPython\\extensions\\ipy_kitcfg.py',
263 'M IPython\\extensions\\ipy_rehashdir.py',
264 'M IPython\\extensions\\ipy_rehashdir.py',
264 ...
265 ...
265 '? build\\lib\\IPython\\Debugger.py',
266 '? build\\lib\\IPython\\Debugger.py',
266 '? build\\lib\\IPython\\extensions\\InterpreterExec.py',
267 '? build\\lib\\IPython\\extensions\\InterpreterExec.py',
267 '? build\\lib\\IPython\\extensions\\InterpreterPasteInput.py',
268 '? build\\lib\\IPython\\extensions\\InterpreterPasteInput.py',
268 ...
269 ...
269
270
270 (lines starting with ? are not under version control).
271 (lines starting with ? are not under version control).
271
272
272 ::
273 ::
273
274
274 [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1)
275 [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1)
275 [Q:/ipython]|36> junk
276 [Q:/ipython]|36> junk
276 <36> SList (.p, .n, .l, .s, .grep(), .fields() availab
277 <36> SList (.p, .n, .l, .s, .grep(), .fields() availab
277 ...
278 ...
278 10: build\bdist.win32\winexe\temp\_ctypes.py
279 10: build\bdist.win32\winexe\temp\_ctypes.py
279 11: build\bdist.win32\winexe\temp\_hashlib.py
280 11: build\bdist.win32\winexe\temp\_hashlib.py
280 12: build\bdist.win32\winexe\temp\_socket.py
281 12: build\bdist.win32\winexe\temp\_socket.py
281
282
282 Now we can just remove these files by doing 'rm $junk.s'.
283 Now we can just remove these files by doing 'rm $junk.s'.
283
284
284 The .s, .n, .p properties
285 The .s, .n, .p properties
285 -------------------------
286 -------------------------
286
287
287 The ``.s`` property returns one string where lines are separated by
288 The ``.s`` property returns one string where lines are separated by
288 single space (for convenient passing to system commands). The ``.n``
289 single space (for convenient passing to system commands). The ``.n``
289 property return one string where the lines are separated by a newline
290 property return one string where the lines are separated by a newline
290 (i.e. the original output of the function). If the items in string
291 (i.e. the original output of the function). If the items in string
291 list are file names, ``.p`` can be used to get a list of "path" objects
292 list are file names, ``.p`` can be used to get a list of "path" objects
292 for convenient file manipulation.
293 for convenient file manipulation.
293
294
General Comments 0
You need to be logged in to leave comments. Login now