Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I often need to change a small part of long environment variable (especially, e.g., paths), and do it either by pasting the thing into an editor and changing it there, or the equivalent.

Is there some small convenience utility to edit environment variables with a cursor on the command line?

I suppose I could always whip one up, but am hoping there's already something that I'm just not aware of.

share|improve this question

2 Answers 2

Can't think of one that would let you interactively edit it directly on the command line but there are ways to make it more convenient than what you're describing. If it's a short term change and you just need to add something to the path, you can self reference the current value. For example, in Bash:

PATH=$PATH:/ADDITIONAL/PATH

Or if you want to alter precedence, you can prepend it with

PATH=/ADDITIONAL/PATH:$PATH

You can also save yourself a cut and paste by echoing the value into a temporary file

echo $PATH > tempfile.txt

If you have more complex or permanent changes to make, you can reference and edit your environment variables in your equivalent rc and or profile file. If they're not already present, you can use the echo method to get them in an editable state, (just make sure to use >> to append instead of overwrite). Or, in vi, you can also use

:r!echo $PATH

In emacs it would be

C-u M-! echo $PATH

To insert the output into the file you're working on.

share|improve this answer
    
These are all things that I do at the moment. I'm trying to make temporary changes to variables, and I'm looking for something with fewer steps to smooth my work flow. –  Catskul Sep 22 '11 at 17:50

This is what I use (ksh syntax but should just work with bash too):

function viPath
{
  typeset _path=${1:-PATH}
  typeset _separator=${2:-:}
  eval echo "\$$_path" | tr $_separator '\n' >~/.path
  vi ~/.path
  eval _path=$(cat ~/.path | tr '\n' $_separator)
  eval ${1:-PATH}=$(eval echo $_path | sed 's/.$//')
  \rm ~/.path
}
share|improve this answer
    
This looks promising, but I didn't get it to quite work. I'll have to tinker with it when I get a chance. –  Catskul Sep 22 '11 at 17:50
    
What doesn't work ? –  jlliagre Sep 22 '11 at 21:19
    
Not certain, but the PATH is empty upon save/exit, ":wq". I still have yet to try to debug. –  Catskul Sep 23 '11 at 17:07
    
I guess you are using it as a shell script. It must be a function for the variable change to persist. –  jlliagre Sep 23 '11 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.