Last opened / edited / visited files in Sublime Text 2 - sublimetext2

Is there a way to get the last 10 opened / edited / visited files in Sublime Text 2 (as data for a python module)?

For edited or visited files I use Goto Anything feature (by default ctrl+p on Win). The panel appears and files are sorted according to your recent activity. Of course you can filter there (just to type only part of the file name is enough). Is it what you are looking for?
In case of already closed files I use very handy GoToRecent plugin. Surely it is worth a try! Default keybindings can be remapped (e.g. for ctrl+e add the following to the array in your user key bindings file):
{ "keys": ["ctrl+e"], "command": "goto_recent" }

Related

Sublime Text 2 different indentations settings

I want to configure my Sublime Text 2 to have two different types of indentations:
4 spaces for general use
2 spaces for my html pages
I think it should be done by editing the settings, but I don't know what to write.
The key setting is "tab_size". By default, if you look at Preferences -> Settings-Default, it is
"tab_size": 4
The items in this file should absolutely not be modified. Instead, copy anything you want to change to the file opened by Preferences -> Settings-User, and alter the values there. It is saved as Packages/User/Preferences.sublime-settings where Packages is the folder opened by selecting Preferences -> Browse Packages....
If you would like to have specific settings for certain syntaxes, open a new file in Sublime with JSON syntax. Like any other Sublime preferences file, open and close it with curly braces { }. Put your desired settings in it, one per line, each followed by a comma , except the last entry (basically, it should be valid JSON, except JavaScript comments are also allowed).
{
"tab_size": 2,
"translate_tabs_to_spaces": true
}
Finally, save it in your Packages/User folder as Syntax Name.sublime-settings - in your case, it should be HTML.sublime-settings. These settings should now be applied to any newly-opened or newly-created HTML files.

Navigate back and forth between recently opened files

Is there a keyboard shortcut in Sublime to navigate back and forth between recently opened files? Something like Alt+Right and Alt+Left (an example from Eclipse or web browsers). If not, is there a way to create a custom shortcut like this?
You can use the following shortcuts to navigate through different open files:
CTRL+PAGE UP Cycle up through tabs in taborder
CTRL+PAGE DOWN Cycle down through tabs in taborder
CTRL+TAB Next recently used file
CTRL+SHIFT+TAB previous recently used file
More Shortcuts for sublime can be found here.
Custom shortcuts
Go to Preferences > Key Bindings-Default and find the binding that you need and copy the command. The next step is to open references > Key Bindings-User where you paste and edit the command.
More concrete info about custom shortcuts can be found here.
It is possible if you still have them opened. Other than that
That's CTRL+TAB / CTRL+SHIFT+TAB.
Other than that, if you need to have possibility to open recently closed files, you may try doing it with CTRL+SHIFT+T (open last closed tab), try this package out or even visit this thread and see if it still works.
If you have a large number of files open, you can use ctrl + P, and use GoToAnything.
Unofficial doc information

Sublime text Autosave only for specific files

