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