In the linux shell (zsh), I would like to pass an argument to an environment variable to…
- User
In the linux shell (zsh), I would like to pass an argument to an environment variable to become active when the shell starts. The variable is EDITOR and I would like to set it to "emacs -nw", so I set it like this EDITOR="emacs -nw". But when I enter $EDITOR from my terminal, it says: "command not found: emacs -nw". Without "-nw" there is no problem. How do I set an environment variable to contain an argument?
Assistant
You've hit a common point of confusion with how the shell interprets commands and variables.
The Problem with
$EDITORdirectly in the terminalWhen you type
$EDITORin your terminal and press Enter, the shell performs variable expansion. IfEDITOR="emacs -nw", then$EDITORexpands to the literal stringemacs -nw. The shell then tries to find a single command namedemacs -nw(with the space as part of the command name). Since there's no such command (the command isemacsand-nwis an argument), you get "command not found: emacs -nw".When
EDITOR="emacs",$EDITORexpands toemacs. The shell finds the commandemacsand runs it. This works.How Programs Use
EDITORPrograms that use the
EDITORenvironment variable (likegit,crontab -e,fc, etc.) are smarter. They typically:- Read the value of the
EDITORvariable. - Split the string by spaces.
- Take the first part as the command to execute (
emacs). - Take the subsequent parts as arguments to that command (
-nw).
So, setting
EDITOR="emacs -nw"is correct for these programs.How to Set It Correctly (and Where)
-
The setting is correct:
export EDITOR="emacs -nw"The
exportcommand makes the variable available to child processes (which is what you want when another program needs to useEDITOR). -
Where to put it: For Zsh, the best place to set environment variables that should be available to all processes (including graphical applications started from the shell environment and non-interactive shells) is
~/.zshenv. Add this line to your~/.zshenvfile:export EDITOR="emacs -nw"If you also want to set
VISUAL(often preferred, withEDITORas a fallback), you can do:export VISUAL="emacs -nw" export EDITOR="$VISUAL" # Or set EDITOR to something else like "vi" if preferred as a fallbackVISUALis generally for full-screen editors, whileEDITORwas traditionally for line editors. Most modern applications will checkVISUALfirst, thenEDITOR. -
Apply the changes: Either source the file:
source ~/.zshenvOr simply open a new terminal window/tab.
How to Test If It's Working (the right way)
Don't just type
$EDITOR. Instead, use a command that relies on theEDITORvariable:-
fc(Fix Command): This is a shell builtin that usually opens the last command in your$EDITOR.# Type some dummy command first if your history is empty or you want a specific one echo "this is a test" fcThis should open
emacs -nwwith theechocommand in it. -
crontab -e: Tries to edit your crontab.crontab -e(You can just exit emacs without saving if you don't want to change your crontab).
-
git commit --amend(if you're in a git repo): If you're in a Git repository and have made a commit, this will try to open the commit message in your editor.
If you want to run
$EDITORdirectly in the shell (for testing/convenience)While not how programs use it, if you want to execute the command stored in
EDITORdirectly from your interactive shell prompt, you have a couple of options:-
eval(use with caution):evaltells the shell to evaluate the string as a full command line.eval $EDITOR somefile.txtThis would expand to
eval emacs -nw somefile.txt, and the shell would then correctly parse and executeemacs -nw somefile.txt. Be careful withevalif the variable could come from an untrusted source, as it can execute arbitrary code. ForEDITORyou set yourself, it's generally fine. -
Shell function/alias (better for interactive use): If you frequently want to type a short command to launch
emacs -nw, create an alias or function in your~/.zshrc:# In ~/.zshrc alias enw='emacs -nw' # or a function # enw() { emacs -nw "$@" }Then just type
enw somefile.txt.
In summary: Your
EDITOR="emacs -nw"setting is correct for its intended purpose. The way you were testing it ($EDITORdirectly) was misleading. Use commands likefcto verify it. Place theexport EDITOR="emacs -nw"line in your~/.zshenv.- Read the value of the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.