I am using Sublime Text 2 with the Sublime TFS plugin. I can check out files without a problem. If I try to save a checked-in file, Sublime TFS will automatically check the file out. However, before the checkout is complete (slow servers), Sublime Text shows an unable to save dialog. I can dismiss the dialog and save the file (because checkout is complete), but it is an annoyance.
Does anyone know of a solution? Perhaps I can change the timeout on a save before the dialog shows?
Found the solution. I changed the argument in thread.join() from 5 to 10 seconds inside the on_pre_save() function, located in sublime_tfs.py. See code below.
def on_pre_save(self, view):
if not hasattr(self, 'manager'):
self.manager = TfsManager()
if self.manager.auto_checkout_enabled:
path = view.file_name()
if not (path is None):
if is_readonly(path):
thread = TfsRunnerThread(path, self.manager.auto_checkout)
thread.start()
ThreadProgress(view, thread, "Checkout...", "Checkout success: %s" % path)
thread.join(10) # Changed from 5 to 10 seconds.
if thread.isAlive():
sublime.set_timeout(lambda: "Checkout failed. Too long operation")
Related
I know the "File > Open folder..." dialog box in Sublime.
The problem is that:
it first opens a "file picker" dialog box
after choosing the right folder, it opens the folder in a new Sublime Text window, instead of the current window
How to open the current file's folder in the left "Folder view" of the current Sublime window, without any popup? (I would like to bind a keyboard shortcut for this). Note: I still use Sublime 2.
The menu item Project > Add Folder to project... will prompt you for the name of a folder and then add it to the current window rather than create a new one. Contrary to the name, this will always work even if you're not explicitly using sublime-project files directly.
For doing this without any sort of prompt, a plugin would be needed to adjust the list of folders that are open in the window currently.
In Sublime Text 3 and above, there is API support for directly modifying the list of folders that are open in the window, while Sublime Text 2 only has an API for querying the list of folders.
All versions of Sublime have a command line helper that can be used to interact with the running copy of Sublime (generally referred to as subl), and one of the things it can do is augment the list of folders in the window by adding an additional one. In Sublime Text 2, the subl helper is just the main Sublime Text executable itself.
The following is a plugin that can be used in Sublime Text 2 and above that will perform the appropriate action to get the path of the current file to open in the side bar. If you're unsure of how to use plugins, see this video on how to install them.
import sublime
import sublime_plugin
import os
# This needs to point to the "sublime_text" executable for your platform; if
# you have the location for this in your PATH, this can just be the name of the
# executable; otherwise it needs to be a fully qualified path to the
# executable.
_subl_path = "/home/tmartin/local/sublime_text_2_2221/sublime_text"
def run_subl(path):
"""
Run the configured Sublime Text executable, asking it to add the path that
is provided to the side bar of the current window.
This is only needed for Sublime Text 2; newer versions of Sublime Text have
an enhanced API that can adjust the project contents directly.
"""
import subprocess
# Hide the console window on Windows; otherwise it will flash a window
# while the task runs.
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen([_subl_path, "-a", path], startupinfo=startupinfo)
class AddFileFolderToSideBarCommand(sublime_plugin.WindowCommand):
"""
This command will add the path of the currently focused file in the window
to the side bar; the command will disable itself if the current file does
not have a name on disk, or if it's path is already open in the side bar.
"""
def run(self):
# Get the path to the current file and add it to the window
self.add_path(self.get_current_path())
def is_enabled(self):
"""
The command should only be enabled if the current file has a filename
on disk, and the path to that file isn't already in the list of folders
that are open in this window.
"""
path = self.get_current_path()
return path is not None and path not in self.window.folders()
def get_current_path(self):
"""
Gather the path of the file that is currently focused in the window;
will return None if the current file doesn't have a name on disk yet.
"""
if self.window.active_view().file_name() is not None:
return os.path.dirname(self.window.active_view().file_name())
return None
def add_path(self, path):
"""
Add the provided path to the side bar of this window; if this is a
version of Sublime Text 3 or beyond, this will directly adjust the
contents of the project data to include the path. On Sublime Text 2 it
is required to execute the Sublime executable to ask it to adjust the
window's folder list.
"""
if int(sublime.version()) >= 3000:
# Get the project data out of the window, and then the list of
# folders out of the project data; either could be missing if this
# is the first project data/folders in this window.
project_data = self.window.project_data() or {}
folders = project_data.get("folders", [])
# Add in a folder entry for the current file path and update the
# project information in the window; this will also update the
# project file on disk, if there is one.
folders.append({"path": path})
project_data["folders"] = folders
self.window.set_project_data(project_data)
else:
# Run the Sublime executable and ask it to add this file.
run_subl(path)
This will define a command named add_file_folder_to_side_bar which will add the path of the current file to the side bar; the command disables itself if the current file doesn't have a name on disk, or if that path is already open in the side bar.
If you're using Sublime Text 2, note that you need to adjust the variable at the top to point to where your copy of Sublime is installed (including the name of the program itself, as seen in the example code), since the plugin will need to be able to invoke it to adjust the side bar.
In order to trigger the command, you can use a key binding such as:
{ "keys": ["ctrl+alt+a"], "command": "add_file_folder_to_side_bar"},
You can also create a file named Context.sublime-menu in your User package (the same place where you put the plugin) with the following contents to have a context menu item for this as well:
[
{ "caption": "-", "id": "file" },
{ "command": "add_file_folder_to_side_bar", "caption": "Add Folder to Side Bar",}
]
Solved for ST2 with #OdatNurd's idea:
class OpenthisfolderCommand(sublime_plugin.TextCommand):
def run(self, edit):
current_dir = os.path.dirname(self.view.file_name())
subprocess.Popen('"%s" -a "%s"' % ("c:\path\to\sublime_text.exe", current_dir))
Add the key binding with for example:
{ "keys": ["ctrl+shift+o"], "command": "openthisfolder"}
This is not about those files that have their read-only flag set at the OS level, but about every file that users don't intend to modify.
I want Sublime Text to ignore any changes and prevent saving anything to such files. One example for this scenario is when the user is reading source code that shouldn't be altered in anyway.
"Just be really careful, and don't press any buttons" is undoubtedly a good advice, but if I were to "accidentally" delete that octothorpe in front of a comment, or add new lines to a file that is sensitive to such things (some config files in Linux) and then accidently hit save....
I found "toggle-readonly" at GitHub, but it is actually toggling the file permissions ("Read Only", "Write"), which is not quite what I wanted.
Yes, this is possible, but you'll have to write a plugin (which actually isn't that hard, especially if you know Python). The API call is view.set_read_only(flag) in the sublime module, where Flag is a boolean. Here's a quick example which checks if a newly-opened file has a certain suffix, and if so sets it to read-only.
import sublime
import sublime_plugin
class MakeViewReadOnlyCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.file_name().endswith(".cfg"):
self.view.set_read_only(True)
class ConfigFileListener(sublime_plugin.EventListener):
def on_load(self, view):
view.run_command("make_view_read_only")
Open a new file with Python syntax, copy the code into it, alter it as needed, then save it in your Packages/User directory as make_view_read_only.py. Restart Sublime to load it, and you should be all set. To test if a certain view is read-only, open the console and enter
view.is_read_only()
The plugin "Toggle the View Read-Only" will do it. It basically does what MattDMo said: when you set the view as read-only, the file can still be changed by another program (or another user), and Sublime Text will pick up those changes. It also has the context menu item you asked for. I like the "Readonly" indicator in status bar.
I didn't test it on Sublime Text 2, but in Sublime Text 3 it works great, and it claims to work on Sublime Text 2 as well.
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.
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
When I perform these steps:
Open an existing file in Sublime Text 2.
Type in arbitrary text at an arbitrary place in the file.
Close Sublime Text 2.
Note, I have not saved the changes.
Open Sublime Text 2.
Open the file from step 1.
I see changes in the file. But if I view the file in, let's say, Notepad, I see no changes.
Where does Sublime Text 2 keep the changes made to files?
As far as I'm concerned the question isn't answered completely...
As nnnn explained, the unsaved changes of a project are saved in its sublime-workspace file.
But if you haven't created a project and you are just working on some files, sublime also does remember the unsaved changes. These were saved in 'Session.sublime_session'.
Where the session can be found, depends on your operating system:
OS X: ~/Library/Application Support/Sublime Text 2/Settings/
Windows: %APPDATA%\Sublime Text 2\Settings\
Linux: ~/.config/sublime-text-2/Settings/
(I just found this info some kind of accidentally in the official sublime forum)
If you have made a project, the magical file-restore fairy will be in the folder where you told Sublime to store your project, in a file called [yourprojectname].sublime-workspace.
If you delete that workspace file before opening, Sublime will nuke your changes and complain about opening any previously open files. This move will probably cause you some grief, so don't try it unless you've already saved all necessary changes.
The workspace saves, among other things, your window layout, all the contents of any files that are open, and your last find/replace/autocomplete entries. (That is why your autocomplete gets "smarter" over time).
Note the little symbol where there is normally an x to close the tab. If it is a dot instead of an x, the file is considered unsaved and will be brought back also unsaved when you re-open Sublime.
I have the issue after updating Sublime Text 2 (old version) to Sublime Text (new version) on macOS. I don't know why the old version has the suffix "2".
Anyway, a solution to restore the whole my previous session is to copy a file Session.sublime_session, before the manipulation close the Sublime Text app, then execute a command:
cp ~/Library/Application\ Support/Sublime\ Text\ 2/Settings/Session.sublime_session ~/Library/Application\ Support/Sublime\ Text/Local/Session.sublime_session
And finally, start the Sublime Text app.