I have installed Emacs, with all its init files, on a USB stick for use on different PCs. The init files contains paths to the USB stick itself. The problem is that the drive letter assigned for the USB stick changes from PC to PC. How can I make Emacs determine the drive letter assigned to my USB stick. store it into a variable that I can use in all paths pointing to the USB stick?

For instance my init file contains, among many others, the following line:

(load "K:/home/.emacs.d/init_calendar")

This will not function on a PC where the USB stick i assigned another letter, e.g. D:. So how can I make Emacs determine which drive it starts from?

EDIT: how can I make Emacs AUTOMATICALLY determine the letter of the drive it starts from? If my usb is assigned letter x on one pc I want emacs to find this letter, and store it in a variable.

  • 1
    Maybe it's contained in invocation-directory?
    – npostavs
    Sep 17, 2017 at 7:49
  • Try writing a batch script that sets the HOME variable to the value of %CD% (or add some directory on top of that) and invoke emacs. That way, ~ will point to whatever you set to your HOME environment variable.
    – user12563
    Sep 17, 2017 at 15:04
  • Many people have suggested looking at the variable invocation-directory. Did that work for you? It seems to work on my windows machine, giving "c:/Users/Me/Emacs-25.2/bin/", the first two characters of which are the drive.
    – Omar
    Oct 20, 2017 at 1:24

1 Answer 1


If you use Dired+ then you can use command diredp-w32-list-mapped-drives, to list your current MS Windows drives.

If you want to roll your own, just use shell command net use. This is all there is to command diredp-w32-list-mapped-drives:

(defun diredp-w32-list-mapped-drives ()
  "List network connection information for shared MS Windows resources.
This just invokes the Windows `NET USE' command."
  (shell-command "net use")
  (display-buffer "*Shell Command Output*"))

Update, after your comment:

If you are asking only how to assign a variable to a file/directory, then the answer is just to use defvar or setq:

(setq my-var "K:/home/.emacs.d/init_calendar")


(setq my-drive "K:")
(setq my-file (concat my-drive "/home/.emacs.d/init_calendar"))

It's not very clear (to me) what you are asking.

At what point do you set the variable? You seemed to say that you don't know what drive letter a USB stick will be given when plugged into this or that machine in this or that context.

If so, then you get that information only after you plug it in, and you get it by using shell command net use (see above). You can, at that point, assign any variable you want to pick up that drive letter.

  • Thanks for your reply. Maybe I misunderstand you, but I need a variable for the drive letter defined already in the first line of my init file, if not I cannot load lines like the one I used in my example. Doesn't your solution give me the drive letter after all packages are installed? Or where is your solution defining av variable I can use for the drive letter?
    – myotis
    Sep 16, 2017 at 21:50
  • I can of course plug in my usb stick, then in Windows Explorer visually check which drive letter is assigned to the stick, then open my init file with notepad, set the drive letter by (setq my-drive "K:"), save and close, and then open emacs with a init file with all pointers correctly set. But this is unpractical. I want emacs itself to find out which drive the usb stick is assigned to, and set the correct paths for where on the stick to install files from.
    – myotis
    Sep 16, 2017 at 22:46
  • I have found som code on the net, but I am not sure if they can be used to automatically extract the usb drive letter: (defvar usb-drive-letter (substring data-directory 0 3)) (defvar usb-home-dir (concat usb-drive-letter "home/"))
    – myotis
    Sep 16, 2017 at 22:48
  • I see my unclearness. What I want is that Emacs automatically determine which drive letter it is started from.
    – myotis
    Sep 17, 2017 at 11:24
  • When you say "started from", just what do you mean: invocation-directory? default-directory? Using such info should give you the info you need. After Emacs is started, it can tell you what the current directory is, which gives you the drive letter as well.
    – Drew
    Sep 17, 2017 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.