Skip to content

Instantly share code, notes, and snippets.

@spelufo
Last active August 29, 2015 14:07
Show Gist options
  • Save spelufo/505db8ddd4496c6a9542 to your computer and use it in GitHub Desktop.
Save spelufo/505db8ddd4496c6a9542 to your computer and use it in GitHub Desktop.
Welcome document

ST3 hot_exit but keep old sessions

Sublimes hot_exit feature is awesome: Don't prompt me, remember the state of my open and unsaved files.... whatever. However, if most of the time you launch sublime with subl directory/i/want/to/open I get two windows: one for your previous session and one for the directory you want to work on. It's a pain to have to close what you were previously working on every time, so some people prefer to disable hot_exit and have sublime ask them if they want to save changes every time they close it.

I wanted to save the previous sessions in case I want to access them, but prevent them from opening when I just want to move on to something new. The last session is saved when you exit sublime to $SUBLIME_CONFIG_DIR/Local/Session.sublime_session. To get this effect I modifies the script that launches sublime /usr/bin/subl and /usr/bin/X11/subl (I'm on linux Mint, and have sublime installed to /opt/sublime_text, should be similar on Ubuntu, etc.) to look like the following.

#!/bin/bash

CURRENT_SESSION_DIR=~/.config/sublime-text-3/Local
SESSIONS_DIR=~/.config/sublime-text-3/Local/sessions
mkdir -p "$SESSIONS_DIR"

COUNT=`ls "$SESSIONS_DIR" | wc -l`


if [[ -e "$CURRENT_SESSION_DIR"/Session.sublime_session ]]; then

    if [ $COUNT -ge 3 ]; then
        rm "$SESSIONS_DIR"/Session3.sublime_session
        mv "$SESSIONS_DIR"/Session{2,3}.sublime_session
        mv "$SESSIONS_DIR"/Session{1,2}.sublime_session
    fi
    mv "$CURRENT_SESSION_DIR/Session.sublime_session" "$SESSIONS_DIR/Session1.sublime_session"

fi


if [ "$1" == "-l" ]; then
    if [ -f "$SESSIONS_DIR/Session$2.sublime_session" ]; then
        echo "Opening session: $SESSIONS_DIR/Session$2.sublime_session"
        cp "$SESSIONS_DIR/Session$2.sublime_session" "$CURRENT_SESSION_DIR/Session.sublime_session"
    fi
    shift
    shift
fi

exec /opt/sublime_text/sublime_text "$@"

Now I can open a fresh sublime with subl whatever and if I need to open previous sessions I do subl -l 1 for the last one or 2 or 3 for the previous ones.

The script works by moving the Session.sublime_session file to a backup location so sublime doesn't know there was a last session, and it starts fresh. It also rotates the filenames of the 3 last saved sessions and deletes the oldest one if you've got more than 3, and recovers them if you specify the -l flag.

Hope someone finds this helpful.

WARNING: I'm no bash wizard. The script is probably terrible, but I got it to work without problems. If it doesn't behave like you think it should, don't blame me, modify it, fix it, improve it, share your results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment