Is there a way to open a file with the default program using command-line instead?
This is a job for xdg-open
:
xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs.
xdg-open comes pre-installed in Ubuntu.
Alternatively, is there a way, given a file extension, to determine a command to open it?
By the extension, no. The default application is chosen based upon the MIME type, not the extension. Linux does not have "extensions".
For a specific file, you can use xdg-mime
to determine both the file's MIME type and the default application associated to it:
Syntax for displaying a file's MIME type:
xdg-mime query filetype {file}
Syntax for displaying MIME type's default application:
xdg-mime query default {mimetype}
Syntax for displaying a file's default application:
xdg-mime query default "$(xdg-mime query filetype {file})"
As a (hacky) workaround you could use the command
xdg-mime query default \
`xdg-mime query filetype "$(find ~ / -iname '*.png' -print -quit)"`
to display the default application for, e.g., PNG images.
This will work if and only if you have a PNG image on your computer and the first found file ending with .png
is a valid PNG image.
I figured out a hacky workaround, but I would still like to get the answers to my questions above so we can find a proper/less hacky solution.
Beginner-friendly step-by-step below.
Create a dummy .cmd file and set it as the always-open-with app for an extension using the GUI. This causes Windows to create a new file type for the dummy .cmd "app" and associate it with the extension by setting the UserChoice
key ProdId
value (ex: Application/customJPEG.cmd
). Repeat this process for each extension using a uniquely named dummy .cmd file and, bam, we have unique file types associated with each extension (ex: Application/customJPEG.cmd
, Application/customPNG.cmd
, etc.). We can then customize the icon and open/edit commands independently for each new file type.
See my flowcart in the original question for more information about how Windows chooses the icon for a file.
Note: This workaround is only required for protected extensions like .jpg and .png. See my question above for solutions to non-protected extensions. However, this workaround should work for any extension regardless.
Quick vocab:
- File extension: What comes after the final dot in a filename. .jpg, .jpeg, .png, etc. are all different file extensions.
- File type: A file classification/category. May be associated with one or more file extensions. For example, the JPEG file type may be associated with both the .jpg and .jpeg file extensions.
Note: Image file extensions like .jpg and .png show thumbnails of the image. To see the icon, either create an empty file (so a thumbnail can't be generated) or open an Explorer window and set the View to Details.
Note: You won't see anything change until you refresh the desktop. Make sure to do this after making changes to the registry. See step 6 for details.
Note: If a key seems to be missing in the registry, try refreshing by pressing F5 or View->Refresh. regedit does not stay up to date and requires a refresh to see the latest.
Repeat the steps below for each file extension. Replace .jpg with your file extension and Photoshop with whatever app you want.
1. Create a dummy .cmd file
- Open Notepad
- You can leave the file empty
- File->Save As
- Use something like
customJEPG.cmd
as the filename and save
It doesn't matter where you save it or what you name it expect the name must be different for each file type. The name of the dummy .cmd will determine the name of the file type. I recommend including the extension or file type in the name so it is easy to distinguish later.
2. Set the dummy .cmd file as the always-open-with app
- Right-click a .jpg file
- Open with->Choose another app
- Make sure "Always use this app to open .jpg files" is checked
- Scroll to the bottom and click "More apps ↓"
- Scroll to the bottom again and click "Look for another app on this PC"
- Browse to and choose your dummy .cmd file
- Ignore and close the "This app can't run on your PC" error
- If the "How do you want to open this file?" window pops up again, make sure your dummy .cmd file is still selected under "Keep using this app" at the top, check "Always use this app to open .jpg files", and hit OK.
If you want multiple file extensions to share the same file type, repeat steps 1-7 above for each extension.
3. Get the new associated file type
- Open regedit
- Navigate to:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jpg\UserChoice
- Copy/write down the value of
ProgId
. This is the file type that is now associated with the .jpg extension
The file type should be Applications\{the name of your dummy .cmd}
. Example: Applications\customJPEG.cmd
.
4. Update the file type to use the correct icon
- In regedit, navigate to
HKEY_CLASSES_ROOT\
- Now open the key with the same name as your file type. For example, if your file type is
Applications\customJEPG.cmd
, You will open:
HKEY_CLASSES_ROOT\Applications\customJEPG.cmd
- Create a new key inside named
DefaultIcon
- Set the (Default) value of the
DefaultIcon
key to a path to the icon you want to use. Example:
E:\Tools\Adobe\Adobe Photoshop CC 2015\Photoshop.exe,30
5. Update the file type to open and edit with Photoshop
- Open the
shell\open\command
key inside your file type key. Example:
HKEY_CLASSES_ROOT\Applications\customJEPG.cmd\shell\open\command
- Edit the (Default) value so that it points to your Photoshop.exe instead of your dummy .cmd file. Example:
"E:\Tools\Adobe\Adobe Photoshop CC 2015\Photoshop.exe" "%1"
- Create the
edit\command
key inside the shell
key if it does not exist. Example:
HKEY_CLASSES_ROOT\Applications\customJEPG.cmd\shell\edit\command
- Edit the (Default) value so it has the same value as your
open\command
key. Example:
"E:\Tools\Adobe\Adobe Photoshop CC 2015\Photoshop.exe" "%1"
6. Refresh the desktop
You will not see anything change until you refresh the desktop. There are 3 ways to do so:
7. Test it out
- The correct icons should now be shown for .jpg files.
- Double clicking a .jpg file should open it in Photoshop.
- Right-click a .jpg file->Edit should open it in Photoshop.
If the "How do you want to open this file?" window pops up again, make sure your dummy .cmd file is still selected under "Keep using this app" at the top, check "Always use this app to open .jpg files", and hit OK.
8. Delete the dummy .cmd
You can delete your dummy .cmd file. This is optional.
A note about finding icons
Often, an app's .exe will contain multiple icons. For example, the Photoshop.exe contains all the icons for all the different file types it supports. Here's how to find and use these icons:
- Create a shortcut to the .exe by right-clicking and selecting Create shortcut
- Right-click the shortcut and select Properties
- Click the Change Icon button near the bottom
- You will now see a window like this (but without the numbers):
- The icons are numbered starting from 0. Find the number of the icon you want to use. Instead of counting, you can use the formula
(column - 1) * 4 + row - 1
. For example, if the icon is in column 6, row 3, (6 - 1) * 4 + 3 - 1 = 22
- Now you can reference that icon by including the number in the path to the .exe like so:
C:\Example\Photoshop.exe,23
Best Answer
Why does my Windows open CSS file with notepad?
Right click on the CSS file and click "Open With" > "Choose default program..."
You will see the the default program is set to "Notepad":
You can use this dialog to change the default application if you wish to do so.
In the registry the key
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.css
has valuePerceivedType
set totext
:Warning:
Notepad is the default application for opening text files.
You can open the css file in a
cmd
shell by typing it's name and it will also open in notepad:Why Notepad is the default application for opening text files?
Notepad is registered as the command to be executed for files of perceived type text.
To change the command to
vim
edit the registry values described below and replace%SystemRoot%\system32\NOTEPAD.EXE
with the path tovim.exe
.Source Application Registration