Sublime Text Prompt to Save File - sublimetext2

I often will have a scratch pad in ST that I then close via Ctrl-F4. It always prompts me to save it, which I find to be a pain.
Is there a setting in ST where I can either change to default of this dialog to "Close without saving", or do not even prompt me at all if it is a new file (i.e. has no name).

You can set a tab to be a scratch buffer (doesn't prompt to save when closed). With the desired tab opened, open the console with Ctrl` and type:
view.set_scratch(True)
then hit Enter, and close the console with Esc. You can now close the tab whenever you want without being prompted. Of course, you can manually save the contents if you wish.
If you would like to have all new buffers set to scratch by default, you'll need a plugin. Create a new Python file in Sublime with the following contents:
import sublime
import sublime_plugin
class SetNewScratchBuffer(sublime_plugin.EventListener):
def on_new(self, view):
view.set_scratch(True)
def on_save(self, view):
view.set_scratch(False)
Save the file as Packages/User/set_new_scratch_buffer.py where Packages is the folder opened when selecting Preferences -> Browse Packages... (~/.config/sublime-text-2/Packages on Linux with ST2). Once saved, it should automatically become active, although you can restart Sublime to make sure. Now, all new buffers created with CtrlN or File -> New File will automatically have the scratch attribute set. This will be disabled when the file is saved, so you don't accidentally destroy changes to an opened file.

Related

How to open and save files

Is there a way to quickly open files in sublime text2?
At the moment I open files like this.
I open my terminal and enter
subl source/myproj/myfile.rs
Can I do this inside of sublime text2/3?
Also when I want to create a new file in source/myproj/ I usally press ctrl + n to create a new window and then I hit ctrl + s to save and name the file. The problem is when I hit ctrl + s my file system dialog opens and I have to use my mouse.
I think I saw someone using vim and he just could say source/myproj/mynewfile.rs and then the file would be created.
Is this possible in sublime text 2/3?
To quickly open a file in your current project, you can hit Ctrl+P and start writing the filename. This will sort through the files in your project and give you a little list, most likely the one you are looking for is pre selected, but if not, it is somewhere near the topmost options to select from, use arrow key down and highlight it, press enter and you just opened that file.
Another option to quickly create a file is using the AdvancedNewFile plugin.
When you hit Ctrl+n, it asks near the bottom of the window for a path and filename, once you enter it, it opens the new tab with the file already created, not an untitled file to later use system dialog to save to disk.

Sublime Text Default Save Options

Why when I save a file in Sublime Text 3 is the default save location the Sublime install directory and why is the default file type nothing?
I want to set the default save location to the Desktop and the default file type to .txt, how can I do this?
Here are my settings:
{
"font_size": 9,
"hot_exit": false,
"ignored_packages": ["Vintage"],
"remember_open_files": false
}
Without no line of code:
Preferences -> Settings -> paste this "default_dir": "your/favorite/path"
And you are done.
More here: https://sublime-text-unofficial-documentation.readthedocs.io/en/latest/reference/settings.html#file-and-directory-settings
Doesn't (currently) address the default extension issue, but you can also try AdvancedNewFile. Rather than creating an unnamed buffer, this plugin creates a named file. The default location is configurable, though there is no default extension.
Disclaimer: I'm the maintainer of the AdvancedNewFile plugin.
Edit
I've updated AdvancedNewFile to support default file extensions.
You can install the Default File Type plugin manually. It's a very simple plugin, and while the Package Control page says that it's for Sublime Text 2 only, I just installed it under OSX and it works fine. To install, navigate to %APPDATA%\Sublime Text 3\Packages and run
git clone https://github.com/spadgos/sublime-DefaultFileType.git DefaultFileType
Then, copy Packages\DefaultFileType\default_file_type.sublime-settings to Packages\User and change its contents to the following:
{
"default_new_file_syntax": "Packages/Text/Plain text.tmLanguage",
"use_current_file_syntax": false
}
Save the file, and now whenever you hit CtrlN to create a new file, it will be set to plain text. The plugin only works with the key combo, not via the File -> New File menu option.
As far as the save location goes, I have a theory, but I haven't been able to find documentation to back it up. At least on Win7 (for me), it seems like the default save location is the directory which contains the file that was open when you hit CtrlN or File -> New File to create the new file. For example, I had my Packages\User\Preferences.sublime-settings file open when I created a new file, and hitting CtrlS opened the Save dialog in Packages\User. I saved the file to the Desktop, hit CtrlN for a new file, entered something, then hit CtrlS and the Save dialog opened in the Desktop.
So, while there isn't a preferences setting for default save location, at least on Windows you can tweak it by always keeping a Desktop file open, then creating new files while that Desktop file is focused.
This should be a built-in option, honestly, but it seems fairly simple to automate yourself. Hit Tools -> New Plugin
Then paste this over the file that's created, hit save and call it "DefaultLanguage.py" or something:
import sublime, sublime_plugin
class EverythingIsPowerShell(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')
Of course, you can change the language from PowerShell to ... whatever you prefer. You just need the relative path to the tmLanguage. You can get that by opening a file in your favorite language and then open the console (View->Show Console) and type:
view.settings().get('syntax')

start opened file from sublimetext in associated program

i usually edit files in sublime text 2 that can also be edited and compiled with another program. As i have them already opened in sublimetext i do the following:
right click and choose "copy file path" (to clipboard)
Win+R to open windows run dialog
CTRL+V to paste the file path
hit enter to open the file with the associated program
i wonder some shortcut can be configured so it automatically starts the opened file with its associate program
thanks in advance
This can be done. I was in a very similar situation using Sublime as my editor of choice over the default SAS program editor. I was able to use the win32com.client.dynamic.Dispatch module to connect to SAS via OLE and pass text from Sublime directly to SAS using Sublime's build system to call my plugin. Making the connection was the easy part, it was the other processing that I had to do which was the time consuming part, but since you want to pass just a file name or the entire contents of your file, this should be a fairly straightforward plugin. Since I do not know what program you wish to open, here is the code that makes my implementation work. Maybe you caan glean something out of this.
def send_to_sas_via_ole(selected_code):
from win32com.client.dynamic import Dispatch
sasinstance = Dispatch("SAS.Application")
# submit the lines to sas
for selection in selected_code:
# for some reason cannot send as one big line to SAS, so split into
# multipe lines and send line by line
for line in selection.splitlines():
sasinstance.Submit(line)
and then the call in the run method of my plugin class:
class RunSasMakoCommand(sublime_plugin.TextCommand):
def run(self, edit):
try:
send_to_sas_via_ole(selected_code)
except Exception as e:
print "\n".join(selected_code)
print "Couldn't connect to SAS OLE"
print e
Good luck!
Open 'regedit.exe';
Navigate to
HKEY_CLASSES_ROOT\Applications\sublime_text.exe\shell\open\command
correct the path. Exit 'regedit.exe'
(optional) restart 'explorer.exe' or reboot your PC.
enjoy :p;
Right click on the file, press "Properties". You will see Opens with SomeProgram and then a change button. Click on the change button, and then look through the list for Sublime Text, if you can't find it, you can choose an application using the file explorer, from there you can navigate to C:\Program Files\Sublime Text 2 and choose sublime_text.exe

Sublime Text 2: How to make Sublime run a command (fold code) when opening a file by default?

I would like to make it so that whenever I open a file in Sublime it will automatically do "Fold Level 2" Coding which is command shortcut Ctrl-K,Ctrl-2 (or CMD-K, CMD-2). I use both mac and pc.
I don't want to enter that shortcut everytime, instead I would like Sublime to automatically run that on opening a file. Please let me know if there a way to do that.
I think that the best solution to your problem is Buffer Scroll plugin. It remembers and restores a lot of things, folding included.
If you don't want to install that plugin, you can create your own:
Create new plugin Tools / New Plugin...
Insert code
import sublime, sublime_plugin
class Folding(sublime_plugin.EventListener):
def on_load(self, view):
view.run_command("fold_by_level", {"level": 2})
Save it in your User directory with the filename you prefer.
This will set folding level to 2, for every file you open.

How do I make Sublime Text 2 prompt me before exit?

There have been too many times where I have accidentally clicked the X on the top right window in Sublime Text 2. I then loose my undo/redo history.
Edit: I have tried to set hot_exit to false in default and user settings.
Look for hot_exit in Preferences.sublime-settings
// Exiting the application with hot_exit enabled will cause it to close
// immediately without prompting. Unsaved modifications and open files will
// be preserved and restored when next starting.
//
// Closing a window with an associated project will also close the window
// without prompting, preserving unsaved changes in the workspace file
// alongside the project.
"hot_exit": true,
and set it to false.
Install the plugin Local History.
Every time you modify a file, a copy of the old contents is kept in
the local history.
Either do it through Package Control (If you don't have it already, install it)
Or get it on Github