W Thirty Two File Associations (original) (raw)

Changing file associations from command line

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 set up Emacs file associations properly, use the commands ‘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

Changing file associations with registry keys

This is similar to the “assoc” commands above. By defining file “meta types”, e.g., "Emacs.AssocFile", and then hanging the file associations (e.g., “.org”) off of those.

With this method, for every associated file, you will be able to see Emacs listed under “Open with” option in context menu and in file properties.

Save below script in a file with “.reg” extension (eg. emacs_file-associations.reg). Remember to change all paths to match installation directory of your Emacs instance.

Windows Registry Editor Version 5.00 ;; ----- ;; Set up the handler ;; ----- [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile] @="Emacs Document" "FriendlyTypeName"="Emacs Document" "PerceivedType"="Document"

;; ----- ;; Set up the application details (ApplicationName will appear in "Open with" context menu) ;; ----- [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile\Application] @="" "ApplicationCompany"="Free Software Foundation, Inc." "ApplicationDescription"="Emacs — it might be thought of as a thermonuclear word processor." "ApplicationIcon"="C:\tools\emacs\bin\emacsclientw.exe,0" "ApplicationName"="Emacs"

;; ----- ;; Set up the icon ;; ----- [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile\DefaultIcon] @="C:\tools\emacs\bin\emacsclientw.exe,0"

;; ----- [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile\Shell] [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile\Shell\open] @="Open in Emacs"

;; ----- ;; Command responsible for opening Emacs (it will open file in the existing window, or try to create a new one) ;; If you want to always open files in new window, use this instead: ;; @="C:\tools\emacs\bin\emacsclientw.exe -n --alternate-editor="" -c -n "%1"" ;; ----- [HKEY_CURRENT_USER\Software\Classes\Emacs.AssocFile\Shell\open\command] @="C:\tools\emacs\bin\emacsclientw.exe -n --alternate-editor="" -n "%1""

;; ----- ;; Associate file types w/ Emacs (examples below) ;; ----- [HKEY_CURRENT_USER\Software\Classes.el\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.md\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.qmd\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.html\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.txt\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.log\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.c\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.h\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.js\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.json\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.toml\OpenWithProgids] "Emacs.AssocFile"="" [HKEY_CURRENT_USER\Software\Classes.css\OpenWithProgids] "Emacs.AssocFile"=""

For me it works immediately after running the script. If not, it should take effect after you restart your desktop Explorer instance, e.g., after rebooting or killing all ‘explorer.exe’ instances in Task Manager.

Changing file associations in Windows Explorer

Instead of the above or editing the registry keys directly you can in Windows Explorer from the menus choose

Tools - Folder Options

and then the tab “File Types”.

Make files with no extension be treated as text files

In the Command Prompt execute:

ASSOC .=txtfile

If that doesn’t work, then investigate the following registry keys. Possibly delete them and repeat the above step.

HKEY_CLASSES_ROOT\.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.

Open text files with EMACS

In the Command Prompt execute, after adapting the path of EmacsClient:

FTYPE txtfile="C:\Program Files\Emacs\bin\emacsclientw.exe" -n "%1"

Other ramblings

There are many ways and places the verbs are stored. HKEY_CLASSES_ROOT contains many of them (using various rules), but a few of the other places they may be found (according to my rummaging around) are:

[HKCU|HKLM]\Classes\Applications

[HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts

[HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder

[HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\Explorer\AppKey

[HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\Explorer\Associations

[HKCU|HKLM]\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\ContentTypeHandlers

… it appears almost everything under ::[HKCU|HKLM]\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer may be playing a role...

[HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\Extensions

[HKCU|HKLM]\Software\Microsoft\Windows NT\CurrentVersion\Extensions

they also may appear in .ini files (though not likely)

Not all of the the right-click entries are 'verbs'. I think they are called ShellExtensions, and might (i'm probably wrong) be accessible by using IContextMenu()::QueryContextMenu(), IContextMenu()::GetCommandString(), and IContextMenu()::InvokeCommand() [through some COM goo (which i don't understand)].


CategoryWThirtyTwo