[vim/vim] `:unlet $FOOBAR` (#1116)

88 views
Skip to first unread message

Luc Hermitte

unread,
Sep 25, 2016, 6:11:14 PM9/25/16
to vim/vim

When an environment variable isn't set, exists() return false. We can assign a value to the variable with :let, after which point it exists. But we can't revert to an non-existent state (related to unsetenv (3C))

call assert_false(exists('$FOOBAR'))
let $FOOBAR = 1
call assert_true(exists('$FOOBAR'))
unlet $FOOBAR
call assert_false(exists('$FOOBAR'))


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

Bram Moolenaar

unread,
Sep 26, 2016, 3:58:23 PM9/26/16
to vim/vim

It's in the todo list:

Can we make ":unlet $VAR" use unsetenv() to delete the env var?
What for systems that don't have unsetenv()?

Iwan

unread,
Oct 30, 2017, 2:26:54 PM10/30/17
to vim/vim, Subscribed

If I would want to implement this, where do I start?

Andy Wokula

unread,
Oct 30, 2017, 2:32:18 PM10/30/17
to vim...@googlegroups.com
Am 26.09.2016 um 00:10 schrieb Luc Hermitte (Vim Github Repository):
> When an environment variable isn't set, `exists()` return false. We can assign a value to the variable with `:let`, after which point it exists. But we can't revert to an non-existent state (related to `unsetenv (3C)`)
>
> ```vim
> call assert_false(exists('$FOOBAR'))
> let $FOOBAR = 1
> call assert_true(exists('$FOOBAR'))
> unlet $FOOBAR
> call assert_false(exists('$FOOBAR'))
> ```

At least on Windows, assigning the empty string "unlets" it.

--
Andy

Tony Mechelynck

unread,
Oct 30, 2017, 5:30:18 PM10/30/17
to vim_dev
OTOH, on Linux, after assigning the empty string, exists('$FOOBAR')
still returns 1, while ":unlet $FOOBAR" gives error E488: Trailing
characters.

The classical way to test for "empty or undefined" is

if ("X" . $FOOBAR) == "X"

which, AFAICT, returns true even if $FOOBAR is undefined (i.e. has
never been set in this environment).

Best regards,
Tony.

Luc Hermitte

unread,
Oct 30, 2017, 9:28:02 PM10/30/17
to vim dev
Hi,

> OTOH, on Linux, after assigning the empty string, exists('$FOOBAR')
> still returns 1, while ":unlet $FOOBAR" gives error E488: Trailing
> characters.
>
> The classical way to test for "empty or undefined" is
>
> if ("X" . $FOOBAR) == "X"
>
> which, AFAICT, returns true even if $FOOBAR is undefined (i.e. has
> never been set in this environment).

We can also test for "is set" with

[[ -n ${FOOBAR+x} ]] && echo 1

or more simply with bash 4.2+

[[ -v FOOBAR ]] && echo 1

Anyway, the distinction becomes interesting as a few programs interpret the unset state as "use a default value", while an empty string really means an empty string.

Given foo.c, and a properly configured gnumake (i.e. not MinGW make), the following will yield different results

$> CC=clang make foo
# prints: clang foo.c -o foo

$> CC= make foo
# prints: foo.c -o foo
# and can't compile anything

$> unset CC # just to assert the situation
$> make foo
# prints: cc foo.c -o foo

This is the recurrent use case I run into that's behind the issue I've opened a while back.

Regards,

--
Luc Hermitte

mattn

unread,
May 1, 2018, 2:52:51 AM5/1/18
to vim/vim, Subscribed

As I mentioned in #2854, Vim doesn't have a way to revert $TZ. So we should implement unlet $FOOBAR.

Jason Franklin

unread,
May 31, 2018, 8:32:18 AM5/31/18
to vim/vim, Subscribed

It looks like this issue can be closed. Patch 8.0.1832 took care of this.

K.Takata

unread,
May 31, 2018, 8:34:10 AM5/31/18
to vim/vim, Subscribed

closing.

K.Takata

unread,
May 31, 2018, 8:34:12 AM5/31/18
to vim/vim, Subscribed

Closed #1116.

Reply all
Reply to author
Forward
0 new messages