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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# Nushell cheat sheet

## Data types

```nu
    > "12" | into int
```

> **converts string to integer**

---

```nu
    > date now | date to-timezone "Europe/London"
```

> **converts present date to provided time zone**

---

```nu
    > {'name': 'nu', 'stars': 5, 'language': 'Python'} | upsert language 'Rust'
```

> **updates a record's language and if none is specified inserts provided value**

---

```nu
    > [one two three] | to yaml
```

> **converts list of strings to yaml**

---

```nu
    > [[framework, language]; [Django, Python] [Lavarel, PHP]]
```

> **prints the table**

---

```nu
    > [{name: 'Robert' age: 34 position: 'Designer'}
     {name: 'Margaret' age: 30 position: 'Software Developer'}
     {name: 'Natalie' age: 50 position: 'Accountant'}
    ] | select name position
```

> **selects two columns from the table and prints their values**

## Strings

```nu
    > let name = "Alice"
    > $"greetings, ($name)!"
```

> **prints `greetings, Alice!`**

---

```nu
    > let string_list = "one,two,three" | split row ","
    $string_list

    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ 0 โ”‚ one   โ”‚
    โ”‚ 1 โ”‚ two   โ”‚
    โ”‚ 2 โ”‚ three โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

> **splits the string with specified delimiter and saves the list to `string_list` variable**

---

```nu
    "Hello, world!" | str contains "o, w"
```

> **checks if a string contains a substring and returns `boolean`**

---

```nu
    let str_list = [zero one two]
    $str_list | str join ','
```

> **joins the list of strings using provided delimiter**

---

```nu
    > 'Hello World!' | str substring 4..8
```

> **created a slice from a given string with start (4) and end (8) indices**

---

```nu
    > 'Nushell 0.80' | parse '{shell} {version}'

    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ # โ”‚  shell  โ”‚ version โ”‚
    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚ 0 โ”‚ Nushell โ”‚ 0.80    โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

> **parses the string to columns**

```nu
    > "acronym,long\nAPL,A Programming Language" | from csv
```

> **parses comma separated values (csv)**

```nu
    > $'(ansi purple_bold)This text is a bold purple!(ansi reset)'
```

> **ansi command colors the text (alsways end with `ansi reset` to reset color to default)**

## Lists

```nu
    > [foo bar baz] | insert 1 'beeze'

    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ 0 โ”‚ foo   โ”‚
    โ”‚ 1 โ”‚ beeze โ”‚
    โ”‚ 2 โ”‚ bar   โ”‚
    โ”‚ 3 โ”‚ baz   โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

> **inserts `beeze` value at st index in the list**

---

```nu
    > [1, 2, 3, 4] | update 1 10
```

> **updates 2nd value to 10**

---

```nu
    > let numbers = [1, 2, 3, 4, 5]
    > $numbers | prepend 0
```

> **adds value at the beginning of the list**

---

```nu
    > let numbers = [1, 2, 3, 4, 5]
    > $numbers | append 6
```

> **adds value at the end of the list**

---

```nu
    > let flowers = [cammomile marigold rose forget-me-not]
    > let flowers = ($flowers | first 2)
    > $flowers
```

> **creates slice of first two values from `flowers` list**

---

```nu
    > let planets = [Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune]
    > $planets | each { |it| $"($it) is a planet of solar system" }
```

> **iterates over a list; `it` is current list value**

---

```nu
    > $planets | enumerate | each { |it| $"($it.index + 1) - ($it.item)" }
```

> **iterates over a list and provides index and value in `it`**

---

```nu
    > let scores = [3 8 4]
    > $"total = ($scores | reduce { |it, acc| $acc + $it })"
```

> **reduces the list to a single value, `reduce` gives access to accumulator that is applied
> to each element in the list**

---

```nu
    > $"total = ($scores | reduce --fold 1 { |it, acc| $acc * $it })"
```

> **initial value for accumulator value can be set with `--fold`**

---

```nu
    > let planets = [Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune]
    > $planets.2
    > Earth
```

> **gives access to the 3rd item in the list**

---

```nu
    > let planets = [Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune]
    > $planets | any {|it| $it | str starts-with "E" }
    > true
```

> **checks if any string in the list starts with `E`**

---

```nu
    > let cond = {|x| $x < 0 }; [-1 -2 9 1] | take while $cond
    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ 0 โ”‚ -1 โ”‚
    โ”‚ 1 โ”‚ -2 โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ•ฏ
```

> **creates slice of items that satisfy provided condition**

## Tables

```nu
    > ls | sort-by size
```

> **sorting table by size of files**

---

```nu
    > ls | sort-by size | first 5
