A simple and minimal command-line snippet manager
MIT License
Snip is a simple and minimal command-line snippet manager.
bash
, zsh
, fish
and powershell
).fzf
to provide fuzzy completion(currently supports zsh
shell).snip {snippet_name}
to view a snippet.fzf
shell integration inzsh
shell, you can find snippets by fuzzy completion. e.g., type snip **
and press tab.snip edit
to open your snippets repository in your favorite editor.snip edit {snippet_path}
to create|edit your snippet in your favorite editor.snip rm {snippet_path}
to remove a snippet. (use -r
flag to remove recursively)snip sync [optional commit message]
to pull and then push your snippets changes. This command runs the followinggit pull origin
git add -A
git commit -m "{your_provided_message | default_message}"
git push origin
[!NOTE] before running
git sync
for first time, you need to initialize git in your snippets directory and also set upstream of your default branch. something like the following commands:
cd $(snip dir)
git init
git remote add origin {your_repo_remote_path}
# Push your first commit to setup upstream branch
git add -A && git commit -m "Initial commit"
git push -u origin main
zsh
(recommended).snip
:))Install using go:
go install -ldflags "-X main.Version=main -X main.Date=`date +'%FT%TZ%z'`" github.com/mehran-prs/snip@main
Or get pre-compiled executables here
[!IMPORTANT] To set up completion, see the instructions below.
Add the following line to your shell configuration file.
# Set up snip completion
source <(snip completion bash)
# Set up snip completion (including fuzzy completion)
source <(snip completion zsh)
# Set up snip completion
snip completion fish | source
[!NOTE] fzf shell integration is a pre-requisite of snip fuzzy completion.
Set the following env variables to customize snip(e.g., put export SNIP_DIR=/path/to/dir
in your shell config file):
Name | Default | Description |
---|---|---|
SNIP_DIR | ~/snippets |
The snippets directory. It must be absolute path |
SNIP_FILE_VIEWER_CMD | cat |
The tool which renders non-markdown files in cmd |
SNIP_MARKDOWN_VIEWER_CMD | cat |
The tool which renders markdown files in cmd |
SNIP_EDITOR | Value of the EDITOR env variable, otherwise vim
|
The editor which snip uses to let you edit snippets |
SNIP_GIT | git |
The git command which it uses to sync snippets with your remote git repository |
SNIP_EXCLUDE | .git,.idea |
comma-separated list of directories that you want to exclude in auto-completion |
SNIP_VERBOSE | "" | Enable verbose mode (values: true ) |
SNIP_LOG_TMP_FILENAME | "" | Set path to a temporary log file. it's helpful in autocompletion debugging |
Usage:
snip [command] [flags]
snip [command]
Available Commands:
completion Generate completion script
dir prints the snippets directory
edit Create|Edit the snippet in the editor
help Help about any command
rm Remove a snippet or directory
sync sync the snippets changes with your remote git repository
version Print the version and build information
Flags:
-h, --help help for snip
export SNIP_FILE_VIEWER_CMD="bat --style plain --paging never"
export SNIP_MARKDOWN_VIEWER_CMD="glow"
[!IMPORTANT] On some operating systems (like ubuntu), the
bat
executable may be installed asbatcat
instead ofbat
, in such cases, setbatcat
instead ofbat
inSNIP_FILE_VIEWER_CMD
env variable.
[!Note] Currently fuzzy completion is supported just in zsh.
fzf
to enable fuzzy completion.fzf
shell integration
I like to have multiple instances of the snip
command under different names for multiple repositories. for example
snip
to manage my snippets, and tasks
to manage my tasks.
We can do that by creating a soft-link to the snip
command (other solutions like aliasing doesn't work
perfectly in auto-completion, at least for me :)) ),
for example to add the tasks
command, follow these steps:
tasks
to the snip
command:ln -s $(whereis snip | awk '{print $2}') /usr/local/bin/tasks
tasks
command(as its snippets directory) and also# Set the tasks directory (change to your own tasks directory)
export TASKS_DIR=/path/to/my/tasks
# Enable shell auto-completion (in this example, enabled for zsh)
source <(tasks completion zsh)
[!NOTE] You may wonder how the
tasks
command reads its directory path fromTASKS_DIR
env variable instead ofSNIP_DIR
, actually thesnip
tool reads env variables from{APPNAME}_{ENV_NAME}
(in this caseTASKS_*
) and if it was empty then reads fromSNIP_{ENV_NAME}
.
git clone https://github.com/<your_username>/snip && cd snip
)git checkout -b my-new-feature
)git add .
)git commit -m 'Add some feature'
)git push -u origin my-new-feature
)# Run tests
go test ./...
# Test coverage
go test -coverprofile cover.out ./...
go tool cover -html cover.out # view as html
go tool cover -func cover.out # output coverage per functions
# Run linters
go install github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint run ./...
# build
go build -o snip .