I don't want to hit cmd+s all day when I code a web UI using Sublime Text 2.
SCENARIO:
Sublime Text with LiveStyle plugin
Google Chrome with Live Style addon
WHAT I DON'T WANT:
Hitting cmd+s every time I edit an html/js/css file
Applying a global autosave solution like "save_on_focus_lost": true to all files of a project (e.g. python or php files shouldn't be autosaved)
WHAT I WANT:
Autosave and automatically reload only certain files of my project in Chrome (thanks to LiveStyle)
QUESTION:
How to selective apply an autosave setting (or plugin) like "save_on_focus_lost": true only to certain files within a project?
Any alternative solution is welcome
Don't know anything about LiveStyle, so I'll speak to the autosaving specifically. If it's all files of a particular type, you can create a syntax specific setting. If it isn't you can write a plugin that ties into the on_load or on_activated event. When this event is triggered, you can apply a setting to the view. Assuming you have the view, you can do something like view.settings().set("save_on_focus_lost", True) in the plugin.

How to edit multiple files through "Find results" in Sublime Text

When I search multiple files via Command + Shift + F, the result is returned as something like a text file. This text file is editable, but changes made don't affect the original files.
Is it possible to do such that changes in "Find Results" propagates to the original source file?
The Find Results Apply Changes plugin was created to do just that.
You can install it through Package Control's "Install Package" option.
Before using this plugin, make sure that you have UTF-8 encoding enabled using the menu:
File > Save with Encoding > UTF-8
Once installed, you can apply any change you made to a "Find Results" buffer back to the files:
Search for "foo" in a folder (Sublime's default shortcut is CTRL+SHIFT+S)
This will open a "Find Results" buffer listing all the files with "foo" in it.
Change the instances of "foo" for "bar" or something else...
Go to the menu:
Find > Find Results - Apply Changes
This will write all the changes made back to the files and save the modified files automatically.
By default, using menus is the only way to make it work. However, it is quite tiresome and doesn't save as much time as it does with a keyboard shortcut. You can set your chosen keyboard shortcut by adding a new line in
Preferences > Key Bindings - User
by adding:
{ "keys": ["ctrl+r"], "command": "find_results_apply_changes" },
Don't use CTRL+S as it will overwrite saving file shortcut.
Warning!: According to the author of the plugin:
Uses regions to allow you do multiline changes, but when inserting new
newlines, will corrupt files if you commit more than once, this
because the new newlines will shift the line numbers. Will also
'corrupt' files if you add/remove newlines in other instances of the
modified files. eg in another tab. To prevent corruption this packages
will alert you and prevent most of these.
(This is a modified version of the description from the Find Results Apply Changes Github page.)
The shortest workaround I can think of would be to open the target file from search results by simply double clicking the path and then jumping to the according line using Ctrl+G on Windows or ⌃+G on Mac OS.
That's the way I do it and must say it is only a matter of seconds, even without the plugin.

Set default syntax to different filetype in Sublime Text 2

How do I set a default filetype for a certain file extension in Sublime Text 2? Specifically I want to have *.cfg files default to having Ini syntax highlighting but I cannot seem to figure out how I could create this custom setting.
In the current version of Sublime Text 2 (Build: 2139), you can set the syntax for all files of a certain file extension using an option in the menu bar. Open a file with the extension you want to set a default for and navigate through the following menus: View -> Syntax -> Open all with current extension as... ->[your syntax choice].
Updated 2012-06-28: Recent builds of Sublime Text 2 (at least since Build 2181) have allowed the syntax to be set by clicking the current syntax type in the lower right corner of the window. This will open the syntax selection menu with the option to Open all with current extension as... at the top of the menu.
Updated 2016-04-19: As of now, this also works for Sublime Text 3.
Go to a Packages/User, create (or edit) a .sublime-settings file named after the Syntax where you want to add the extensions, Ini.sublime-settings in your case, then write there something like this:
{
"extensions":["cfg"]
}
And then restart Sublime Text
In ST2 there's a package you can install called Default FileType which does just that.
More info here.
You can turn on syntax highlighting based on the contents of the file.
For example, my Makefiles regardless of their extension the first line as follows:
#-*-Makefile-*- vim:syntax=make
This is typical practice for other editors such as vim.
However, for this to work you need to modify the
Makefile.tmLanguage file.
Find the file (for Sublime Text 3 in Ubuntu) at:
/opt/sublime_text/Packages/Makefile.sublime-package
Note, that is really a zip file. Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it.
Edit the new Makefile.tmLanguage by adding the "firstLineMatch" key and string after the "fileTypes" section. In the example below, the last two lines are new (should be added by you). The <string> section holds the regular expression, that will enable syntax highlighting for the files that match the first line. This expression recognizes two patterns: "-*-Makefile-*-" and "vim:syntax=make".
...
<key>fileTypes</key>
<array>
<string>GNUmakefile</string>
<string>makefile</string>
<string>Makefile</string>
<string>OCamlMakefile</string>
<string>make</string>
</array>
<key>firstLineMatch</key>
<string>^#\s*-\*-Makefile-\*-|^#.*\s*vim:syntax=make</string>
Place the modified Makefile.tmLanguage in the User settings directory:
~/.config/sublime-text-3/Packages/User/Makefile.tmLanguage
All the files matching the first line rule should turn the syntax highlighting on when opened.
The best solution for me turned out to be to used the ApplySyntax package.
The steps are as follows:
Install the package via Package Control
CTRL + SHIFT + P and enter ApplySyntax: Browse Syntaxes. Find your desired syntax here and note the exact line shown, e.g. I was looking to set it to Markdown from the Markdown Editing package, so for me the line was MarkdownEditing/syntaxes/Markdown.
CTRL + SHIFT + P and enter ApplySyntax: Settings.
On line "new_file_syntax": "XYZ", enter the line from Step 2.
See here for further documentation.
I found this to work better than the DefaultFileType package, because it isn't limited to just new files created by pressing CTRL + N and captured new tabs opened by clicking the empty space to the right of an open tab.
I hope is useful to someone 11 years after the original question was asked. 😅