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