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