We are ACORN!

プログラムやサーバーの設定など、技術的な記事を載せております。

sublime text

sublime text 3でプロジェクトの状態を復元する

投稿日:2019-02-27 更新日:

sublime text 3を便利に使っているのですが、一つ不満点があって、プロジェクトの状態を復元してくれないんです。

わたしはエディタを開いた直後は前に使っていたファイルが開くのが嫌なので、設定で

"remember_open_files": false

としています。

でも、プロジェクトファイルを開いたときは復元してほしいのです。
それで、プロジェクトの設定ファイルに

"remember_open_files": true

と書いても、何も起こりません。
不思議に思って検索すると、やはりバグトラックにissueがありました。

In my user settings I have “remember_open_files”: false and in my project setting file (.sublime-project) I have “remember_open_files”: true. It seems Sublime Text only remembers project opened files if the project is closed with the “Close Project” command but completely forgets them if its window just gets closed with either “Alt+F4” or a mouse.

ユーザ設定で “remember_open_files”:false を設定しています。
プロジェクト設定ファイル(.sublime-project)で “remember_open_files”:true を設定しています。
Sublime Textは、プロジェクトを「Close Project」コマンドで閉じるとプロジェクトが開いたファイルを覚えていますが、そのウィンドウが「Alt + F4」またはマウスのいずれかで閉じられると完全に忘れてしまうようです。

まさにこれです、これ!
sublime textはissueを放置なんですよね・・・

仕方がないので、プラグインを作ることにします。

まず、「基本設定」の「Packagesフォルダ…」でフォルダを開きます。
そこに、「closeWorkspaceAndExit」というフォルダを作ります。

その中に二つのファイルを作ります。
closeWorkspaceAndExit.py

import sublime
import sublime_plugin


class CloseWorkspaceAndExitCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("close_workspace")
        self.window.run_command("close")

closeWorkspaceAndExit.sublime-commands

[
    {"caption":"ワークスペースを閉じて終了します。", "command":"close_workspace_and_exit"}
]

そしてショートカットキーを割り当てます。
わたしはあんまり使っていなかった、Alt + Q にしました。

[
    { "keys": ["alt+q"], "command": "close_workspace_and_exit" }
]

これで、Alt + Q でスムーズに終了し、再度.sublime-projectファイルを開いても、作業中のファイルが開いた状態になります。
Pythonでカスタマイズできるのは、sublime textの強みですね。

追記:
最初run_command("exit");としていましたが、これだと全てのウィンドウが閉じてしまうので、run_command("close");に変更しました。

-sublime text


comment

メールアドレスが公開されることはありません。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

関連記事

関連記事はありませんでした