Is there a shortcut for Sublime Text to find an open file (Eclipse Ctrl + E)? - sublimetext2

Ctrl+P of Sublime Text lets me find a file from all project files.
However, there are too many duplicated names. I’m looking for a shortcut key like Ctrl+E in Eclipse, so that I just need to find the file in my opened file. That would save a lot of key striking. Probably called “sidebar filter”?
Does not matter if it’s 2 or 3.

Sounds easy to implement just select Tools >> Developer >> New Plugin... and add the content:
import sublime_plugin
import os
def _show_name(name):
return ([os.path.basename(name), name] if name
else ["untitled", "untitled"])
class ShowBuffersCommand(sublime_plugin.WindowCommand):
def run(self):
window = self.window
views = list(window.views())
show_entries = [_show_name(v.file_name()) for v in views]
def on_done(index):
if index == -1:
return
window.focus_view(views[index])
window.show_quick_panel(show_entries, on_done)
Afterwards save it into your Package/User folder and add this (or an other keybinding) to your keymap:
{
"keys": ["ctrl+e"],
"command": "show_buffers"
},
(Tested on ST3)

Option One, Go to the "View" menu and choose "Side Bar", then "Show Open Files"
Option Two, There is a small plugin here https://github.com/rrg/ListOpenFiles

There is in Sublime Text a useful function called Goto Anything. You can access this by pressing Ctrl + P in Windows, and then you can search through any file located in a current project (to open a project, enable the sidebar, and drag and drop a folder from the explorer to the sidebar).

Related

Shortcut to open the currently opened file's folder left panel

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"}

How to negate 'remove all folders' mistake in Sublime Text project

In Sublime Text (2 & 3) I find I accidentally remove all folders from the project when I don't want to (this option is poorly placed in the menu, with no obvious undo or warning and is arguably similar to a 'clear' button on a form).
I often have many folders open in a project each one a leaf in the folder tree structure, which is my workflow, so naturally this is a nasty break in my work if it's accidentally triggered!
I would like to know if I can either disable this option or undo it if I accidentally trigger it?
Aside from a backup, a version control system, or a versioning feature on your file system, there is unfortunately no way of undoing the "Remove all Folders from Project" command, because as soon as the command is fired the folders are removed from the .sublime-project file, and the file is saved. However, there is a way to disable the command. The methods vary between Sublime Text 2 and 3, so I'll go over 2 first.
In Sublime Text 2, click on Preferences -> Browse Packages... to open the Packages folder, whose location varies by operating system. Go into the Default folder, and open Main.sublime-menu in Sublime (it's a JSON file). Search for "close_folder_list" and find the line that looks like this (it's line 737 in version 2.0.2):
{ "command": "close_folder_list", "caption": "Remove all Folders from Project", "mnemonic": "m" },
Now, you can either simply delete the entire line, or comment it out by putting // as the first characters on the line. Save the file, then click on the Project menu to see that the option is gone.
If you're using Sublime Text 3, you'll need a workaround to access the Packages/Default folder and its contents, as in this version most of the packages that you would normally have seen in the Packages directory in ST2 are zipped into .sublime-package files and stored elsewhere. However, there's a plugin for that! Make sure you have Package Control installed, then open the Command Palette, type pci to bring up Package Control: Install Package, and search for PackageResourceViewer. Install it, open the Command Palette again, type prv, and select PackageResourceViewer: Edit Package Resource. Scroll down to Default, click on it or hit Enter, then scroll down to Main.sublime-menu and select it to open it for editing. You can now follow the instructions above to find the line containing "close_folder_list" (it should be line 795) and either delete it or comment it out.
If you'd like to keep the menu item, but move it to a different spot, you can do that as well. For example, if you'd like it at the very bottom of the menu, separated by a divider, delete the original line, put the cursor below the "refresh_folder_list" line, and paste in the following:
{ "caption": "-" },
{ "command": "close_folder_list", "caption": "Remove all Folders from Project", "mnemonic": "m" },
so it looks like this:

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.

