|
| 1 | +--- |
| 2 | +title: Top 5 Nushell Hacks |
| 3 | +author: The Nu Authors |
| 4 | +author_site: https://twitter.com/nu_shell |
| 5 | +author_image: https://www.nushell.sh/blog/images/nu_logo.png |
| 6 | +excerpt: Here are the top 5 nushell hacks. |
| 7 | +--- |
| 8 | + |
| 9 | +# Nushell Hacks |
| 10 | + |
| 11 | +Listed below are the top nushell hacks that people often forget about or didn't know. |
| 12 | + |
| 13 | +## 1. ends-with / starts-with operators |
| 14 | +```nushell |
| 15 | +❯ ls | where name ends-with .toml or name starts-with Car |
| 16 | +╭─#─┬────────name─────────┬─type─┬───size───┬────modified────╮ |
| 17 | +│ 0 │ Cargo.lock │ file │ 165.4 KB │ 22 minutes ago │ |
| 18 | +│ 1 │ Cargo.toml │ file │ 6.4 KB │ an hour ago │ |
| 19 | +│ 2 │ Cross.toml │ file │ 666 B │ an hour ago │ |
| 20 | +│ 3 │ rust-toolchain.toml │ file │ 1.1 KB │ an hour ago │ |
| 21 | +╰───┴─────────────────────┴──────┴──────────┴────────────────╯ |
| 22 | +``` |
| 23 | + |
| 24 | +## 2. easter egg |
| 25 | + |
| 26 | +Did you know that when you start nushell, the `$env.CMD_DURATION_MS` value of 0823 is an easter egg? [It's nushell's first pubic release date month day](https://github.com/nushell/nushell/releases/tag/0.2.0). |
| 27 | + |
| 28 | +## 3. abbr |
| 29 | + |
| 30 | +With this menu and keybinding configured appropriately in your config.nu file, you can hit `ctrl+space` and your alias abbreviation will expand to it's non-abbreviated form. |
| 31 | + |
| 32 | +For instance, if you have this alias |
| 33 | +```nushell |
| 34 | +alias gwch = git whatchanged -p --abbrev-commit --pretty=medium |
| 35 | +``` |
| 36 | +and then you type `gwch<ctrl+space>` it will expand on the prompt line to |
| 37 | +```nushell |
| 38 | +git whatchanged -p --abbrev-commit --pretty=medium |
| 39 | +``` |
| 40 | + |
| 41 | +### keybinding |
| 42 | +```js |
| 43 | + { |
| 44 | + name: abbr |
| 45 | + modifier: control |
| 46 | + keycode: space |
| 47 | + mode: [emacs, vi_normal, vi_insert] |
| 48 | + event: [ |
| 49 | + { send: menu name: abbr_menu } |
| 50 | + { edit: insertchar, value: ' '} |
| 51 | + ] |
| 52 | + } |
| 53 | +``` |
| 54 | +### menu |
| 55 | +```js |
| 56 | + { |
| 57 | + name: abbr_menu |
| 58 | + only_buffer_difference: false |
| 59 | + marker: "👀 " |
| 60 | + type: { |
| 61 | + layout: columnar |
| 62 | + columns: 1 |
| 63 | + col_width: 20 |
| 64 | + col_padding: 2 |
| 65 | + } |
| 66 | + style: { |
| 67 | + text: green |
| 68 | + selected_text: green_reverse |
| 69 | + description_text: yellow |
| 70 | + } |
| 71 | + source: { |buffer, position| |
| 72 | + scope aliases |
| 73 | + | where name == $buffer |
| 74 | + | each { |it| {value: $it.expansion }} |
| 75 | + } |
| 76 | + } |
| 77 | +``` |
| 78 | +## 4. case-insensitive `where` with `ls` |
| 79 | + |
| 80 | +People often forget that the `=~` and `!~` are regular expression operators, which means you can do a case-insensitive search like this. |
| 81 | + |
| 82 | +```nushell |
| 83 | +❯ ls | where name =~ '(?i)car' |
| 84 | +╭─#─┬────name────┬─type─┬───size───┬────modified────╮ |
| 85 | +│ 0 │ Cargo.lock │ file │ 160.6 KB │ 8 hours ago │ |
| 86 | +│ 1 │ Cargo.toml │ file │ 6.4 KB │ 8 hours ago │ |
| 87 | +│ 2 │ car.txt │ file │ 0 B │ 11 seconds ago │ |
| 88 | +╰───┴────────────┴──────┴──────────┴────────────────╯ |
| 89 | +``` |
| 90 | + |
| 91 | +## 5. the magic of nushell datatype closures |
| 92 | + |
| 93 | +In your config.nu file you can have themes that color nushell datatypes a particular color. One thing that is sometimes overlooked is that these colors can also be closures, like the one below for `string`. |
| 94 | + |
| 95 | +```nushell |
| 96 | +string: {|| if $in =~ '^#[a-fA-F\d]+' { $in } else { 'default' } } |
| 97 | +``` |
| 98 | + |
| 99 | +This allows nushell to detect anything that looks like a hex color and display that color in nushell. |
| 100 | + |
| 101 | +This allows you to take a file like the x11 rgb.txt file and parse it into colors. |
| 102 | + |
| 103 | +rgb.txt |
| 104 | +```nushell |
| 105 | +❯ open ~/Downloads/rgb.txt | lines | last 10 |
| 106 | +╭───┬───────────────────────────────────╮ |
| 107 | +│ 0 │ 139 0 0 DarkRed │ |
| 108 | +│ 1 │ 144 238 144 light green │ |
| 109 | +│ 2 │ 144 238 144 LightGreen │ |
| 110 | +│ 3 │ 220 20 60 crimson │ |
| 111 | +│ 4 │ 75 0 130 indigo │ |
| 112 | +│ 5 │ 128 128 0 olive │ |
| 113 | +│ 6 │ 102 51 153 rebecca purple │ |
| 114 | +│ 7 │ 102 51 153 RebeccaPurple │ |
| 115 | +│ 8 │ 192 192 192 silver │ |
| 116 | +│ 9 │ 0 128 128 teal │ |
| 117 | +╰───┴───────────────────────────────────╯ |
| 118 | +``` |
| 119 | +And after parsing the file looks similar to this. |
| 120 | + |
0 commit comments