Change the current working directory. This command automatically maintains an internal list of directories you visit during your IPython session, in the variable ``_dh``. The command :magic:`%dhist` shows this history nicely formatted. You can also do ``cd - `` t
(self, parameter_s='')
| 297 | @skip_doctest |
| 298 | @line_magic |
| 299 | def cd(self, parameter_s=''): |
| 300 | """Change the current working directory. |
| 301 | |
| 302 | This command automatically maintains an internal list of directories |
| 303 | you visit during your IPython session, in the variable ``_dh``. The |
| 304 | command :magic:`%dhist` shows this history nicely formatted. You can |
| 305 | also do ``cd -<tab>`` to see directory history conveniently. |
| 306 | Usage: |
| 307 | |
| 308 | - ``cd 'dir'``: changes to directory 'dir'. |
| 309 | - ``cd -``: changes to the last visited directory. |
| 310 | - ``cd -<n>``: changes to the n-th directory in the directory history. |
| 311 | - ``cd --foo``: change to directory that matches 'foo' in history |
| 312 | - ``cd -b <bookmark_name>``: jump to a bookmark set by %bookmark |
| 313 | - Hitting a tab key after ``cd -b`` allows you to tab-complete |
| 314 | bookmark names. |
| 315 | |
| 316 | .. note:: |
| 317 | ``cd <bookmark_name>`` is enough if there is no directory |
| 318 | ``<bookmark_name>``, but a bookmark with the name exists. |
| 319 | |
| 320 | Options: |
| 321 | |
| 322 | -q Be quiet. Do not print the working directory after the |
| 323 | cd command is executed. By default IPython's cd |
| 324 | command does print this directory, since the default |
| 325 | prompts do not display path information. |
| 326 | |
| 327 | .. note:: |
| 328 | Note that ``!cd`` doesn't work for this purpose because the shell |
| 329 | where ``!command`` runs is immediately discarded after executing |
| 330 | 'command'. |
| 331 | |
| 332 | Examples |
| 333 | -------- |
| 334 | :: |
| 335 | |
| 336 | In [10]: cd parent/child |
| 337 | /home/tsuser/parent/child |
| 338 | """ |
| 339 | |
| 340 | try: |
| 341 | oldcwd = os.getcwd() |
| 342 | except FileNotFoundError: |
| 343 | # Happens if the CWD has been deleted. |
| 344 | oldcwd = None |
| 345 | |
| 346 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
| 347 | # jump in directory history by number |
| 348 | if numcd: |
| 349 | nn = int(numcd.group(2)) |
| 350 | try: |
| 351 | ps = self.shell.user_ns['_dh'][nn] |
| 352 | except IndexError: |
| 353 | print('The requested directory does not exist in history.') |
| 354 | return |
| 355 | else: |
| 356 | opts = {} |