How do I set sublime text to auto detect a file type after setting it once? - sublimetext3

for instance I have a .zsh file that I would like to always open in sublime as a "Shell Script (bash)" file type. Currently it defaults back to a text file format even when I change and reopen it.

Look in the bottom right of the window.
Click the file type name. Let's assume it is 'Shell Script (Bash)'.
Notice that the first option is 'Open all with current extension as...'
Follow the obvious steps from there and you should be all set.

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

Opening a random file only knowing the extension name anywhere on the computer in python [duplicate]

I want to do the following:
Save numeric data in a CSV-like formatting, with a ".foo" extension;
Associate the ".foo" file extension with some python script, which in turns opens the .foo file, reads its content, and plots something with a plotting library (matplotlib most probably).
The use-case would be: double-click the file, and its respective plot pops up right away.
I wonder how I should write a python script in order to do that.
Besides, the windows "open with" dialog only allows me to choose executables (*.exe). If I choose "fooOpener.py", it doesn't work.
This isn't really a programming question, but what you need to do is to figure out how to get the Python executable into the registry key that opens your data file.
For example, I created a little Python script called opener.py that looks like this:
import sys
print(sys.argv)
input()
Then I created a testfile.foo and used the "change" button in that file's property dialog to choose opener.py. (You can do this if you click Browse and change the Open With dialog's file filter to "All Files".)
Of course this didn't work (as you noticed). So I opened regedit and searched for opener.py and found it at the following registry key:
HKEY_CURRENT_USER\Software\Classes\Applications\opener.py\shell\open\command
The default value of this key was "C:\opener.py" %1. I changed it to python "C:\opener.py" %1. It worked!
Long story short, to do this properly you need to custom-edit the registry. Actually setting up the file association is more complex than just editing that one key (you have to also indicate that .foo is associated with opener.py).
An alternative approach would be to turn your Python script into a standalone executable using one of the several tools available for that purpose, or write a small executable in C that launches the script.
press the windows key
type cmd
right click the result and choose "run as administrator"
assoc .foo=foofile
ftype foofile="C:\Users\<user>\AppData\Local\Programs\Python\PYTHON~1\python.exe" "C:\<whatever>\fooOpener.py" "%1" %*
Use pythonw.exe if it's a .pyw file (to prevent a cmd window from spawning).
If you want to use an existing file type, you can find its alias by not assigning anything. For example, assoc .txt returns .txt=txtfile.
instead of editing registry, you can create batch file
opener.cmd
with content
python "C:\<whatever>\fooOpener.py" %1
and associate extension with this file.
It works!

Sublimetext file extension annoyance

If I create a brand new sublimetext buffer, then set the syntax of the buffer to something (in this example json).
When I go to save the file, it will have untitled.json in the file save dialog automatically. If I then change that to foo.bar and hit save. The file saved to my filesystem will be foo.bar.json.
Why? My intentions as a user are clear in this case, I'm overriding the file extension for my own reasons.
Is there a way to globally set sublimetext to stop appending what it assumes is the proper extensions to files? Any number of google searches has turned up nothing.

How to enable sublime text to take first line as file name while saving?

Earlier the Sublime used to take first line as file name by default but now it's "untitled". Is there a way to enable it or is there a plugin for that?
Thanks
The first line is only used as the file name for unsaved files when the syntax is set to Plain Text. As soon as you change the syntax highlighting and type something, it will change the tab name to "untitled".
The implementation for this is in the Default package, set_unsaved_view_name.py file. To get it to work for all syntaxes:
Install PackageResourceViewer through Package Control if it is not already installed
Open Command Palette
Type PRV: and select PackageResourceViewer: Open Resource
Select Default
Select set_unsaved_view_name.py
Find if syntax != 'Packages/Text/Plain text.tmLanguage':
Select from there to the end of the if statement (the first return statement) (Python is indentation based) inclusive return to be commented out.
Go to the Edit menu -> Comment -> Toggle Comment
Save the file
Ensure that, in your preferences (user, syntax specific etc.), set_unsaved_view_name is not set to false
Note: these instructions are valid as at ST build 3131, and the implementation could change in future builds.

Sublime Text 3 specific file type settings only has JSON file settings. How to create new?

I'm trying to create a syntax specific settings for file types in ST3. As per the documentation I am supposed to find what I need in
Sublime Text>Preferences>Settings - More>Syntax Specific - User
However, when I click that I only get JSON.sublime-settings file. What if I want some other settings file?
The settings file that opens when you select Prefereces > Settings - Syntax Specific is sensitive to the type of file that you're currently editing. So, if you happen to be in a JSON file when you invoke the menu command, you get the file for settings specific to JSON.
In order to get at the setting specific to a different syntax, open a file of that type first, or create an empty buffer and set the syntax to the desired language via View > Syntax from the menu, the command palette, or the menu that pops up when you click the file type in the right side of the status bar (where it will say Plain Text).
The file that you actually want to save is SyntaxName.sublime-settings in your User package, e.g. Python.sublime-settings for Python, etc. However to forestall any problems with the filename (like incorrect case or spelling) it's generally better to do it as above instead, particularly since the name of the syntax can sometimes be non-obvious.

Resources