Skip to content

Commit

Permalink
top 5 nushell hacks (#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdncred committed May 15, 2024
1 parent ae6d29a commit 2e7be64
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Binary file added assets/images/nushell_hacks_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions blog/2024-05-15-top-nushell-hacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: Top 5 Nushell Hacks
author: The Nu Authors
author_site: https://twitter.com/nu_shell
author_image: https://www.nushell.sh/blog/images/nu_logo.png
excerpt: Here are the top 5 nushell hacks.
---

# Nushell Hacks

Listed below are the top nushell hacks that people often forget about or didn't know.

## 1. ends-with / starts-with operators
```nushell
โฏ ls | where name ends-with .toml or name starts-with Car
โ•ญโ”€#โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€nameโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€typeโ”€โ”ฌโ”€โ”€โ”€sizeโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€modifiedโ”€โ”€โ”€โ”€โ•ฎ
โ”‚ 0 โ”‚ Cargo.lock โ”‚ file โ”‚ 165.4 KB โ”‚ 22 minutes ago โ”‚
โ”‚ 1 โ”‚ Cargo.toml โ”‚ file โ”‚ 6.4 KB โ”‚ an hour ago โ”‚
โ”‚ 2 โ”‚ Cross.toml โ”‚ file โ”‚ 666 B โ”‚ an hour ago โ”‚
โ”‚ 3 โ”‚ rust-toolchain.toml โ”‚ file โ”‚ 1.1 KB โ”‚ an hour ago โ”‚
โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

## 2. easter egg

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).

## 3. abbr

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.

For instance, if you have this alias
```nushell
alias gwch = git whatchanged -p --abbrev-commit --pretty=medium
```
and then you type `gwch<ctrl+space>` it will expand on the prompt line to
```nushell
git whatchanged -p --abbrev-commit --pretty=medium
```

### keybinding
```js
{
name: abbr
modifier: control
keycode: space
mode: [emacs, vi_normal, vi_insert]
event: [
{ send: menu name: abbr_menu }
{ edit: insertchar, value: ' '}
]
}
```
### menu
```js
{
name: abbr_menu
only_buffer_difference: false
marker: "๐Ÿ‘€ "
type: {
layout: columnar
columns: 1
col_width: 20
col_padding: 2
}
style: {
text: green
selected_text: green_reverse
description_text: yellow
}
source: { |buffer, position|
scope aliases
| where name == $buffer
| each { |it| {value: $it.expansion }}
}
}
```
## 4. case-insensitive `where` with `ls`

People often forget that the `=~` and `!~` are regular expression operators, which means you can do a case-insensitive search like this.

```nushell
โฏ ls | where name =~ '(?i)car'
โ•ญโ”€#โ”€โ”ฌโ”€โ”€โ”€โ”€nameโ”€โ”€โ”€โ”€โ”ฌโ”€typeโ”€โ”ฌโ”€โ”€โ”€sizeโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€modifiedโ”€โ”€โ”€โ”€โ•ฎ
โ”‚ 0 โ”‚ Cargo.lock โ”‚ file โ”‚ 160.6 KB โ”‚ 8 hours ago โ”‚
โ”‚ 1 โ”‚ Cargo.toml โ”‚ file โ”‚ 6.4 KB โ”‚ 8 hours ago โ”‚
โ”‚ 2 โ”‚ car.txt โ”‚ file โ”‚ 0 B โ”‚ 11 seconds ago โ”‚
โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```

## 5. the magic of nushell datatype closures

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`.

```nushell
string: {|| if $in =~ '^#[a-fA-F\d]+' { $in } else { 'default' } }
```

This allows nushell to detect anything that looks like a hex color and display that color in nushell.

This allows you to take a file like the x11 rgb.txt file and parse it into colors.

rgb.txt
```nushell
โฏ open ~/Downloads/rgb.txt | lines | last 10
โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ 0 โ”‚ 139 0 0 DarkRed โ”‚
โ”‚ 1 โ”‚ 144 238 144 light green โ”‚
โ”‚ 2 โ”‚ 144 238 144 LightGreen โ”‚
โ”‚ 3 โ”‚ 220 20 60 crimson โ”‚
โ”‚ 4 โ”‚ 75 0 130 indigo โ”‚
โ”‚ 5 โ”‚ 128 128 0 olive โ”‚
โ”‚ 6 โ”‚ 102 51 153 rebecca purple โ”‚
โ”‚ 7 โ”‚ 102 51 153 RebeccaPurple โ”‚
โ”‚ 8 โ”‚ 192 192 192 silver โ”‚
โ”‚ 9 โ”‚ 0 128 128 teal โ”‚
โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
```
And after parsing the file looks similar to this.
![rgb](../assets/images/nushell_hacks_01.png)

0 comments on commit 2e7be64

Please sign in to comment.