-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |