Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

enviromental variables into dot.emacs

35 views
Skip to first unread message

daniele.g

unread,
Aug 14, 2011, 11:50:31 AM8/14/11
to help-gn...@gnu.org
Can I put enviromental variables in my .emacs file? How?

Thanks in advance.
--
"Come va?".
"Bene, grazie. E tu?".
"Medio". Cosi' diceva sempre.
-- Enrico Brizzi, Jack Frusciante e' uscito dal gruppo


Tassilo Horn

unread,
Aug 14, 2011, 12:07:52 PM8/14/11
to help-gn...@gnu.org
dgi...@iol.it (daniele.g) writes:

Hi Daniele,

> Can I put enviromental variables in my .emacs file?

Sure you can.

> How?

,----[ C-h f setenv RET ]
| setenv is an interactive compiled Lisp function in `env.el'.
|
| (setenv VARIABLE &optional VALUE SUBSTITUTE-ENV-VARS)
|
| Set the value of the environment variable named VARIABLE to VALUE.
| VARIABLE should be a string. VALUE is optional; if not provided or
| nil, the environment variable VARIABLE will be removed.
|
| Interactively, a prefix argument means to unset the variable, and
| otherwise the current value (if any) of the variable appears at
| the front of the history list when you type in the new value.
| This function always replaces environment variables in the new
| value when called interactively.
|
| SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
| variables in VALUE with `substitute-env-vars', which see.
| This is normally used only for interactive calls.
|
| The return value is the new value of VARIABLE, or nil if
| it was removed from the environment.
|
| This function works by modifying `process-environment'.
|
| As a special case, setting variable `TZ' calls `set-time-zone-rule' as
| a side-effect.
`----

HTH,
Tassilo


daniele.g

unread,
Aug 14, 2011, 4:12:05 PM8/14/11
to help-gn...@gnu.org
Tassilo Horn <tas...@member.fsf.org> writes:

> Dgi...@iol.it (daniele.g) writes:
>
> Hi Daniele,

Hi Tassilo

There was a misunderstanding. I don't want to set an enviromental
variable, I want to _read_ it. For example, I want set up my email address
for Gnus from $USER and $HOSTNAME values. I know I can use getenv to
read them, but I don't know how to use them in my dot-emacs.

My aim is to unify my conf files making them picking as many values as
possible from the same place.
--
Tutti per uno, uno per tutti.
-- Dumas padre, "I tre moschettieri"


Michael Markert

unread,
Aug 14, 2011, 12:07:58 PM8/14/11
to daniele.g, help-gn...@gnu.org
On 14 Aug 2011, daniele g. wrote:

> Can I put enviromental variables in my .emacs file? How?

Yes, use `setenv`.

Michael

Jason Earl

unread,
Aug 14, 2011, 5:18:26 PM8/14/11
to
On Sun, Aug 14 2011, daniele.g wrote:

> Tassilo Horn <tas...@member.fsf.org> writes:
>
>> Dgi...@iol.it (daniele.g) writes:
>>
>> Hi Daniele,
>
> Hi Tassilo
>
> There was a misunderstanding. I don't want to set an enviromental
> variable, I want to _read_ it. For example, I want set up my email
> address for Gnus from $USER and $HOSTNAME values. I know I can use
> getenv to read them, but I don't know how to use them in my dot-emacs.

(getenv "USER") and such should work in your .emacs file.

> My aim is to unify my conf files making them picking as many values as
> possible from the same place.

That's a good goal.

Jason

Jay Belanger

unread,
Aug 14, 2011, 5:46:55 PM8/14/11
to help-gn...@gnu.org, jay.p.b...@gmail.com

> My aim is to unify my conf files making them picking as many values as
> possible from the same place.

Same here. I have COMPUTERNAME set to different values on different
computers, so my init file contains

(cond ((string= (getenv "COMPUTERNAME") "home")
... home computer specific stuff ...)
((string= (getenv "COMPUTERNAME") "office")
... office computer specific stuff ...)
((string= (getenv "COMPUTERNAME") "netbook")
... netbook specific stuff ...))

William F Hammond

unread,
Aug 14, 2011, 6:17:31 PM8/14/11
to
dgi...@iol.it (daniele.g) writes:

> There was a misunderstanding. I don't want to set an enviromental
> variable, I want to _read_ it. For example, I want set up my email address
> for Gnus from $USER and $HOSTNAME values. I know I can use getenv to
> read them, but I don't know how to use them in my dot-emacs.

Not that I still have email addresses that are so contructible,
but the code would be something like (code not tested):

(setq user-mail-address (concat (getenv "USER") "@" (getenv "HOSTNAME")))


-- Bill

daniele.g

unread,
Aug 14, 2011, 7:35:36 PM8/14/11
to help-gn...@gnu.org
Jay Belanger <jay.p.b...@gmail.com> writes:

>> My aim is to unify my conf files making them picking as many values as
>> possible from the same place.
>
> Same here. I have COMPUTERNAME set to different values on different
> computers, so my init file contains
>

> (Cond ((string= (getenv "COMPUTERNAME") "home")


> ... home computer specific stuff ...)

> ((String= (getenv "COMPUTERNAME") "office")


> ... office computer specific stuff ...)

> ((String= (getenv "COMPUTERNAME") "netbook")


> ... netbook specific stuff ...))

Forgive me, but my elisp skills are quite low. Can I use a syntax like
this?

(setq var (getenv $VAR))
--
Era cosi' povero che non poteva neanche permettersi di regalare uno
yo-yo al suo bambino per Natale. Fece in modo di regalargli uno yo.
-- Martin Kauffman


Jay Belanger

unread,
Aug 14, 2011, 8:01:03 PM8/14/11
to help-gn...@gnu.org, jay.p.b...@gmail.com

> Forgive me, but my elisp skills are quite low. Can I use a syntax like
> this?
>
> (setq var (getenv $VAR))

It will have to be (getenv "VAR"), and var will then be a string.


Stefan Monnier

unread,
Aug 14, 2011, 10:58:37 PM8/14/11
to
> There was a misunderstanding. I don't want to set an enviromental
> variable, I want to _read_ it. For example, I want set up my email address
> for Gnus from $USER and $HOSTNAME values. I know I can use getenv to
> read them, but I don't know how to use them in my dot-emacs.

Depends on what you wan to do with them.
BTW, instead of (getenv "USER") you can use `user-login-name' and
instead of (getenv "HOSTNAME") you may prefer to use `system-name'.


Stefan

Michael Markert

unread,
Aug 14, 2011, 4:28:28 PM8/14/11
to daniele.g, help-gn...@gnu.org
On 14 Aug 2011, daniele g. wrote:

> There was a misunderstanding. I don't want to set an enviromental
> variable, I want to _read_ it. For example, I want set up my email
> address for Gnus from $USER and $HOSTNAME values. I know I can use
> getenv to read them, but I don't know how to use them in my dot-emacs.
>

> My aim is to unify my conf files making them picking as many values as
> possible from the same place.

Do you think of this?
#+begin_src emacs-lisp
(cond
((and (string= (getenv "USER") "johndoe")
(string= (getenv "HOSTNAME") "bar"))
(setq user-mail-address "joh...@bar.com")
(require 'john))
((and (string= (getenv "USER") "janedoe")
(string= (getenv "HOSTNAME") "bar"))
(setq user-mail-address "joh...@bar.com")
(require 'jane))
(t
(setq user-mail-address (concat (getenv "USER") "@" (getenv "HOSTNAME")))))
#+end_src emacs-lisp

But note that $HOSTNAME is often not set. You can use `system-name'
here.

Michael

daniele.g

unread,
Aug 15, 2011, 4:47:28 AM8/15/11
to help-gn...@gnu.org
Michael Markert <markert...@googlemail.com> writes:

> On 14 Aug 2011, daniele g. wrote:
>
>> There was a misunderstanding. I don't want to set an enviromental
>> variable, I want to _read_ it. For example, I want set up my email
>> address for Gnus from $USER and $HOSTNAME values. I know I can use
>> getenv to read them, but I don't know how to use them in my dot-emacs.
>>
>> My aim is to unify my conf files making them picking as many values as
>> possible from the same place.
>
> Do you think of this?

Oh yesss!

> #+Begin_src emacs-lisp


> (cond
> ((and (string= (getenv "USER") "johndoe")
> (string= (getenv "HOSTNAME") "bar"))
> (setq user-mail-address "joh...@bar.com")
> (require 'john))
> ((and (string= (getenv "USER") "janedoe")
> (string= (getenv "HOSTNAME") "bar"))
> (setq user-mail-address "joh...@bar.com")
> (require 'jane))
> (t
> (setq user-mail-address (concat (getenv "USER") "@" (getenv "HOSTNAME")))))
> #+end_src emacs-lisp
>
> But note that $HOSTNAME is often not set. You can use `system-name'
> here.

Indeed, the hostname can be recall using the program hostname. This is
the further step, using shell commands within the file. :-)
--
- Grazie al cielo, ha lavoro. Chi?
- L'astronomo!!


0 new messages