blob: fd9cbb0091e225aa904466665aad335bc926c5ff [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
Doug Kearnsa9c06422025-02-12 20:44:17 +010033echo (true) ? (true) : (false)
Doug Kearnsc273f1a2025-01-20 21:53:01 +010034echo (1) ? (1) : (0)
35
36# Falsy
37
38echo expr ?? expr
39
40echo theList ?? 'list is empty'
41echo GetName() ?? 'unknown'
42
43echo theList
44 \ ?? 'list is empty'
45echo theList ??
46 \ 'list is empty'
47
48echo true ?? true
49echo 1 ?? 1
50echo "foo" ?? "foo"
51echo foo ?? foo
52echo g:foo ?? g:foo
53echo $FOO ?? $FOO
54echo True() ?? True()
55echo @a ?? @a
Doug Kearnsa9c06422025-02-12 20:44:17 +010056echo (true) ?? (true)
Doug Kearnsc273f1a2025-01-20 21:53:01 +010057echo (1) ?? (1)
58
59
Doug Kearnsa9c06422025-02-12 20:44:17 +010060# Function calls
61
62Foo(true, false, null)
63
64
Doug Kearns4d427d42024-09-15 19:21:18 +020065# Command {expr} arguments
66
67if true
68 echo true
69elseif false
70 echo false
71endif
72
73while true
74 break
75endwhile
76
77def Foo(): bool
78 return true
79enddef
80
Doug Kearnsc273f1a2025-01-20 21:53:01 +010081
82# Issue #14423 (vim.vim: Opt out of vimSearch*)
83
84:?truthy
85const truthy: number = false
86 ? (0
87 )
88 : (1
89 )
90echo truthy
91
92def Foo()
93 :?truthy
94 const truthy: number = false
95 ? (0
96 )
97 : (1
98 )
99 echo truthy
100enddef
101
102
103# Issue #16227 (Vimscript ternary expression highlighting)
104
105var foo = 'foo' # comment
106var bar = foo == 'foo' ? 'bar' : 'baz'
107var baz = foo == 'foo'
108 \ ? 'baz'
109 \ : 'bar'
110var qux = foo == 'foo'
111 ? 'qux' # comment
112 : 'qux' # comment
113echo qux ?? 'quux'
114