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