Sublime Text 2 keyboard shortcut to open file in specified browser (e.g. Chrome)

I do web development and am trying out Sublime Text 2. Is there a keyboard shortcut to open the current file in specified browser (e.g. Chrome)?
Any help to get setup in Sublime Text for web development is appreciated!
I'm not really sure this question is approprate here, but you can add a new "Build System" under Tools -> Build System -> New Build System...
As with all configuration in Sublime Text its just JSON, so it should be pretty straight forward. The main thing you are going to want to configure is the "cmd" key/val. Here is the build config for launching chrome on my mac.
{
"cmd": ["open", "-a", "Google Chrome", "$file"]
}
Save that as Chrome.sublime-build, relaunch Sublime Text and you should see a new Chrome option in the build list. Select it, and then you should be able to launch Chrome with Cmd+B on a Mac (or whatever hotkey you have configured for build, maybe its F7 or Ctrl+B on a Windows machine)
At least this should give you a push in the right direction.
Edit:
Another thing I end up doing a lot in Sublime Text 2 is if you right click inside a document, one of the items in the context menu is Copy File Path, which puts the current file's full path into the clipboard for easy pasting into whatever browser you want.
Sublime Text 3
(linux example)
"shell_cmd": "google-chrome '$file'"
"Open in Browser context menu for HTML files" has been added in the latest build (2207). Its release date was 25 June 2012.
Windows7 FireFox/Chrome:
{
"cmd":["F:\\Program Files\\Mozilla Firefox\\firefox.exe","$file"]
}
just use your own path of firefox.exe or chrome.exe to replace mine.
Replace firefox.exe or chrome.exe with your own path.
This worked on Sublime 3:
To browse html files with default app by Alt+L hotkey:
Add this line to Preferences -> Key Bindings - User opening file:
{ "keys": ["alt+l"], "command": "open_in_browser"}
To browse or open with external app like chrome:
Add this line to Tools -> Build System -> New Build System... opening file, and save with name "OpenWithChrome.sublime-build"
"shell_cmd": "C:\\PROGRA~1\\Google\\Chrome\\APPLIC~1\\chrome.exe $file"
Then you can browse/open the file by selecting Tools -> Build System -> OpenWithChrome and pressing F7 or Ctrl+B key.
Install the View In Browser plugin using Package Control or download package from github and unzip this package in your packages folder(that from browse packages)
after this, go to Preferences, Key Bindings - User, paste this
[{ "keys": [ "f12" ], "command": "view_in_browser" }]
now F12 will be your shortcut key.
You can install SideBarEnhancements plugin, which among other things will give you ability to open file in browser just by clicking F12.
To open exactly in Chrome, you will need to fix up “Side Bar.sublime-settings” file and set "default_browser" to be "chrome".
I also recommend to learn this video tutorial on Sublime Text 2.
On windows launching default browser with a predefined url:
Tools > Build System > New Build System:
{
"cmd": ["cmd","/K","start http://localhost/projects/Reminder/"]
}
ctrl + B and voila!
There seem to be a lot of solutions for Windows here but this is the simplest:
Tools -> Build System -> New Build System, type in the above, save as Browser.sublime-build:
{
"cmd": "explorer $file"
}
Then go back to your HTML file. Tools -> Build System -> Browser. Then press CTRL-B and the file will be opened in whatever browser is your system default browser.
Here is another solution if you want to include different browsers in on file.
If you and Mac user, from sublime menu go to, Tools > New Plugin. Delete the generated code and past the following:
import sublime, sublime_plugin
import webbrowser
class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self,edit,keyPressed):
url = self.view.file_name()
if keyPressed == "1":
navegator = webbrowser.get("open -a /Applications/Firefox.app %s")
if keyPressed == "2":
navegator = webbrowser.get("open -a /Applications/Google\ Chrome.app %s")
if keyPressed == "3":
navegator = webbrowser.get("open -a /Applications/Safari.app %s")
navegator.open_new(url)
Save.
Then open up User Keybindings. (Tools > Command Palette > "User Key bindings"), and add this somewhere to the list:
{ "keys": ["alt+1"], "command": "open_browser", "args": {"keyPressed": "1"}},
{ "keys": ["alt+2"], "command": "open_browser", "args": {"keyPressed": "2"}},
{ "keys": ["alt+3"], "command": "open_browser", "args": {"keyPressed": "3"}}
Now open any html file in Sublime and use one of the keybindings, which it would open that file in your favourite browser.
On mac and sublime text 3 , which version is 3103, the content should be
{
"shell_cmd": "open -a 'Google Chrome' '$file'"
}
Tools -> Build System -> New Build System. The type following as your OS, save as Chrome.sublime-build
Windows OS
{
"cmd": ["C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "$file"]
}
MAC Os
{
"cmd": ["open", "-a", "/Applications/Google Chrome.app", "$file"]
}
Save the file - Chrome.sublime-build in location
C:\Users\xnivirro\Downloads\Software-Installed\Sublime-2\Data\Packages\User
Sublime View in Browswer - https://github.com/adampresley/sublime-view-in-browser (Tried with Linux and it works)
egyamado's answer was really helpful! You can enhance it for your particular setup with something like this:
import sublime, sublime_plugin
import webbrowser
class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self, edit, keyPressed, localHost, pathToFiles):
for region in self.view.sel():
if not region.empty():
# Get the selected text
url = self.view.substr(region)
# prepend beginning of local host url
url = localHost + url
else:
# prepend beginning of local host url
url = localHost + self.view.file_name()
# replace local path to file
url = url.replace(pathToFiles, "")
if keyPressed == "1":
navigator = webbrowser.get("open -a /Applications/Firefox.app %s")
if keyPressed == "2":
navigator = webbrowser.get("open -a /Applications/Google\ Chrome.app %s")
if keyPressed == "3":
navigator = webbrowser.get("open -a /Applications/Safari.app %s")
navigator.open_new(url)
And then in your keybindings:
{ "keys": ["alt+1"], "command": "open_browser", "args": {"keyPressed": "1", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}},
{ "keys": ["alt+2"], "command": "open_browser", "args": {"keyPressed": "2", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}},
{ "keys": ["alt+3"], "command": "open_browser", "args": {"keyPressed": "3", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}}
We store sample urls at the top of all our templates, so the first part allows you to highlight that sample URL and launch it in a browser. If no text is highlighted, it will simply use the file name. You can adjust the command calls in the keybindings to your localhost url and the system path to the documents you're working on.
I have similar situation like you. I dont wannt sublime open editor for binary like jpg png files. Instead open system default application is more reasonable.
create one Build. Just like the accepted answer. But it will both open default application and hex editor.
Pulgin OpenDefaultApplication https://github.com/SublimeText/OpenDefaultApplication
It will have context right click menu OpenInDefaultApplication. But It will both open default application and hex editor as well
Pulgin: Non Text Files https://packagecontrol.io/packages/Non%20Text%20Files Add config in the user settting
"binary_file_patterns": ["*.JPG","*.jpg", "*.jpeg", "*.png", "*.gif", "*.ttf", "*.tga", "*.dds", "*.ico", "*.eot", "*.pdf", "*.swf", "*.jar", "*.zip"],
"prevent_bin_preview": true,
"open_externally_patterns": [
"*.JPG",
"*.jpg",
"*.jpeg",
"*.JPEG",
"*.png",
"*.PGN",
"*.gif",
"*.GIF",
"*.zip",
"*.ZIP",
"*.pdf",
"*.PDF"
]
I choose the third way, it's quite sutiable for me. It will open jpg file in system default application and quickly close the edit mode automaically at the same time. As to the first two ways, you can set "preview_on_click": false, to stop openning automaticlly the hex editor compromisely.
or try this
"cmd": ["cmd","/K","start http://localhost/Angularjs/$file_name"]