When I first started a personal site years ago I maintained a blog post full of little snippets / tricks I picked up along my journey of learning to program. Today I had an idea for one to add to that list but that post had since died, as it was not rolled over in between several iterations of my site. Here are my thoughts on why write about snippets and a technical demo of how I brought the snippets back to life, added a bunch more, and the new snippets section of my site.
Back from the grave, thank god for version control
I keep my site in a long running git repository so even in the cases where a completely wipe and
start over I have a history of my old posts. So to bring snippets back just had to find the old
markdown file, I knew had had to do with the word “shell”, by a git log search
$ git log --grep="shell" -1 -p. Here is the output:
commit af73bc55cce3514be1c906fb8669e30eabae8f7f Author: Travis Shears <firstname.lastname@example.org> Date: Tue Dec 4 14:01:08 2018 +0100 Add shell snippet that pretty prints json diff --git a/content/blog/shell.md b/content/blog/shell.md index d21b783..334c0b3 100644 --- a/content/blog/shell.md +++ b/content/blog/shell.md @@ -7,6 +7,18 @@ tags: ["cli", "shell", "linux", "mac"] Collecting some of my favorite shell commands, both to share and have a recond for myself. ...
From here we have the commit hash and file path but we need to be careful this commit may not be the newest one to edit the file. So to find that we can log by file:
$ git log -- content/blog/shell.md
commit 34519c09fd5d9dd71e3e07c4587f9c4c45998b56 Author: Travis Shears <email@example.com> Date: Wed Apr 3 09:53:21 2019 +0200 delete old files and dirs commit 0cce04da5d867d3f9ca4a9265bfc80d9f6207d36 Author: Travis Shears <firstname.lastname@example.org> Date: Fri Dec 28 09:45:19 2018 +0100 Add lsof to scripts commit af73bc55cce3514be1c906fb8669e30eabae8f7f Author: Travis Shears <email@example.com> Date: Tue Dec 4 14:01:08 2018 +0100 Add shell snippet that pretty prints json
Now with the we have the hash of the last edit before deletion “0cce04” and we can retrieve the file at this commit and pipe to the clipboard with:
$ git show 0cce04da5d867d3f9ca4a9265bfc80d9f6207d36:content/blog/shell.md | pbcopy
Nice, so we have the contents of that old blog post, time to come up with a way to represent these snippets in the new site.
This time around instead of cramming all these snippets into a single massive blog post I wanted to leverage hugo, the static site generator this site is built with, and do something more manageable. I split the snippets from the old post into separate markdown files under a new folder snippets and I added a new custom taxonomy “snippet_type” to the frontmatter of each file ex:
--- title: "Move Branch" date: 2020-01-11T05:06:49+01:00 draft: false snippet_types: ["git"] --- ...content...
New file structure:
. ├── blog │ ├── _index.de.md | . ├── notes │ ├── _index.de.md | . └── snippets ├── _index.en.md ├── aws-cloud-front-inval.en.md ├── aws-s3-sync.en.md ├── ffmpeg-screen-casts.en.md ├── find-folder.en.md ├── git-better-git-add.en.md ├── git-log-grep.en.md ├── git-move-branch.en.md ├── git-nocommit-merge.en.md ├── git-prev-commit-changes.en.md ├── git-rebase.en.md ├── git-revert-branch.en.md ├── git-revert.en.md ├── git-who-last.en.md ├── gitlab-runners-config.en.md ├── pass-bulk-import.en.md ├── pass-copy.en.md ├── pass-search.en.md . . .
After adjusting the theme template files this gave me a result I’m quite happy with.
/snippets – main snippets listing page, which still shows the full content of each snippet similar to the blog post but paginates at 10 per page.
/snippet_types/vim – snippet type listing pages, which lists snippets of a certain type making it much faster to navigate to older snippets
/snippets/vim-placeholders – single snippet page, which shows only the content a single snippet, great for sharing on social media
Why document snippets
For me a big part of my life is programming, either at work or in a café working on a personal projects. Sharing little snippets and tricks I find along the way with colleges and friends brings me great joy. I also love the short reward cycle of authoring and posting a snippet as they are much easier to write then blog posts for me. I know I often come back to the snippet list when I forget one so I hope others may find it helpful as well.