Giter Club home page Giter Club logo

sniplib's Introduction

SnipLib ⚡️

Fast and simple organizer for your code snippets.

The main purpose of this script is having a fast ⚡️ and easy way to search through your code snippets, mixing the power of ripgrep and fzf.

I created this to manage code snippets that I saw myself using and searching for regularly. But now I also store short notes or cheatsheets I need to go back.

Why

The amount of tools to keep notes / snippets is huge. I created sniplib for the following reasons:

  • No lock-in, files are just text and you can keep them however you want.
  • Interactive finding + grepping. Since ripgrep is very fast, I can afford running it every time I type something so that I can have content grepping + fuzzy file matching.
  • No need to install any app.

Apart from that, I am constantly updating my snippets. Every time I find myself googling the same thing more than twice I put it in a snippet, so next time I can save some time and I know where I can find and answer that works.

Installation

Requirements:

Note: if you want to use a highligther that is not bat, edit the sniplib.sh. Uncomment the 17th line and place in in the preview command, between bat and cat.

This utility relies on pbcopy or xclip, so if you are not using macOS install xclip with sudo apt-get install -y xclip:

macOS & linux:

git clone https://github.com/polyrand/sniplib.git
chmod +x sniplib.sh
./sniplib.sh [SNIPPETS FOLDER] [SEARCH QUERY - optional]

Note: this has been developed and tested only on macOS, but it should be compatible with anything that can run xclip too. If not, please open an issue so that I can look at it.

Recommended usage:

Add an alias to your .bashrc (or whatever you use) to launch the script with your snippets folder as an argument (or you can use mine 😊):

#sniplib (with custom folder)
alias snip='~/Projects/sniplib/sniplib.sh ~/Projects/sniplib/snips'

(Note: ~/Projects/sniplib/sniplib.sh would be where you download the script and ~/Projects/sniplib/snips would be you snippets/notes folder).

Color configuration:

Since sniplib relies on fzf, you can (and should) set up the colors as you want to have a better experience:

BUG CAUTION

The sort_tags.py script does what it says, it sorts alphabetically all the tags. There was a bug in which if a file had no tags, all its content would get deleted. The bug is fixed now and tested, but it is a reminder to use a version control system.

Usage example

See it in action (first copying to clipboard then editing the snippet):

asciicast

If you want to sort alphabetically the tags in all the snippets, run:

chmod +x sort_tags.py
./sort_tags.py

For a better experience, set up FZF colors as you like. The ones I use are different from that terminal video:

export FZF_DEFAULT_COMMAND='fd --type f --follow --exclude .git --color=always'

export FZF_DEFAULT_OPTS='
  --color hl:#e84343,hl+:#23a4fa
'

Benchmarks

I replicated my personal snippets folder many times. Total size is 604Mb and there are 163680 files inside the directory tree (find load/ -type f | wc -l). Take into account this is an very deeply nested tree of folders.

# load/ is the test folder full of files
# 'pandas where' is a random query I chose.

time ./sniplib.sh load/ 'pandas where'
real	0m27.536s
user	0m7.713s
sys		0m23.021s

That's the time it takes to traverse the whole directory looking for those tags. The timing is not very precise because I have to ctrl + C for it to finish. On average (of a few runs) it was taking 21 seconds.

Using that same folder, the interactive search works perfectly, just slightly more laggy than with a not huge folder.

Notice

If you find a snippet without the appropriate source inside please let me know so that I can give credit to its author.

Release History

  • 1.0
    • Bugs fixed
  • 0.3
    • Enable custom folder
  • 0.2
    • Add tag sort script
  • 0.1
    • First release

Meta

Ricardo Ander-Egg Aguilar – @ricardoanderegg

Distributed under the MIT license. See LICENSE for more information.

https://github.com/polyrand/

Contributing

  1. Fork it (https://github.com/polyrand/sniplib/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

sniplib's People

Contributors

polyrand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sniplib's Issues

Add option to copy only part of the code

This would be useful to add examples inside the code snippet, for example:

"""tag1 array flatten tag2 numpy tag3""" 

import numpy as np

matrix = np.array([[1, 2, 3],
                               [4, 5, 6],
                               [7, 8, 9]])
#pasteon
matrix.flatten()  # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
#pasteoff

When ctrl-p (or any key is finally chosen) is pressed, only the code between #pasteon and #pasteoff will be copied to clipboard, that way you can have an example inside the snippet but without having it copied to the clipboard.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.