bcarlin.net/content/blog/005-automatically-open-sublime-text-projects-in-a-directory/index.en.md

40 lines
1.2 KiB
Markdown
Raw Normal View History

2025-06-07 02:33:02 +02:00
---
title: Automatically open Sublime Text projects in a directory
2025-06-20 02:33:45 +02:00
slug: 5-automatically-open-sublime-text-projects-in-a-directory
2025-06-07 02:33:02 +02:00
date: "2013-05-15T00:00:00+02:00"
2025-06-20 02:33:45 +02:00
categories:
- Tooling
tags:
- Sublime Text
- Bash
2025-06-07 02:33:02 +02:00
summary: >
How to automatically open Sublimetext with a file, a project or the current
directory according to the context.
---
I usually start Sublime Text 2 from the command line to work, depending
on the case, on the content of a directory or on a project (materialized
with a `*.sublime-project` file).
It ends up with one of the following commands :
- `subl .`
- `subl my-project.sublime-project`
2025-06-20 02:33:45 +02:00
Here is the snippet I added to my `.bashrc` file to have the `subl`
2025-06-07 02:33:02 +02:00
command automatically "guess" what I want. It does the following:
2025-06-20 02:33:45 +02:00
- If a path is given (`subl "my/file.txt"`), it opens the file.
- If nothing is given and a `.sublime-project` file exists in the current
2025-06-07 02:33:02 +02:00
directory, it opens it
2025-06-20 02:33:45 +02:00
- If nothing is given and no `.sublime-project` file has been found, it
2025-06-07 02:33:02 +02:00
opens the folder.
2025-06-20 02:33:45 +02:00
```bash
2025-06-07 02:33:02 +02:00
function project_aware_subl {
project_file=$(ls *.sublime-project 2>/dev/null | head -n 1)
subl ${*:-${project_file:-.}}
}
alias subl="project_aware_subl"
2025-06-20 02:33:45 +02:00
```