Open Files in a GUI Editor from the Terminal

From LemonWiki共筆
Revision as of 11:00, 30 April 2026 by Planetoid (talk | contribs)
Jump to navigation Jump to search

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

How to open files with your favorite editor from the terminal on macOS

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 files with your favorite editor from the terminal on Ubuntu

Most GUI editors on Ubuntu install a CLI command automatically, so you can often skip straight to using them:

code .      # VS Code
cursor .    # Cursor
zed .       # Zed
subl .      # Sublime Text

If the command isn't found, follow the steps below.

1. Find the editor's executable path:

which code
# or
find /usr/bin /usr/local/bin /snap/bin -name "code" 2>/dev/null

2. Open your shell config file:

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

3. Add an alias pointing to the executable:

alias code="/usr/bin/code"
alias cursor="/usr/local/bin/cursor"

4. Reload your config:

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

One-liner shortcut:

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

# Append the alias (replace the path with your actual executable path)
echo 'alias code="/usr/bin/code"' >> "$RC_FILE"

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

Tip: Snap-installed editors (common on Ubuntu) are usually found under /snap/bin/. Run ls /snap/bin/ to check.