Open Files in a GUI Editor from the Terminal

From LemonWiki共筆
Revision as of 10:53, 30 April 2026 by Planetoid (talk | contribs) (Created page with "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 `.ap...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How to open files with your favorite editor from the terminal?

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"