```

> **sorting table by size of files and show first 5 entries**

---

```nu
    > let $a = [[first_column second_column third_column]; [foo bar snooze]]
    > let $b = [[first_column second_column third_column]; [hex seeze feeze]]
    > $a | append $b

    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ # โ”‚ first_column โ”‚ second_column โ”‚ third_column โ”‚
    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚ 0 โ”‚ foo          โ”‚ bar           โ”‚ snooze       โ”‚
    โ”‚ 1 โ”‚ hex          โ”‚ seeze         โ”‚ feeze        โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

> **concatenate two tables with same columns**

---

```nu
    > let teams_scores = [[team score plays]; ['Boston Celtics' 311 3] ['Golden State Warriors', 245 2]]
    > $teams_scores | drop column

    โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ # โ”‚         team          โ”‚ score โ”‚
    โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚ 0 โ”‚ Boston Celtics        โ”‚   311 โ”‚
    โ”‚ 1 โ”‚ Golden State Warriors โ”‚   245 โ”‚
    โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

> **remove the last column of a table**

## Files & Filesystem

```nu
    > start file.txt
```

> **opens a text file with the default text editor**

---

```nu
    > 'lorem ipsum ' | save file.txt
```

> **saves a string to text file**

---

```nu
    > 'dolor sit amet' | save --append file.txt
```

> **appends a string to the end of file.txt**

---

```nu
    > { a: 1, b: 2 } | save file.json
```

> **saves a record to file.json**

---

```nu
    > glob **/*.{rs,toml} --depth 2
```

> **searches for `.rs` and `.toml` files recursively up to 2 folders deep**

---

```nu
    > watch . --glob=**/*.rs {|| cargo test }
```

> **runs cargo test whenever a Rust file changes**

---

## Custom Commands

```nu
    def greet [name: string] {
        $"hello ($nameยญ)"
    }
```

> **custom command with parameter type set to string**

---

```nu
    def greet [name = "nushell"] {
        $"hello ($nameยญ)"
    }
```

> **custom command with default parameter set to nushell**

---

```nu
    def greet [
        name: string
        --age: int
    ] {
        [$name $age]
    }

    > greet world --age 10
```

> **passing named parameter by defining flag for custom commands**

---

```nu
    def greet [
        name: string
        --age (-a): int
        --twice
    ] {
        if $twice {
            [$name $age $name $age]
        } else {
            [$name $age]
        }
    }
    > greet -a 10 --twice hello
```

> **using flag as a switch with a shorthand flag (-a) for the age**

---

```nu
    def greet [...name: string] {
        print "ยญhello all:"
        for $n in $name {
            print $n
        }
    }

    > greet earth mars jupiter venus
```

> **custom command which takes any number of positional arguments using rest params**

## Variables & Subexpressions

```nu
    > let val = 42
    > print $val
    42
```

> **an immutable variable cannot change its value after declaration**

---

```nu
    > let val = 42
    > do { let val = 101;  $val }
    101
    > $val
    42
```

> **shadowing variable (declaring variable with the same name in a different scope)**

---

```nu
    > mut val = 42
    > $val += 27
    > $val
    69
```

> **declaring a mutable variable with mut key word**

---

```nu
    > mut x = 0
    > [1 2 3] | each { $x += 1 }
```

> **closures and nested defs cannot capture mutable variables from their enviroยญnment.
> This expression results in error.**

```nu
    > const plugin = 'path/ยญto/ยญplugin'
    > register $plugin
```

> **a constant variable is immutable value which is fully evaluated at parse-time**

---

```nu
    > let files = (ls)
    > $files.naยญme?.0?
```

> **using question mark operator to return null instead of error if provided path is incorrect**

---

```nu
    > let big_files = (ls | where size > 10kb)
    >  $big_files
```

> **using subexpยญression by wrapping the expression with parentยญheses ()**

---

## Modules

```nu
    > module greetings {
        export def hello [name: string] {
            $"hello ($nameยญ)!"
        }

        export def hi [where: string] {
            $"hi ($where)!ยญ"
        }
    }

    > use greetings hello
    > hello "ยญworยญld"
```

> **using inline module**

---

```nu
    # greetiยญngs.nu
    export-env {
        $env.MยญYNAME = "ยญArthur, King of the Britonยญs"
    }
    export def hello [] {
        $"hello ($env.MยญYNยญAMEยญ)"
    }

    > use greetiยญngs.nu
    > $env.MยญYNAME
    Arthur, King of the Britons
    > greetings hello
    hello Arthur, King of the Britons!
```

> **importing module from file and using its enviroยญnment in current scope**

---

```nu
    # greetiยญngs.nu
    export def hello [name: string] {
        $"hello ($nameยญ)!"
    }

    export def hi [where: string] {
        $"hi ($where)!ยญ"
    }

    export def main [] {
        "ยญgreยญetings and salutations!"
    }

    > use greetiยญngs.nu
    > greetings
    greetings and salutaยญtions!
    > greetings hello world
    hello world!
```

> **using main command in module**

---