Sublime text: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
[https://www.sublimetext.com/ Sublime Text - Text Editing, Done Right]
[https://www.sublimetext.com/ Sublime Text - Text Editing, Done Right]


== How do I open a Sublime Text file in terminal ==
== How to open existing text files with Sublime Text from terminal? ==


* Open the terminal
* Open the terminal
* Key in {{kbd | key=<nowiki>subl path/to/file.txt</nowiki>}}
* Key in {{kbd | key=<nowiki>subl path/to/file.txt</nowiki>}} where {{kbd | key=<nowiki>subl</nowiki>}} is the alias shorthand for sublime text


=== Explanation ===
=== Explanation ===
Line 35: Line 35:
alias subl='open -a "Sublime Text"'
alias subl='open -a "Sublime Text"'
</pre>
</pre>
5. Save the file, then reload the configuration:
<pre lang="bash">
source ~/.zshrc
</pre>
== How to open files with your favorite editor from the terminal? ==
1. Open your shell config file:
<pre>
vi ~/.zshrc    # for zsh
vi ~/.bashrc  # for bash
</pre>
2. Add an alias for your editor:
<pre>
# VS Code
alias code="open -a 'Visual Studio Code'"
<pre>
Tip: Not sure of the exact app name? Run this to find it:
<pre>
ls /Applications/ | grep -i "visual\|cursor\|zed\|sublime"
</pre>
The name must exactly match the `.app` filename in `/Applications/` (without the `.app` extension).
3. Reload your config:
<pre>
source ~/.zshrc    # for zsh
source ~/.bashrc  # for bash
</pre>
One-liner shortcut:
<pre>
# Detect current shell and set the corresponding config file
RC_FILE="$HOME/.$(basename $SHELL)rc"
# Append the alias to the config file (>> appends without overwriting)
echo 'alias code="open -a Visual\ Studio\ Code"' >> "$RC_FILE"
# Reload the config file so the alias takes effect immediately
source "$RC_FILE"
</pre>
== How to open and create non-existent files with Sublime Text from terminal? ==
1. First, edit your <code>.zshrc</code> file and remove the previously defined <code>subl</code> alias
2. Then add the new function definition
Open your <pre>.zshrc</pre> file:
<pre lang="bash">
vi ~/.zshrc
</pre>
3. Find and remove the previous "subl" alias line, which looks something like:
<pre lang="bash">
alias subl='open -a "Sublime Text"'
</pre>
Then add this function:
<pre lang="bash">
subl() {
  for f in "$@"; do
    [ -f "$f" ] || touch "$f"
  done
  open -a "Sublime Text" "$@"
}
</pre>
This function creates a command <code>subl</code> that opens files in Sublime Text while automatically creating any non-existent files:
# <code>for f in "$@"</code> - Loops through each filename you provide
# <code>[ -f "$f" ] || touch "$f"</code> - Checks if each file exists, creates it if not
# <code>open -a "Sublime Text" "$@"</code> - 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:
<pre lang="bash">
source ~/.zshrc
</pre>
5. Now it should work properly. Try creating a non-existent file:
<pre lang="bash">
subl non-exist.md
</pre>
This should create the "non-exist.md" file and open it in Sublime Text.


== References ==
== References ==

Latest revision as of 18:09, 28 April 2026

Sublime Text - Text Editing, Done Right

How to open existing text files with Sublime Text from terminal?[edit]

  • Open the terminal
  • Key in subl path/to/file.txt where subl is the alias shorthand for sublime text

Explanation[edit]

1. Determine which shell you're using by running this in Terminal:

echo $SHELL

2. Edit the appropriate configuration file:

My result on macOS icon_os_mac.png 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?[edit]

1. Open your shell config file:

vi ~/.zshrc    # for zsh
vi ~/.bashrc   # for bash

2. Add an alias for your editor:

# VS Code
alias code="open -a 'Visual Studio Code'"
<pre>

Tip: Not sure of the exact app name? Run this to find it:
<pre>
ls /Applications/ | grep -i "visual\|cursor\|zed\|sublime"

The name must exactly match the `.app` filename in `/Applications/` (without the `.app` extension).

3. Reload your config:

source ~/.zshrc    # for zsh
source ~/.bashrc   # for bash


One-liner shortcut:

# Detect current shell and set the corresponding config file
RC_FILE="$HOME/.$(basename $SHELL)rc"

# Append the alias to the config file (>> appends without overwriting)
echo 'alias code="open -a Visual\ Studio\ Code"' >> "$RC_FILE"

# Reload the config file so the alias takes effect immediately
source "$RC_FILE"

How to open and create non-existent files with Sublime Text from terminal?[edit]

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:

  1. for f in "$@" - Loops through each filename you provide
  2. [ -f "$f" ] || touch "$f" - Checks if each file exists, creates it if not
  3. open -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.

References[edit]