# EmacsMsWindowsIntegration

## Run Emacs from a cmd' Window

Put the Emacs ‘bin’ folder in the Windows PATH environment variable (right-click Computer, Properties, Advanced, Environment Variables). This also simplifies other methods mentioned on this page (no explicit paths needed then).

## Create a shortcut

Use this code for a desktop/task-bar shortcut. Right-click in a file folder, select New → Shortcut, and enter:

c:\path\to\emacs\bin\emacsclientw.exe -c -n -a runemacs.exe


This shortcut will connect to Emacs daemon if it is running, otherwise opens a new frame.

  -c tells emacs to create a new frame instead of trying to use an existing frame.
-n means no wait - don't wait for the server to return
-a EDITOR specifies to the emacsclientw.exe which editor to run if emacs.exe is not running. An empty string starts a new server if needed

This may not work correctly on Windows 7. See MsWindowsSevenProblems

You may want to add a command to “Open in Emacs” when you right click on files in Explorer. There are several solutions to that which you can find here: MsWindowsGlobalContextMenu

## Create File Associations

NOTE: After following the instructions below, emacs client doesn’t work properly. For example, clicking on a .org file will create a new emacs window, even when one is already running (Windows 10, July 2019, Emacs 26.2).

To open text files with the emacs daemon, run cmd.exe as administrator use the commands and ‘ftype’ and ‘assoc’:

    ftype txtfile=emacsclientw -na runemacs "%1"
ftype EmacsLisp=emacsclientw -na runemacs "%1"
ftype CodeFile=emacsclientw -na runemacs "%1"
assoc .txt=txtfile
assoc .text=txtfile
assoc .log=txtfile
assoc .org=txtfile
assoc .el=EmacsLisp
assoc .c=CodeFile
assoc .h=CodeFile

## Use emacsclient' to edit git commits

This seems to works:

  git config --global core.editor "C:/path/to/emacsclientw.exe --alternate-editor='C:/path/to/runemacs.exe' '%*'"


Be sure not to add -n to the command here. A non-blocking editor call will cause Git to fail, since it thinks you entered an empty commit message.

## Replace notepad.exe' calls

A quick and dirty way to replace all notepad.exe calls from any application (and your file manager) would be to use the Registry:

1. Start regedit.exe

2. Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion?\Image File Execution Options

3. Add a new key named notepad.exe

4. Add (in there) a new string named Debugger and use this value: emacsclientw.exe -na runemacs.exe -F (requires emacsclientw to be in your PATH)

Note that this will pretty much break the usual -F behavior but it is needed as emacsclientw has no -z parameter (yet).

## Workaround to get term'/ansi-term' working with cmd.exe' or Cygwin bash

Check out fakecygpty. This is your best bet to get ‘term’/‘ansi-term’ working with a proper shell on Windows with Win32 Emacs.

You will need Cygwin installed to build it.

## Run Emacs server automatically at windows startup

The script creates a .bat file in your shell:startup folder. It also adds a line to delete any old emacs server data that may not have gotten cleaned, if the previous shutdown was dirty. Tested on Windows 10.

# Path to startup file
$startup_file = "$([Environment]::GetFolderPath('Startup'))\StartEmacsServer.bat"

# Introduce variable HOME:
# 1. for simplicity
# 2. runemacs.exe expects it to be
#
# %APPDATA% is chosen because runemacs.exe operates there
#	https://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html#Find-Init
#	https://www.gnu.org/software/emacs/manual/html_node/emacs/General-Variables.html#General-Variables
New-Item $startup_file -type file Set-Content -Path$startup_file -Value "set HOME=%APPDATA%"

# Clean previous server file info first
Add-Content -Path $startup_file -Value "del /Q ""%HOME%/.emacs.d/server/*""" Add-Content -Path$startup_file -Value "runemacs.exe --daemon"



You can also open Explorer, type in shell:startup in the address bar, and press enter. In this startup folder, create a shortcut by right clicking and selecting new->shortcut. When asked for location, put the following: "X:\path\to\emacs\bin\runemacs.exe" --daemon where you have substituted the proper path to runemacs.exe. This shortcut will run at startup. You can double click on the shortcut to start the Emacs server if it is not already running.

## Debug Version

Sometimes it is useful to be able to start up default autonomous copy of Emacs (not client/server), for example if you play with your config file and something gets messed up.

Create a new shortcut to “emacs.exe”. Right click newly created shortcut and choose properties. In target field type: "X:\path\to\emacs\bin\emacs.exe" --debug-init Close properties dialog.

CategoryWThirtyTwo