commit - /dev/null
commit + 155837cce7f5d654cf7992feb5d32cccd47416f1
blob - /dev/null
blob + e788283d0347159b01def972b59d7e2f3ec06f8a (mode 644)
--- /dev/null
+++ README.md
+# nushell-snippets
+
+A snippet search and collection tool written in and for [nushell](https://github.com/nushell/nushell).
+
+This version is based on the nushell nuon table format. The first version was compatible with TextMate and VSCode snippet's JSON format, but that proved somewhat cumbersome. Using nuon makes life easier and the VSCode JSON format currently isn't one of my use cases. If someone is interested, I can look into creating a compatible version again.
+
+## Usage
+
+### Searching for entries
+If multiple search terms are given, the search terms are applied in a "fold" operation, i.e. first the 1. term is searched for, then the second term is search for on the result of the first term, and so on.
+
+Search for entries and show them with the found words highlighted, the regexes search in an case insensitive manner:
+```nushell
+snip show nmap
+```
+
+Same as above but further limit and refine the search:
+```nushell
+snip show nmap version
+```
+
+Search for entries and output the unaltered nuon table structure:
+```nushell
+snip show nmap version -n
+```
+
+Search for entries and output the body of the first found entry:
+```nushell
+snip show nmap version -g 0
+```
+
+This can then be used in all kinds of manners, e.g.
+
+Replace the commandline with the selected command
+```
+snip show nmap version -g 0 | commandline edit -r $in
+```
+
+Copy the selected command into the tmux buffer:
+```
+snip show nmap version -g 0 | tmux setb $in
+```
+
+Copy the selected command into the clipboard buffer using xclip:
+```
+snip show nmap version -g 0 | xclip -selection clipboard
+```
+
+Using the new `tee` command to print the command and then to copy it into the clipboard buffer using xclip:
+```
+snip show nmap version -g 0 | tee { to text | print } | xclip -selection clipboard
+```
+
+### Adding entries
+You can add entries using the `snip add` command:
+
+```
+snip add "list users using smb null session #win #enum" "netexec smb <dc-ip> -u <username> -p <password> --sam"
+```
+
+You can also add lists, both for the description and the body, if you want, e.g. if you have multiple steps, or want to save multiple links, etc.
+
+As a convenience `snip edit` opens `$env.snippets-path` in the `$env.EDITOR`.
+
+
+## Dependencies
+The code currently uses one dependency: [ripgrep (rg)](https://github.com/BurntSushi/ripgrep).
+Rg is used as a somewhat quick and simple hack to highlight the found search terms.
+Currently, it seems like this can't be done in a straightforward way in nushell natively (as far as I know).
+
+ripgrep is available for basically all platforms, see [ripgrep installation](https://github.com/BurntSushi/ripgrep#installation)
+
+## Installation
+The current code relies on the environment variable `$env.snippets-path` to point to the `.nuon` file that stores the snippets.
+For example:
+```nushell
+mkdir ~/snippets
+cp template.nuon ~/snippets/snippets.nuon
+echo '$env.snippets-path = "~/snippets/snippets.nuon"' o>> $nu.env-path
+```
+
+Then you can add snippets as a module in $nu.config-path (on *BSD and Linux usually `~/.config/nushell/config.nu`)
+```nushell
+use <PATH>/snippets *
+```
+
+## Todo
+- [ ] Better examples, with example output
blob - /dev/null
blob + d14728d081b4dc13b1dece542e1c1c2ed3524c09 (mode 644)
--- /dev/null
+++ snippets/mod.nu
+# Search and list saved commands, or other information.
+export def "snip show" [
+ ...regex:string # Search terms as regexes, may be more than one
+ --get(-g):number # Row to get
+ --nuon(-n) # With the table output (i.e. not -g), output the untransformed nuon output (i.e. no table | rg)
+]: [nothing -> table, nothing -> string] {
+ let rgregex = ($regex | str join "|")
+ let cmds = open $"($env.snippets-path)"
+
+ $regex | reduce -f $cmds { |it, acc| $acc | find -c [v] -ir $"($it)" }
+ | match ($get | is-empty) {
+ true => {
+ if $nuon {
+ $in
+ } else {
+ $in | table -e | rg --passthru $"($rgregex)" }
+ }
+ false => { $in | get $get | get v.body | to text },
+ }
+}
+
+# Add an entry to the snippets table
+export def "snip add" [
+ description: any # The description of the entry
+ body: any # The body of the entry, in general a command
+]: nothing -> nothing {
+ let entry = {
+ uuid: (random uuid | str upcase | str trim -r -c "\n"),
+ v: {
+ description: $description
+ body: $body
+ }
+ }
+ cp $"($env.snippets-path)" $"($env.snippets-path).bak"
+ open $"($env.snippets-path)" | append $entry | to nuon -i 2 | save -f $"($env.snippets-path)"
+}
+
+# Open the default editor ($env.EDITOR) to edit the snippets file ($env.snippets-path)
+export def "snip edit" []: nothing -> nothing {
+ ^$"($env.EDITOR)" $"($env.snippets-path)"
+}
blob - /dev/null
blob + 22a491a56faf4214959c85274e0396deb143c559 (mode 644)
--- /dev/null
+++ template.nuon
+[
+ [
+ uuid,
+ v
+ ];
+ [
+ "735BD89C-5DAE-4AC4-938E-9ADF706F1526",
+ {
+ description: "Add IP address to interface #linux #network",
+ body: "ip addr add <ip_CIDR> dev <ifname>"
+ }
+ ],
+ [
+ "B23C1637-DC7D-42A0-9344-8F15DB0C4559",
+ {
+ description: "Scan all ports with version detection using a list of hosts in a file",
+ body: "nmap -Pn -A -p- -oA nmap_full_tcp-host -iL <hosts-file>"
+ }
+ ],
+ [
+ "62756028-EED8-48FB-B71D-3B1629F4FD72",
+ {
+ description: "Show inet routing table on OpenBSD showing labels and without resolving addresses",
+ body: "route -nv show -inet"
+ }
+ ],
+ [
+ "101F2134-E15E-49A2-8CE5-37D438029BF2",
+ {
+ description: [
+ "description one",
+ "description two"
+ ],
+ body: [
+ "step one",
+ "step two"
+ ]
+ }
+ ]
+]
\ No newline at end of file