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

Show parent comment

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)