Open Files in a GUI Editor from the Terminal

From LemonWiki共筆
Revision as of 11:14, 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.

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

On Windows, you can launch GUI editors from the terminal using either Command Prompt or PowerShell. Most modern editors (VS Code, Cursor) install a CLI command automatically during setup.

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:

Open File Explorer and look under common install locations:

C:\Users\<YourName>\AppData\Local\Programs\
C:\Program Files\
C:\Program Files (x86)\

Or search from PowerShell:

Get-Command code -ErrorAction SilentlyContinue
# or
where.exe code

2. Add the executable to your PATH (recommended):

  1. Open Start → search Environment Variables → click Edit the system environment variables
  2. Click Environment Variables...
  3. Under User variables, select Path → click Edit
  4. Click New and paste the folder path containing the editor's .exe
  5. Click OK to save, then restart your terminal

3. (Optional) Add a PowerShell alias:

Open your PowerShell profile:

notepad $PROFILE

If the file doesn't exist yet, create it first:

New-Item -ItemType File -Path $PROFILE -Force

Add an alias:

function code { & "C:\Users\<YourName>\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd" @args }

Save and reload:

. $PROFILE

One-liner shortcut (PowerShell):

# Append alias to your PowerShell profile and reload it
Add-Content $PROFILE 'function code { & "C:\Users\<YourName>\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd" @args }'
. $PROFILE

Tip: Using Windows Terminal + PowerShell 7 is recommended for the best developer experience on Windows. Install both from the Microsoft Store.