Function: cd

cd is an interactive and byte-compiled function defined in files.el.gz.

Signature

(cd DIR)

Documentation

Make DIR become the current buffer's default directory.

If your environment includes a CDPATH variable, try each one of that list of directories (separated by occurrences of path-separator(var)/path-separator(fun)) when resolving a relative directory name. The path separator is colon in GNU and GNU-like systems.

View in manual

Probably introduced at or before Emacs version 17.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun cd (dir)
  "Make DIR become the current buffer's default directory.
If your environment includes a `CDPATH' variable, try each one of
that list of directories (separated by occurrences of
`path-separator') when resolving a relative directory name.
The path separator is colon in GNU and GNU-like systems."
  (interactive
   (list
    ;; FIXME: There's a subtle bug in the completion below.  Seems linked
    ;; to a fundamental difficulty of implementing `predicate' correctly.
    ;; The manifestation is that TAB may list non-directories in the case where
    ;; those files also correspond to valid directories (if your cd-path is (A/
    ;; B/) and you have A/a a file and B/a a directory, then both `a' and `a/'
    ;; will be listed as valid completions).
    ;; This is because `a' (listed because of A/a) is indeed a valid choice
    ;; (which will lead to the use of B/a).
    (minibuffer-with-setup-hook
        (lambda ()
          (setq-local minibuffer-completion-table
		      (apply-partially #'locate-file-completion-table
				       cd-path nil))
          (setq-local minibuffer-completion-predicate
		      (lambda (dir)
			(locate-file dir cd-path nil
				     (lambda (f) (and (file-directory-p f) 'dir-ok))))))
      (unless cd-path
        (setq cd-path (or (parse-colon-path (getenv "CDPATH"))
                          (list "./"))))
      (read-directory-name "Change default directory: "
                           default-directory default-directory
                           t))))
  (unless cd-path
    (setq cd-path (or (parse-colon-path (getenv "CDPATH"))
                      (list "./"))))
  (cd-absolute
   (or
    ;; locate-file doesn't support remote file names, so detect them
    ;; and support them here by hand.
    (and (file-remote-p (expand-file-name dir))
         (file-accessible-directory-p (expand-file-name dir))
         (expand-file-name dir))
    (locate-file dir cd-path nil
                 (lambda (f) (and (file-directory-p f) 'dir-ok)))
    (if (getenv "CDPATH")
        (error "No such directory found via CDPATH environment variable: %s" dir)
      (error "No such directory: %s" dir)))))