Sublime text
How to open existing text files with Sublime Text from terminal?
- Open the terminal
- Key in subl path/to/file.txt where subl is the alias shorthand for sublime text
Explanation
1. Determine which shell you're using by running this in Terminal:
echo $SHELL
2. Edit the appropriate configuration file:
My result on macOS
is
/bin/zsh
Choose appropriate configuration file
For Bash: ~/.bash_profile or ~/.bashrc For Zsh (default on newer macOS): ~/.zshrc
3. Open the file with a text editor:
vi ~/.zshrc
4. Existing alias in the file:
alias subl='open -a "Sublime Text"'
5. Save the file, then reload the configuration:
source ~/.zshrc
How to open files with your favorite editor from the terminal?
How to open and create non-existent files with Sublime Text from terminal?
1. First, edit your .zshrc file and remove the previously defined subl alias
2. Then add the new function definition
Open your
.zshrc
file:
vi ~/.zshrc
3. Find and remove the previous "subl" alias line, which looks something like:
alias subl='open -a "Sublime Text"'
Then add this function:
subl() {
for f in "$@"; do
[ -f "$f" ] || touch "$f"
done
open -a "Sublime Text" "$@"
}
This function creates a command subl that opens files in Sublime Text while automatically creating any non-existent files:
for f in "$@"- Loops through each filename you provide[ -f "$f" ] || touch "$f"- Checks if each file exists, creates it if notopen -a "Sublime Text" "$@"- Opens all files in Sublime Text
Simply put: It ensures all files exist before opening them in Sublime Text, preventing "file not found" errors.
4. Save the file, then reload the configuration:
source ~/.zshrc
5. Now it should work properly. Try creating a non-existent file:
subl non-exist.md
This should create the "non-exist.md" file and open it in Sublime Text.