Enable Autosave files plugin on Sublime Startup - sublimetext3

How can the AutoSave plugin for Sublime Text be automatically enabled on Sublime Text 3 startup?
link to auto-save is here

This particular package doesn't naively support the idea of enabling itself automatically when Sublime starts, so in order to accomplish this a small plugin is needed to make this happen, which I've posted below.
In order to use this plugin, select Tools > Developer > New Plugin... from the menu, then replace the stub plugin code that is presented to you with the code below and save the file in the location that Sublime will default to (your User package) as a Python file, for example auto_save_on_startup.py
NOTE: See the edit below; this plugin requires that you add a setting in order to control whether it's enabled or not.
import sublime
import sublime_plugin
# Sublime executes this every time it loads the plugin, which includes when
# it first starts, as well as whenever the this file changes on disk.
def plugin_loaded():
settings = sublime.load_settings("auto_save.sublime-settings")
# See if auto_save_toggle_at_startup is turned on; default it to
# not being turned on if the setting is missing, because that is
# how the package would behave if you didn't add the setting.
if settings.get("auto_save_toggle_at_startup", False):
sublime.set_timeout(lambda: sublime.run_command("auto_save"), 1000)
Edit: The original version of this plugin called run_command directly; however Sublime invokes the plugin_loaded endpoint before it has fully added all of the command classes provided by plugins, so it was possible for this to try to run the command before it was available.
The code above has been modified so that there is a delay imposed before the command triggers to give the commands time to become available.
As the comment suggests, every time Sublime loads a plugin file, it will execute the plugin_loaded() function inside of that plugin file, if it happens to exist.
Here the code checks the auto save package settings to see if you have set the value auto_save_toggle_at_startup to true, and if you have it will invoke the command from the auto save package that turns it on.
As such, you also need to select Preferences > Package Settings > Auto-save > Settings - User from the menu and add the appropriate setting. Preferences is under Sublime Text in the menu if you're using MacOS.
If that brings up an empty file (because you're using the default settings), then you should enter the following into the file and save it. Otherwise you can just add the setting itself to the existing settings.
{
// Toggle auto save at startup (from User/auto_save_on_startup.py)
"auto_save_toggle_at_startup": true
}
Here the comment is a reminder that this setting is being provided by something outside of the package itself, in case you forget.
Since the plugin command will only toggle the state of the auto save when it's loaded, you either have to add the setting before you add the plugin, save the plugin file to get Sublime to reload it once the setting is in place, or restart Sublime.
Alternatively you can replace the plugin_loaded() function to contain only the run_command line to unconditionally always toggle the state at startup, but you may want to temporarily stop it from doing that at some point in the future, which you could easily do by just toggling the setting.

Related

How to use file:duplicate from the palette using plugin in sublime text?

How to use File : duplicate option from the palette using plugin in sublime text ?
In order to see which command is being run when a particular action is being performed, open the console (Ctrl`) and run
sublime.log_commands(True)
Next, open the Command Palette (command: show_overlay {"overlay": "command_palette"} will show up in the console) and select File: Duplicate. The console will close, but when you reopen it you will see that command: side_bar_duplicate {} has been logged. At this point, you can enter
sublime.log_commands(False)
to stop logging, as it tends to fill the console with unnecessary garbage.
As you can see from the name of the command, File: Duplicate is from the SideBarEnhancements plugin (defined here) and is not built-in to Sublime. Therefore, if you are making a plugin for public distribution, you users will need to have SideBarEnhancements installed first.
To call this (or any) command in your plugin, simply put
window.run_command("side_bar_duplicate")
into your code at the appropriate location.
As #OdatNurd reminded me, because SideBarDuplicateCommand is a subclass of sublime_plugin.WindowCommand, it can only be run on an instance of sublime.Window.

Sublime text shortcut to open keybindings and settings

I would like to be able to map a keyboard shortcut to be able to open the settings and open the keybindings.
I would like it to behave the exact same as going to Menu > Preference > Settings \ Keybindings.
Is this possible?
If a command appears in a Menu or in the command palette, then it's possible to bind it to a key provided you can determine what the command is and what arguments it takes.
One way to determine that for a menu item is to use View Package File from the command palette and then open the appropriate menu resource. The main menu always comes from a Main.sublime-menu resource, so once you enter the command you can enter Main.sublime-menu in order to see all of the resources that match, then pick the one for the appropriate package.
For a command that appears in the command palette you can do the same by entering sublime-commands to see all of the files that are contributing commands to the command palette and then select the appropriate one to see what command it's executing.
For determining the command bound to a key (in case you want to go the other way and add it to a menu or the command palette) you would look for sublime-keymap files instead.
These options require you to either know the package that is contributing the command or be able to infer it based on what it does. The Default package is what contains the default Sublime behaviour; other files augment the files from that package.
Another method is to open the Sublime console with Ctrl+` or the menu item View > Show Console and then enter the command sublime.log_commands(True) to turn on command logging.
With that enabled you can take the action you want to know the command for and the console will tell you the command being executed and what arguments it's taking.
In your particular case, the output in the console would be:
command: edit_settings {"base_file": "${packages}/Default/Default ($platform).sublime-keymap", "default": "[\n\t$0\n]\n"}
command: new_window
The first of these is the appropriate command, and the second is that command executing a command of it's own to open a new window for the settings to be stored in.
Armed with that, you can make an appropriate key binding:
{
"keys": ["ctrl+alt+shift+k"],
"command": "edit_settings",
"args": {
"base_file": "${packages}/Default/Default ($platform).sublime-keymap",
"default": "[\n\t$0\n]\n"
}
},
Note that recent (as of the time of this answer) builds of Sublime have a bug in which commands executed from the command palette don't always get logged, in which case if that's how you want to determine the correct command you would have to look in the resource directly.
To add a more simple approach to OdatNurd's fantastic answer: I recently found myself wanting a shortcut to open my keybindings file and came across a slightly longer route using the Command Pallet that I was content with.
This doesn't answer the specific issue of creating a keyboard shortcut to open the user's .sublime-keymap file, but it does solve the general issue of getting to that file in a timely manner, without having to use the mouse.
The approach is:
Ctrl+Alt+P (Open Command Pallet)
Type: "keybind"
This gives:
Hit Enter and the user's .sublime-keymap file will be opened as it would be if you navigated to Menu > Preferences > Key Bindings.

How to auto-reload current file in sublime text

Sometimes I would like to modify files in another text editor and switch back, but Sublime Text 3 wouldn't reload the current file that I edited before.
How do I get it to reload the file automatically?
There's a setting to control whether you get to see a prompt, but otherwise Sublime Text reloads the file automatically by default.
See the setting in Preferences.sublime-settings:
// Always prompt before reloading a file, even if the file hasn't been
// modified. The default behavior is to automatically reload a file if it
// hasn't been edited. If a file has unsaved changes, a prompt will always
// be shown.
"always_prompt_for_file_reload": false
I believe AutoReload should do the trick.
You can also reload file manually using menu File->Revert File
If the file is changed on disk, and is open in Sublime, the default behavior is a dialog window that prompts to see if you want to reload the file.
If you have previously selected "Ignore All" when prompted with that dialog for that particular file, then Sublime's behavior for that file will be changed. The file view setting will be changed from True to False for reload_file_on_change.[1]
You can get the current file view setting (for whichever file is open in your view) by typing this in the console[2]:
view.settings().get('reload_file_on_change')
And can you set the current file view setting back to True with this[3]:
view.settings().set('reload_file_on_change', True)
I searched ["ignore all" sublime reload save] to find this question/comment from thundt and the answer from bschaaf in the Sublime Forum: Ignore All when unsaved changes and file changed on disk is not helpful!
Found via Sublime Text Docs > Settings > Troubleshooting
Found via Sublime Text API Reference > Settings > .set

Sublime text editor doesn't auto-refreshes the file if it is modified by another program

Although Sublime is a really powerful text editor but I am facing an issue. I have been using sublime text editor to view logs of my application.Suppose I have already opened file in my editor. After the logs are modified by the app server.
Sublime doesn't give any popup like we get in other editors
Example:
NOtepad++ says:
Also it doesn't modify the file. I have to close the file explicitly and then I re-open the file to ready the modified logs.
Only options i get in my sublime preferences are :
Please help..!
You are using an extremely outdated version of sublime (1.4). You can enable this functionality by upgrading and performing a small settings tweak:
Download the new SublimeText
Install it and open it
Go to the preferences menu and select "settings"
This will open the settings files for sublime, scroll down to line 349 on the left panel and copy that line.
Paste in the copied line into the right pane and replace "false" with "true"
Save and restart SublimeText
This should fix your issue entirely while also upgrading you to the awesome new SublimeText :)
Happy coding!
Set the following setting to true.
Menu > Preferences > Settings
// Always prompt before reloading a file, even if the file hasn't been
// modified. The default behavior is to automatically reload a file if it
// hasn't been edited. If a file has unsaved changes, a prompt will always
// be shown.
"always_prompt_for_file_reload": true

How do I remove CodeIntel in Sublime Text 3?

I'm using ST3 build 3114. How do I stop the code completion popup? I can't find SublimeCodeIntel anywhere in my Package Control to remove, I added SublimeCodeIntel to my 'ignored_packages' in settings which did nothing, and I searched my entire hard drive for CodeIntel and found nothing. Why does this keep popping up? I would actually like to just disable it for CSS files, but that doesn't work too.
The autocompletion of property names in CSS is supplied by the built in CSS package. To disable it:
Install PackageResourceViewer from Package Control, if it is not already installed.
Open the Command Palette
Type PRV: O and select PackageResourceViewer: Open Resource
Select CSS
Select css_completions.py
Delete the contents of the file
Save it
To disable other auto completion sources (like word/buffer completion) in CSS files, you can add the following to your user preferences:
"auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc - source.css",
Note that this is the default auto_complete_selector value with - source.css added to the end of it.

Resources