Skip to main content
added 141 characters in body
Source Link
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

There isStarting from 8.2.1794 vim has null coalescing operator aka falsy operator

let value = somevar ?? othervar ?? 100

:h ??

The expression before the '??' is evaluated.  If it evaluates to
|truthy|, this is used as the result.  Otherwise the expression after the '??'
is evaluated and used as the result.  This is most useful to have a default
value for an expression that may result in zero or empty: >
    echo theList ?? 'list is empty'
    echo GetName() ?? 'unknown'

Neovim uses quite an old vimscript so there is a doubt it would ever have this provided it is focused on lua.

There is also get function that could be used for cases like:

" my_var = g:my_var if it is defined, 100 otherwise
let my_var = get(g:, "my_var", 100)

" my_var = s:my_var if it is defined, 100 otherwise
let my_var = get(s:, "my_var", 100)

There is null coalescing operator aka falsy operator

let value = somevar ?? othervar ?? 100

:h ??

The expression before the '??' is evaluated.  If it evaluates to
|truthy|, this is used as the result.  Otherwise the expression after the '??'
is evaluated and used as the result.  This is most useful to have a default
value for an expression that may result in zero or empty: >
    echo theList ?? 'list is empty'
    echo GetName() ?? 'unknown'

There is also get function that could be used for cases like:

" my_var = g:my_var if it is defined, 100 otherwise
let my_var = get(g:, "my_var", 100)

" my_var = s:my_var if it is defined, 100 otherwise
let my_var = get(s:, "my_var", 100)

Starting from 8.2.1794 vim has null coalescing operator aka falsy operator

let value = somevar ?? othervar ?? 100

:h ??

The expression before the '??' is evaluated.  If it evaluates to
|truthy|, this is used as the result.  Otherwise the expression after the '??'
is evaluated and used as the result.  This is most useful to have a default
value for an expression that may result in zero or empty: >
    echo theList ?? 'list is empty'
    echo GetName() ?? 'unknown'

Neovim uses quite an old vimscript so there is a doubt it would ever have this provided it is focused on lua.

There is also get function that could be used for cases like:

" my_var = g:my_var if it is defined, 100 otherwise
let my_var = get(g:, "my_var", 100)

" my_var = s:my_var if it is defined, 100 otherwise
let my_var = get(s:, "my_var", 100)

Source Link
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

There is null coalescing operator aka falsy operator

let value = somevar ?? othervar ?? 100

:h ??

The expression before the '??' is evaluated.  If it evaluates to
|truthy|, this is used as the result.  Otherwise the expression after the '??'
is evaluated and used as the result.  This is most useful to have a default
value for an expression that may result in zero or empty: >
    echo theList ?? 'list is empty'
    echo GetName() ?? 'unknown'

There is also get function that could be used for cases like:

" my_var = g:my_var if it is defined, 100 otherwise
let my_var = get(g:, "my_var", 100)

" my_var = s:my_var if it is defined, 100 otherwise
let my_var = get(s:, "my_var", 100)