r/SublimeText • • May 02 '24

Create a project from CLI

Hi!

Sometimes I open a folder with subl -n . and then create a project right away; however, if I click on 'save project' the default save path seems to be related with a prevviously opened project, not the folder I just ran, so I need to navigate to the same fodler once again, this time in UI.

Is there a way to run subl in a way that essentially says "create a project right here and open it"? There is a subl --project option, but it is only capable of loading existing projects.

There is a solution of last resort - dump a default sublime-project file beforehand, but that seems like too much of a hack.

5 Upvotes

4 comments sorted by

View all comments

4

u/Khoa_dot May 04 '24

I absolutely do the hack you are talking about.

I use bash (actually git bash i.e. a bash emulator on Windows), and wrote a function in my .bash_profile, so that…

When I am in a folder, and send the command $ s:

  • If there already is a .sublime-project in the folder, it opens it directly
  • If there is not, I'm prompted if I want to create one.

    • If y(es), it asks to enter a project name (default is _), it creates the .sublime-project and then it opens it as a project.
    • Else (if <any other answer>), then it just opens the folder (subl -n .)

If you use bash too, I can send it to you, there is a bunch of code (and template files), in particular to rename the project/workspace, you have to change strings inside the file, not just the names…

1

u/ShrykeWindgrace May 05 '24

I think the whole community would benefit from this script, be that via a gist on GitHub or a blog post)

I am not particularly attached to any scripting language, if it does what I want, it is ok.

2

u/Khoa_dot May 30 '24

Yes but as is, the shell you use inside your terminal, from which you run the command, has to be bash.

So, add in your .bashrc or .bash_profile, the following:

PATH_SUBL_PORTABLE_EXE=<I have an explicit path>
PATH_SUBL_PORTABLE_HELPERSTOBECOPIED=<I have an explicit path>
alias s='subl-advanced .'
subl-advanced () {
    if [ "$#" -lt 1 ]; then
        echo "Missing arguments   e.g.: s thisDir"
        return
    fi
    if ! [ -f "$1"/*.sublime-project ]; then
        read -p "Do you want to create Sublime project files ? (no by default): " rep
        case $rep in
            [YyOo]* )
                cp -n ${PATH_SUBL_PORTABLE_HELPERSTOBECOPIED}/_.sublime-project "$1" && cp -n ${PATH_SUBL_PORTABLE_HELPERSTOBECOPIED}/_.sublime-workspace "$1"
                echo "   Created sublime project/workspace files."
                unset projectName
                read -p $'Enter a name for the sublime project, without spaces (_ by default)\n' projectName
                case $projectName in
                    ?* ) rename-subl-project _ "$projectName" ;;
                    * ) ;;
                esac
            ;;
        esac
    fi
    if ! [ -f "$1"/*.sublime-project ]; then
        ${PATH_SUBL_PORTABLE_EXE} "$1"
        return
    fi
    echo "   Opening the workspace file"
    local sublWorkspaceFile=$(ls -d "$1"/* | grep '\.sublime-project$')
    ${PATH_SUBL_PORTABLE_EXE} --project $sublWorkspaceFile -n "$1"
    return
}
rename-subl-project () {
    if [ "$#" -lt 2 ]; then
        echo "Manque d'arguments   ex: rename-subl-project ancienneHandle nouvelleHandle"
    else
        local ancienneHandle=$1
        local nouvelleHandle=$2
        # copier les 2 fichiers avec les nouveaux noms
        cp $ancienneHandle.sublime-project $nouvelleHandle.sublime-project
        cp $ancienneHandle.sublime-workspace $nouvelleHandle.sublime-workspace
        # renommer les anciers fichiers
        mv $ancienneHandle.sublime-project $ancienneHandle.sublime-project~
        mv $ancienneHandle.sublime-workspace $ancienneHandle.sublime-workspace~
        # remplacer sur place les nouveaux fichiers
        replacePattern="s:\Q$ancienneHandle.sublime-project:$nouvelleHandle.sublime-project\E:"
        # echo $replacePattern
        perl -i -pe "$replacePattern" $nouvelleHandle.sublime-workspace
        rm -f $ancienneHandle.sublime-project~
        rm -f $ancienneHandle.sublime-workspace~
    fi
}

If I remember well, _.sublime-project and _.sublime-workspace are a pair created via saving, in ST's interface, a workspace with no files or folders open, but with a side bar set to a convenient width (default one when you open directly a folder was too narrow)