Can the size of Sublimes's paste history be increased? - sublimetext3

Sublime Text 3's paste_from_history command is great, but I would like a longer history than 15 entries. Can this be configured?

This can't be configured directly, but you can modify the command itself to change the size of the history to achieve the same effect, if you would like.
To do so you need to create an override on the Default/paste_from_history.py plugin file, which tells Sublime to use your modified version of the file instead of the one that it ships with.
The easiest way to do so would be to use the PackageResourceViewer package. From the command palette, choose PackageResourceViewer: Open Resource (make sure you don't accidentally use the command with Extract in the name) and select first Default and then paste_from_history.py.
This will open the packed version of the file and set things up so that you can edit it and easily make an override. The part you want to change is on line 12, where LIST_LIMIT is defined to be 15:
class ClipboardHistory():
"""
Stores the current paste history
"""
LIST_LIMIT = 15
def __init__(self):
self.storage = []
You can change that number to the size that your desired value and save the file. As soon as you save, Sublime will reload the plugin and make your changes live (note that this clears the clipboard history).
Behind the scenes, what this is doing is creating a folder named Default in your Packages folder (Preferences > Browse Packages shows you where that is), with the modified file stored inside.
As long as that file exists, while Sublime is loading packages it will ignore the version it ships with and use your modified copy instead. Removing your copy and restarting Sublime will revert back to the defaults.
Sublime won't warn you if a future update changes the shipped file; it will still always use your modified copy. If you want to be warned when this happens, the OverrideAudit package can come in handy. It will automatically detect when a file you're overriding is updated and warn you so that you can see if you need to incorporate any changes.

Related

How to set default file extension of saving file for specific syntax in Sublime Text 3

The C++ standard doesn't rule the file extension, so the most of people use .cpp, but I prefer using .cc, how to tell Sublime Text save to .cc when using C++ syntax as default?
The way to change the default file extension shown by the save-as dialog when saving a file set to a specific syntax is to override that syntax's default .sublime-syntax file. In your case that would be the C++.sublime-syntax file.
Of course, as MattDMo points out, the easiest thing to do is to manually type the preferred file extension whenever you save a file. But for those that want to make the change, here's how to do it.
Open the Command Palette and select View Package File.
Type C++ and then select C++/C++.sublime-syntax. ST will open the file.
Near the top you should see file_extensions: and a list like this:
file_extensions:
- cpp
- cc
- cp
- cxx
- c++
...snip...
ST uses the 1st item in the file_extensions list as the default file extension for the save-as dialog.
To make .cc the default extension instead of .cpp, edit the list order so that - cc is the 1st item and - cpp is 2nd, i.e. swap the top 2 lines around.
Now save the file in your ST config Packages directory with this path: ../Packages/C++/C++.sublime-syntax. If you like you can create a C++ directory in Packages and then just use Ctrl+S to save because the path will have been automatically set by ST but the file won't save unless the ../Packages/C++/ directory already exists.
Note: The full path on a Linux machine would be like this: /home/user/.config/sublime-text-3/Packages/C++/C++.sublime-syntax
The Package Resource Viewer plugin's Open Resource Command Palette command could be used to extract .sublime-syntax files instead of ST's native View Package File command. That plugin will automatically create the appropriate Packages directory when necessary.
Your altered Packages/C++/C++.sublime-syntax file will now override the default version that is shipped with ST. You can reverse this easily simply by deleting the file. It is safe to delete the directory as well if you are not overriding any other files in the same directory.
The only problem with this is when you install a new version of ST. If the newly installed version has an updated C++.sublime-syntax file, the local one you've created will continue to override the new one. To get around this delete your altered Packages/C++/C++.sublime-syntax file when you install a new version of ST and repeat the steps above to restore your preferred default file extension once the installation has been done.
Unfortunately there is no way around this, you can not partially override the C++.sublime-syntax file with just the file_extensions: section. Of course new versions of ST don't come along very frequently and the C++.sublime-syntax file gets updated even less often. So this is not a major issue.

How do I add items to a menu in Sublime 3?

I want to add a couple of items to the Edit menu in Sublime 3. In fact, I just want to copy the Find and the Replace menu items from the Find menu. Call me lazy, but I just want to use the same Alt-E menu shortcuts I've grown accustomed to across so many other programs. I don't like making the mental switch from Alt-E to Alt-I when I switch from Sublime to anything else.
Anyway, from the sublime documentation I read that I can create a file Main.sublime-menu in the Packages/Default (or Packages/User) directory. The JSON format is easy enough to follow. But the problem is that there is no such file by default. If I add one (and I did) then sumblime replaces the entire main menu, which is not the result I want.
I don't want to replace the entire menu, I just want to add two entries to the Edit menu. Ideally, I would like to copy & paste the Find and Replace entries from the Find menu. That would save me the time of figuring out the command names.
Resource files that ship with Sublime are stored in sublime-package files that exist in a special folder stored in the installation folder of Sublime (where the executable is), which keeps them safe from modification because Sublime will replace them wholesale when it updates.
You can view the content of any resource file currently known to Sublime by using the View Package File command from the command palette. It will show you a list of every resource, and you can filter the list the same as the command palette entries to drill down to find what you need:
Choosing an item from this list will open the file for you to look at. If it's coming from a sublime-package file, it will be a read-only buffer that you can't modify to remind you that you can't edit the file. Resources that come from your Packages folder directly will be editable, however (such as your User package).
The Default package is where things like the default settings, key bindings and menus are defined. So although what you see in the list depends on the packages you have installed, the item you want here is Default/Main.sublime-menu.
Note that if your intention is to just add some items, you want to put your modifications into your User package. Any items you add here will augment the existing menu; that is, you can only add items, you can't modify or remove them.
If you put the file into the Default package folder (which you may or may not have to create), the file you create will override the one that's provided inside of the sublime-package file. You would do this if you want to remove entries, change what command they execute, etc.
If you go that route, note that Sublime will use this file forever even if a future update modifies the file. In that case I would recommend the OverrideAudit package (disclaimer: I am the author of said package) as it will warn you when that happens.
If this is your intention, OverrideAudit's Create Override command will allow you to seamlessly open the file and save it to create the override, saving you the trouble of finding the right place to put the file.

Automatically loading a Sublime workspace on startup

I posted a question here: Easier access to Sublime macros
That was brilliant and worked perfectly! But there is one last thing I can't figure out. How do I set it so Sublime loads a saved workspace automatically whenever it is opened? Because I have my macros folder in the sidebar but I want it there automatically every time I start Sublime, I added the folder and saved the workspace but when I close Sublime the folder is gone and I have to go to project->open-recent->myworkspace
Is there a way to either load a workspace automatically upon starting Sublime or automatically adding a folder to the project when starting Sublime?
If the appropriate folder is open in the side bar and you have the hot_exit setting turned on (which it is by default), then quitting Sublime and restarting it should keep the folder there, and the same should be true for individual windows if they are referencing a project or workspace.
It's not uncommon to run afoul of the distinction that closing the last window is not the same thing as quitting Sublime depending on the platform that you're on, which can cause Sublime to record in the session that there were no windows open when it quit.
However based on the comments on your question it sounds more like this isn't actually your issue and you'd instead like a way to have a specific folder or folders added to newly created windows and projects instead, so that your macros folder is just always available.
This is possible via a plugin, which can adjust the folders in a particular window as appropriate. This can be semi-mostly automated (more on that below). Here's the plugin we need for this:
import sublime
import sublime_plugin
class AddAdditionalFoldersCommand(sublime_plugin.WindowCommand):
"""
Adds all of the folders from the `additional_folders` setting in the user
preferences to the current window, if they are not already present there.
"""
def run(self):
# Get the list of folders from the settings
settings = sublime.load_settings("Preferences.sublime-settings")
additional = settings.get("additional_folders", [])
# Get the project data and associated folders from the current window,
# with sensible defaults if the window has no project data yet
project_data = self.window.project_data() or {}
project_folders = project_data.get("folders", [])
# Add in any additional folders that are not already present
for folder in additional:
if not any(o["path"] == folder for o in project_folders):
project_folders.append({"path": folder})
# Update project data
project_data["folders"] = project_folders
self.window.set_project_data(project_data)
class AdditionalFolderListener(sublime_plugin.EventListener):
"""
Listens to see if a new window has been created, and if so add additional
folders if desired.
"""
def on_post_window_command(self, window, command_name, args):
if command_name == "new_window":
settings = sublime.load_settings("Preferences.sublime-settings")
if settings.get("auto_add_additional_folders", False):
window = sublime.active_window()
window.run_command("add_additional_folders")
To use this, you also need to add some settings to your Preferences.sublime-settings file (change the values as appropriate):
// When true, the folders in "additional_folders" will be
// automatically added when a new window is created.
"auto_add_additional_folders": true,
// Additional folders to add; these must be absolute paths
"additional_folders": [
"/home/odatnurd/.config/sublime-text-3/Packages/User/macros"
],
This does two things:
When you run the add_additional_folders command, all of the folders from the list configured in the additional_folders setting will be added to the current window so long as they are not already present (otherwise the same folder would appear more than once).
When a new window is created, if the auto_add_additional_folders setting is true (it defaults to false in the code for safety), then the command will be executed automatically for the new window.
Cycling back to the above post, the "semi-mostly automated" part of this is because in order to work, the new_window command has to be invoked for the event handler to be able to know that a new window was created.
This will be the case if you use the menu item or key binding to create a new window, but that command isn't executed if you open an existing project or workspace. It's also possible for windows to be created without using this command, such as if you quit Sublime with no windows and it creates a new one by default, or on MacOS if there are no windows and you click the dock icon.
Thus depending on how you use Sublime you may want to bind add_additional_folders to a key so that you can invoke it manually as needed. You can also create a file named something like AdditionalFolders.sublime-commands in your User package with the following content to add the command to the command palette. You can adjust the caption to suit; also the name of the file is not important so long as the extension is correct.
[
{ "caption": "Add Additional Folders", "command": "add_additional_folders" },
]
Final Notes:
Despite the name of the API used here, this will work in all windows even if they don't have a project associated with them (or they just have a workspace associated with them) because all windows inherently carry project data
It's been hinted that the next version of Sublime will have additional events that make it easier to detect new windows. The above covers up to build 3211; if you're reading this and there is a newer build available, the above could possibly be made smarter.

Sublime Text 3: import projects list from folder/files to OpenRecent list

I save several projects in a same folder by manual click Project -> Save Project As...
and I used to use cmd + ctrl + p to open Switch Projectlist to switch between projects
and everything's works fine.
but today, I accidentally remove my Switch Project list in by click Project -> Open Recent -> Clear Items, so my Switch Project list is empty now...
I know I could add them back through reopen ALL my projects. due to the number of projects is pretty a lot, that will be kind of annoying to add them back one by one.
I wanna know if there's a smarter way to do that for me.
maybe import all my *.sublime-project files from folder or something.
thanks
Short of manually opening every project, I don't think there is any way to do something like this directly. There isn't a command or plugin endpoint that I'm aware of that lets you open a project by name or filename, so it's not possible to create a plugin to do the work, and Sublime doesn't have the ability to pre-load the list of packages directly either.
That said, it is possible to manually update the list of recent projects, but whether or not that is more or less work than opening all of the projects is something to consider.
If you use Preferences > Browse Packages from the menu or the command palette, a file browser will open on your Packages folder. From there go up one directory level and go inside of the Local folder, where you will find a Session.sublime_session file.
Sublime saves it's state into this file when you quit it, and uses it to restore state when you start it again. Here you will find all of the saved information, such as the windows and files that were open and so on.
Changing this file will change the data that Sublime loads, so you can modify the session file to set up the data that you want. You need to make sure that you modify the file while Sublime is not running or your changes will be ignored and clobbered away. Also it's a good idea to make a backup of the file before you start in case things go pear shaped.
Down near the bottom of the file you will find a top level key named workspaces, and inside of it a recent_workspaces key:
"workspaces":
{
"recent_workspaces":
[
"/home/tmartin/local/src/OverrideAudit/OverrideAudit.sublime-workspace",
]
}
This is where the list of recent projects is stored for use in the menu and the quick switch project command. Particular things to notice are that the entries are naming sublime-workspace files, and that the paths are absolute.
NOTE: On windows, the filenames stored in the session file are in a format like /C/Users/tmartin and not c:\users\tmartin; on that platform you need to make sure that you adjust the paths accordingly. As long as there is already at least one entry in the list when you look at the session file, you can easily see how to construct the paths that you need.
Despite the name of the commands and menu items, what you're actually switching between is different workspaces. Every sublime-project is associated with a sublime-workspace file, which acts as a dedicated sublime_session file for that particular project. This mapping is one-to-many in that you can have multiple workspaces for the same project file, allowing you to reference the same paths in multiple windows but have different window layouts.
While Sublime is not running you can edit this file to add in the full paths to all of your workspace files; when you start Sublime up the list will be populated (every sublime-workspace file knows what sublime-project it is associated with).
What remains is whether or not it's quick to come up with the list of files in a way that you can easily paste them into the session file.

Where are the default packages in Sublime Text 3 on Ubuntu?

I'm migrating from Sublime Text 2 to 3. In Sublime Text 2, I changed a lot of the default settings of the editor -- such as the tab bar height, sidebar color, etc. -- by modifying the Default.sublime-theme file in sublime-text-2/Packages/Theme - Default. I was also able to modify the colors of the default color schemes in a similar fashion. I've been trying to figure out how to do this for Sublime Text 3, but can't seem to find these files. ~/.config/sublime-text-3 only seems to contain overrides for user settings, not the default settings.
This link on the Sublime Text forums seems to give the location for Windows and Mac, but not for Ubuntu. I've searched a bit to no avail. Does anyone have suggestions?
Thank you!
To amplify on #skuroda's answer - ST3 contains all of its data that, in ST2, was stored in Packages/PackageName, in PackageName.sublime-package files that are basically just zip files, or "Resources" as they're now known. Using PackageResourceViewer, you can easily edit the individual files contained within the resource, then save it back again. When saved, the proper directory structure under Packages/PackageName will be created, allowing you to edit the file directly next time. The way file precedence works in Sublime, any file that exists in ~/.config/sublime-text-3/Packages/PackageName/ will override any file of the same name stored in PackageName.sublime-package.
However, since you don't want these files to be accidentally overwritten, I would suggest creating ~/config/sublime-text-3/Packages/User/Themes/ and User/Color Schemes directories and storing your customized files there instead. The User/ directory is protected from overwrites during upgrades, etc., and unless you're planning on creating a customized theme or color scheme for redistribution through Package Control, it's best practice to keep your files in there.
EDIT
I just realized you hadn't gotten an answer to your original question - where are the files stored? If you installed the .deb file from sublimetext.com, all the .sublime-package files are in /opt/sublime_text/Packages.
If you want the easiest way to save your customized settings first copy all the default settings and paste into a new file named Preferences.sublime-settings.
Then make your changes and save the file to:
/home/**username**/.config/sublime-text-3/Packages/User
In order to see the .config folder you will have to enable show hidden files.
The instructions are written at the top of the default settings list in case you are wondering where got this from.
In the ST console, try entering sublime.executable_path(). This should get you close to where you need to be. Also, you should copy the content of the file out and move them (under a new name to the packages folder). You probably don't want your modifications to be reverted on subsequent update. An alternative method is to try overriding the files you need to. I haven't done that for theme files, so I'm unsure if everything will work as it should. If you take the override route, I'd recommend using PackageResourceViewer. I wrote it to help with browsing/overriding package files in ST3.
You can use
dpkg -L <package name>
For example
dpkg -L subli<tab>
Just open up sublime text, hit ctrl+` to open sublime text console, then run these command accordingly
sublime.packages_path() to see user installed packages directory
sublime.installed_packages_path() to see zipped packages (with .sublime-package file type) directory
Mostly likely, you'll find the Default package inside zipped packages directory.
You can use PackageResourceViewer to see zipped packages's contents as #MattDMo suggested, but more over, you can actually extract zipped packages into user installed package directory directly using PackageResourceViewer's extract command.

Resources