Emacs function to add new path elements to the $PATH environment variable

A very simple eLisp function to add new path elements to the PATH environment variable. Very useful for adding new comint executables from within .emacs/init.el files.

(defun my-add-path (path-element)
 "Add the specified path element to the Emacs PATH"
  (interactive "DEnter directory to be added to path: ")
  (if (file-directory-p path-element)
    (setenv "PATH"
       (concat (expand-file-name path-element)
               path-separator (getenv "PATH")))))
Advertisement

2 thoughts on “Emacs function to add new path elements to the $PATH environment variable

  1. I suggest this change:

    (or (getenv “PATH”) “”)

    because, in theory, PATH variable can be nonexistent. In practice it rarely is but CONCAT function expects sequence-type arguments and it should always get those even if GETENV returns nil.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.