blob: 7faaa5fbf84bd465468e52022e652af02b85e44d [file] [log] [blame]
Doug Kearns4d427d42024-09-15 19:21:18 +02001vim9script
2# Vim9-script expressions
3
4
Doug Kearnsc273f1a2025-01-20 21:53:01 +01005# Operators
6
7# Ternary
8
9echo expr ? expr : expr
10
11echo lnum == 1 ? "top" : lnum
12echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
13
14echo lnum == 1
15 ? "top"
16 : lnum == 1000
17 ? "last"
18 : lnum
19echo lnum == 1 ?
20 "top" :
21 lnum == 1000 ?
22 "last" :
23 lnum
24
25echo true ? true : false
26echo 1 ? 1 : 0
27echo "foo" ? "foo" : "bar"
28echo foo ? foo : bar
29echo g:foo ? g:foo : g:bar
30echo $FOO ? $FOO : $BAR
31echo True() ? True() : False()
32echo @a ? @a : @b
33echo (1) ? (1) : (0)
34
35# Falsy
36
37echo expr ?? expr
38
39echo theList ?? 'list is empty'
40echo GetName() ?? 'unknown'
41
42echo theList
43 \ ?? 'list is empty'
44echo theList ??
45 \ 'list is empty'
46
47echo true ?? true
48echo 1 ?? 1
49echo "foo" ?? "foo"
50echo foo ?? foo
51echo g:foo ?? g:foo
52echo $FOO ?? $FOO
53echo True() ?? True()
54echo @a ?? @a
55echo (1) ?? (1)
56
57
Doug Kearns4d427d42024-09-15 19:21:18 +020058# Command {expr} arguments
59
60if true
61 echo true
62elseif false
63 echo false
64endif
65
66while true
67 break
68endwhile
69
70def Foo(): bool
71 return true
72enddef
73
Doug Kearnsc273f1a2025-01-20 21:53:01 +010074
75# Issue #14423 (vim.vim: Opt out of vimSearch*)
76
77:?truthy
78const truthy: number = false
79 ? (0
80 )
81 : (1
82 )
83echo truthy
84
85def Foo()
86 :?truthy
87 const truthy: number = false
88 ? (0
89 )
90 : (1
91 )
92 echo truthy
93enddef
94
95
96# Issue #16227 (Vimscript ternary expression highlighting)
97
98var foo = 'foo' # comment
99var bar = foo == 'foo' ? 'bar' : 'baz'
100var baz = foo == 'foo'
101 \ ? 'baz'
102 \ : 'bar'
103var qux = foo == 'foo'
104 ? 'qux' # comment
105 : 'qux' # comment
106echo qux ?? 'quux'
107