Make Emacs totally portable under Windows

Franco Pasut
Jul 19, 2019 · 5 min read
Image for post
Image for post
Figure 1: Emacs + USB Pendrive = Emacs Portable

Emacs for Windows is natively portable, but not too much

, it is simply a work of art in computer engineering.

Although native to the Unix world, Emacs is also available in Windows version and is already distributed in portable mode, ie without the need for the typical installation within the operating system.

However, the portability of Emacs for Windows is limited, in the absence of specific configuration, to the basic version. As soon as you try to customize the program or install some extension, Emacs adds traces of code where the home of the operating system is located, which is usually located on the hard disk, ie in the traditional Disco C / computer, resulting in loss of /portability.

The fundamental principle of portability is, in fact, the total separation between the programs on the external device and the operating system of the computer used, from time to time, as host.

To make Emacs totally portable under Windows is not, therefore, enough to extract the program in a /pendrive USB /, you must also change the configuration to make it always point to the inside of the key itself.

In the network there are various solutions, but some do not completely pass the sieve of absolute portability that, in Windows, is made a little more complicated by the variable attribution of letters to external drives in the various machines where the key could be inserted.

In other words, the same USB stick could be found with the letter E, F, G… in different computers and this would make it impossible to create a launch system valid for all, unless… this is exactly the subject of the article you’re reading.

A basic solution: create a batch file inside the bin folder

An interesting solution is described in

Here is the step-by-step procedure you have tried yourself:

  1. Create an external disk folder named emacshome
  2. Create a .bat file named runemacs.bat in the subfolder bin of Emacs
  3. In the .bat file, enter the following code:

set HOME=%~dp0..\..\emacshome "%~dp0runemacs.exe" %*

At this point you just need to launch Emacs from the .bat file instead of the relative .exe and home will always be inside the same device, even in the case of a change in the reference letter assigned by the system.

However, this is a basic solution for a very simple reason: it forces you to search for the drive letter assigned to the stick, to open the /bin/ subfolder, to scroll through the list of files to runemacs.bat and, only after the previous steps, to launch the batch file.

This is certainly not a quick solution!

Nor is it enough to create a link to the batch file from the root directory of the stick as we cannot know in advance what letter will be assigned to our stick in the different computers in which it might be inserted.

It is necessary, therefore, a solution drive letter free, that is to say that allows Launch the batch file with any letter associated with the key.

Second part of the solution: launch system valid for any drive letter associated with the key

A first solution, not very efficient, is to use another batch at the root of the stick to call the batch created within the bin.

If Emacs is in a folder called emacs-26.2-i686, then the batch (which we could call, for example, run.bat) located at the root of the stick that calls the other batch located inside the folder bin could have the following text content:

/emacs-26.2-i686/bin/runemacs.bat

The file batch located at the root of the pendrive would launch the other batch located inside the folder bin.

However, this solution involves switching between two separate batches and each step in the batch slows down the call of the final executable (remember that the final executable is runemacs.exe).

A much more efficient solution is to modify the batch file inside the bin so as to directly launch the Emacs executable from the base of the pendrive without the need for a double pass between two different batch files.

Here is the solution, that is the modified runemacs to be used in the RADICE of the key with any letter associated with the drive (always with the assumption that the folder where Emacs is located is named emacs-26.2-i686):

set HOME=%~dp0emacshome
"%~dp0/emacs-26.2-i686/bin/runemacs.exe" %*

With this solution you get the launch of Emacs directly from the root (home) regardless of the drive letter assigned to the key with the simultaneous modification of the home inside the program itself.

If you upgrade to another version of Emacs you only need to change the folder name in the second line.

It’s not over: we can also create a Documents folder valid for all drive letters

After configuring a folder home inside the key and neutralizing the letters assigned to the key itself, we can use this same configuration to create a folder /Documents / having the same characteristics.

Just create the Documents folder below the root folder as configured above: i.e. ~/Documents (note the tilde ~ which symbolically represents the home).

The drive-free folder can be used, for example, to configure the monitoring of Org-Mode by entering the following text string in .emacs:

(setq org-agenda-files '("~/Documents"))

In this way the folder Documents will be monitored by the commands of Org-Agenda and its contents can be directly recalled, for example, with the command C-c a t.

32bit version for all computers

As a last touch to have a system with the widest portability it is advisable to use the 32bit version of Emacs.

The 32-bit Windows operating system is still widespread, especially in laptops, and the use of the 32-bit version of the program allows it to be launched in both 32-bit and 64-bit operating systems while the 64-bit version is not backwards compatible.

Thank you for your attention.

Originally published at https://francopasut-en.blogspot.com on July 19, 2019.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store