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
|
# snip - Minimal POSIX Snippet Manager
A simple, POSIX-compatible snippet manager that follows the Unix philosophy. Store, search, and quickly access code snippets with cross-platform clipboard support.
## Features
- **POSIX Compatible**: Works on any Unix-like system
- **Git Integration**: Snippets directory is git-tracked for version control
- **Cross-Platform**: Supports macOS and Linux clipboard operations
- **Unix Philosophy**: Pipes in/out, does one thing well
- **Minimal**: Single script, no dependencies beyond standard Unix tools
## Installation
1. Clone or download this repository
2. Run the setup script:
```bash
./setup
```
This will:
- Create `~/.snippets` directory
- Initialize git repository
- Set up proper `.gitignore`
- Add example snippets
## Usage
### Basic Commands
```bash
# List all snippets
./snip list
# Search snippets
./snip search "hello world"
# Interactive selection (copies to clipboard)
./snip select
# Add new snippet
./snip add bash hello 'echo "Hello, World!"'
# Initialize snippets directory
./snip init
```
### Directory Structure
Snippets are organized in `~/.snippets/`:
```
~/.snippets/
├── .git/
├── .gitignore
├── bash/
│ ├── hello-world.txt
│ └── loop-example.txt
├── python/
│ └── fibonacci.txt
└── git/
└── commit-message.txt
```
### Adding Snippets
```bash
# Add to existing category
./snip add bash "loop-example" 'for i in {1..10}; do echo $i; done'
# Create new category
./snip add python "fibonacci" 'def fib(n): return n if n <= 1 else fib(n-1) + fib(n-2)'
```
### Searching and Selecting
```bash
# Search all snippets with content preview
./snip search "fibonacci"
# Search only in specific category
./snip search "bash:hello"
./snip search "python:fibonacci"
# Interactive selection with search
./snip select "bash"
# Interactive selection of all snippets
./snip select
```
The search command now shows:
- Content matches with relevant line previews and context
- Category-specific search using `category:search-terms` syntax
- Direct select commands for each result (e.g., `./snip select html:boilerplate`)
### Direct Selection
You can directly select snippets using the `category:file` pattern:
```bash
# Direct selection without interactive menu
./snip select "html:boilerplate"
./snip select "bash:hello-world"
```
## Clipboard Support
- **macOS**: Uses `pbcopy`
- **Linux**: Uses `xclip` or `xsel` (whichever is available)
## Git Integration
The snippets directory is a git repository, allowing you to:
- Track changes to snippets
- Sync across machines
- Collaborate with others
- Maintain version history
## Design Principles
- **Single Responsibility**: One script, one purpose
- **Composability**: Pipes in/out with other Unix tools
- **Simplicity**: Minimal code, maximum functionality
- **Portability**: POSIX shell, no external dependencies
- **Immutability**: Snippets are files, not database entries
## Examples
```bash
# Quick workflow
./snip search "commit" | head -5
./snip select "git"
# Pipe snippets to other tools
./snip list | grep "python" | xargs cat
# Add snippet from file
./snip add bash "my-script" "$(cat my-script.sh)"
```
## Requirements
- POSIX-compatible shell (`/bin/sh`)
- `find`, `grep`, `sort` (standard Unix tools)
- `git` (for version control)
- Clipboard utility: `pbcopy` (macOS) or `xclip`/`xsel` (Linux)
|