{"id":15750369,"url":"https://github.com/whoan/snip","last_synced_at":"2025-04-30T11:20:55.728Z","repository":{"id":56576154,"uuid":"216462369","full_name":"whoan/snip","owner":"whoan","description":":truck: Use code snippets from the web (or your local file-system) in your code","archived":false,"fork":false,"pushed_at":"2021-01-17T15:35:03.000Z","size":52,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T15:46:53.274Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whoan.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-21T02:36:05.000Z","updated_at":"2022-04-29T13:56:11.000Z","dependencies_parsed_at":"2022-08-15T21:20:14.206Z","dependency_job_id":null,"html_url":"https://github.com/whoan/snip","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fsnip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fsnip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fsnip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fsnip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whoan","download_url":"https://codeload.github.com/whoan/snip/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251688997,"owners_count":21627814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2024-10-04T06:40:28.514Z","updated_at":"2025-04-30T11:20:55.675Z","avatar_url":"https://github.com/whoan.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Snip\n\nAdd code snippets to your code directly from the web (or your local filesystem).\n\nUseful to maintain a repo of snippets as I do in https://github.com/whoan/snippets/.\n\n## Installation\n\n### Dependencies\n\n- `curl` to download snippets\n\n### Bash\n\n```bash\n# download script and place it whenever you want\ncurl --fail \"https://raw.githubusercontent.com/whoan/snip/master/snip.sh\" \u003e snip.sh\n```\n\n```bash\n# now place this handy function in your shell rc file\nsnip() {\n  # source snip on demand to avoid slowing down your session startup. you're welcome :)\n  if ! declare -f __snip \u003e /dev/null; then\n    local path_to_snip=\"/full/path/to/snip.sh\"  # HEY! CHANGE THIS PLEASE!!!\n    if ! [ -f \"$path_to_snip\" ]; then\n      echo \"$path_to_snip was not found\" \u003e\u00262\n      return 1\n    fi\n    source \"$path_to_snip\"\n  fi\n  __snip \"$@\"\n}\n# start a new shell session to take changes\n```\n\n## Usage\n\n- Add `snip(\"\u003curl|local_path\u003e\")` (a.k.a. *the snip line*) anywhere in your code (usually as a comment) and the retrieved content will be placed after that line.\n- Prefix any command with `snip` (eg: `snip bash script.sh`) and the *snip lines* (if any) will be replaced with the content retrieved from the url provided.\n\n\u003e Adding your *snip line* as a comment avoids your linter to complain about syntax (it works the same).\n\n### Optional Parameters\n\n- You can provide the `-f/--force` flag to force downloading the content regardless of it being present in the cache (*~/.cache/snip*). The cache will be updated with new content.\n\n### Settings\n\nYou can set the following in `~/.config/snip/settings.ini`:\n\n- `base_url`: Specify a url to shorten the *snip line* in your code.\n\n\n    Example:\n\n    ```bash\n    $ cat ~/.config/snip/settings.ini\n\n    ```\n    ```\n    base_url=https://raw.githubusercontent.com/whoan/snippets/master/cpp/\n    ```\n\n    Now, you can write this snip line in your code:\n\n    ```cpp\n    //snip(\"print.hpp\")\n    ```\n\n    Instead of this:\n\n    ```cpp\n    //snip(\"https://raw.githubusercontent.com/whoan/snippets/master/cpp/print.hpp\")\n    ```\n\n## Features\n\n- It supports any type of text file\n- If a snippet has a snippet, those are replaced recursively too.\n- Once a snippet is downloaded, it is cached to fasten next uses of `snip`, unless yo provide `-f` to force redownloading.\n- For C/C++ files, after [PR #16](https://github.com/whoan/snip/pull/16), you will see proper line numbers and filenames on compilation errors. You will even know if the error is inside a snippet (see [Issue #15](https://github.com/whoan/snip/issues/15) as an example).\n\n## Examples\n\nIt works with **any** type of file. Feel free to add examples through PRs.\n\n### C++\n\nLet's compile *main.cpp* prefixed with `snip`:\n\n```bash\n$ cat examples/main.cpp\n```\n```cpp\n//snip(\"https://raw.githubusercontent.com/whoan/snip/master/examples/snippet.hpp\")\nint main() {\n  say_hello();\n  return 0;\n}\n```\n\n```bash\n$ snip g++ examples/main.cpp \u0026\u0026 ./a.out\n\u003e Hello World\n```\n\nIf you set `base_url` in your settings, you can also shorten the reference to the snippet like this:\n\n```cpp\n//snip(\"snippet.hpp\")  // snip will download $base_url/snippet.hpp\n```\n\nAnd if you want to reference a snippet in your file system, just provide the path to the file:\n\n```cpp\n//snip(\"/home/you/snippet.hpp\") // full path\n//snip(\"./snippet.hpp\")         // relative path\n//snip(\"~/snippet.hpp\")         // you can even reference your home path with ~\n```\n\n### Bash\n\n```bash\n$ cat examples/main.sh\n```\n```bash\n#snip(\"https://raw.githubusercontent.com/whoan/snip/master/examples/snippet.sh\")\nsay_hello\n```\n\n```bash\n$ snip bash examples/main.sh\n\u003e Hello World\n```\n\n### Python\n\n```bash\n$ cat examples/main.py\n```\n```python\n#snip(\"https://raw.githubusercontent.com/whoan/snip/master/examples/snippet.py\")\nsay_hello()\n```\n\n```bash\n$ snip python examples/main.py\n\u003e Hello World\n```\n\n### Docker\n\n```bash\n$ cat examples/Dockerfile\n```\n```\nFROM alpine\n#snip(\"https://raw.githubusercontent.com/whoan/snip/master/examples/snippet.dockerfile\")\nCMD sh say_hello.sh\n```\n\n```bash\n$ snip docker build -q -t snip-docker -f examples/Dockerfile . \u0026\u0026 docker run snip-docker\n\u003e Hello World\n```\n\n## TODO\n\n- ~Add cache to avoid downloading same code over again~ (Thanks [@sapgan](https://github.com/sapgan) and [@danstewart](https://github.com/danstewart))\n- ~Allow setting base_url in a file to shorten snip line~\n\n## Final notes\n\nI created this script to reuse code with ease. It is not production ready... unless you know what you are doing.\n\n## License\n\n[MIT](https://github.com/whoan/snip/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoan%2Fsnip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhoan%2Fsnip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoan%2Fsnip/lists"}