Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Sublime text
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[https://www.sublimetext.com/ Sublime Text - Text Editing, Done Right] == How to open existing text files with Sublime Text from terminal? == * Open the terminal * 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 === 1. Determine which shell you're using by running this in Terminal: <pre> echo $SHELL </pre> 2. Edit the appropriate configuration file: My result on {{Mac}} is <pre> /bin/zsh </pre> Choose appropriate configuration file <pre> For Bash: ~/.bash_profile or ~/.bashrc For Zsh (default on newer macOS): ~/.zshrc </pre> 3. Open the file with a text editor: <pre> vi ~/.zshrc </pre> 4. Existing alias in the file: <pre> alias subl='open -a "Sublime Text"' </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? == [[Open Files in a GUI Editor from the Terminal]] == 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 == * [https://www.folkstalk.com/tech/open-file-in-sublime-from-terminal-with-code-examples/ Open File In Sublime From Terminal With Code Examples] * [https://www.sublimetext.com/docs/command_line.html Command Line Interface] [[Category: Software]] [[Category: Text editor]] [[Category: Revised with LLMs]] [[Category: Developer environment]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Kbd
(
edit
)
Template:Mac
(
edit
)