如何判断我在Sublime Text中使用哪个项目?
问题描述:
我的机器上经常有相同的Git仓库的多个副本。我通常会打开多个Sublime Text窗口,每个窗口都有一个Git repo副本的打开项目。如何判断我在Sublime Text中使用哪个项目?
是否有任何设置可以在状态栏或标题栏上显示项目文件的路径,或者是否可以轻松区分其他类似项目?事实上,我没有简单的方法来区分哪个Sublime Text窗口正在使用哪个项目文件。
答
Sublime的标题栏会默认显示当前与窗口关联的项目的文件名部分;它是当前选定文件名称右边的文本,位于圆括号内。例如,在这里我有OverrideAudit
项目当前打开的:
有没有办法,(目前)显示在标题栏等信息,但使用一些插件代码可以显示在状态栏中的文本,而不是。
[编辑]问题跟踪器上有一个open feature request,用于添加配置标题栏的功能,您可能需要权衡该标题栏。 [/编辑]
下面是一个插件的示例,它复制将窗口标题中的项目名称放入状态栏。如果需要,您可以修改show_project
中的代码,该代码仅将项目名称隔离为如果需要,请包含路径。
要使用此功能,您可以从菜单中选择Tools > Developer > New Plugin...
,并使用此代码替换默认存根,根据需要进行修改。
import sublime
import sublime_plugin
import os
# Related Reading:
# https://forum.sublimetext.com/t/displaying-project-name-on-the-rite-side-of-the-status-bar/24721
# This just displays the filename portion of the current project file in the
# status bar, which is the same text that appears by default in the window
# caption.
def plugin_loaded():
"""
Ensure that all views in all windows show the associated project at startup.
"""
# Show project in all views of all windows
for window in sublime.windows():
for view in window.views():
show_project (view)
def show_project(view):
"""
If a project file is in use, add the name of it to the start of the status
bar.
"""
if view.window() is None:
return
project_file = view.window().project_file_name()
if project_file is not None:
project_name = os.path.splitext (os.path.basename (project_file))[0]
view.set_status ("00ProjectName", "[" + project_name + "]")
class ProjectInStatusbar(sublime_plugin.EventListener):
"""
Display the name of the current project in the status bar.
"""
def on_new(self, view):
show_project (view)
def on_load(self, view):
show_project (view)
def on_clone(self, view):
show_project (view)