Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


Sending Files Locally -- 05.03.2020 %

For a long time I used pushbullet to quickly send pictures to my android phone from my mac.

Ex: Took cool terminal screen shot and want to post it to my Instagram story via my phone.

Problem: The pushbullet firefox extention on mac has a dialog bug prefenting file upload.

Solution:

  1. Connect phone and mac to same WIFI network

  2. Start local seb server, hosting the screen_shots folder

    $ npx http-server ./screen_shots
    
  3. Send the local ip url to phone via pushbullet

  4. Profit

accessing files on the phone

accessing files on the phone

Resource:

\- [ sync, android, files ]

Vim Spelling -- 05.03.2020 %

Exampled by the errors in my blog posts, spelling is not my strength. Never was. Always felt focusing on spelling limited my vocabulary. So I rely on tech. From the first red squiggly line in MS Word to now.

Web UI base tools:

The problem with these is I have to leave the terminal. Tech Motto: Never leave the terminal. So I use vim’s built-in. It is surprisingly good.

In action:

asciicast

demo using vim's builtin spell check

Config:

I write in both English and German so I have key maps for both

nnoremap <leader>se :setlocal spell spelllang=en<CR>
nnoremap <leader>sd :setlocal spell spelllang=de<CR>
nnoremap <leader>sn :setlocal nospell<CR>

Other spelling and writing tools: https://www.are.na/travis-shears/writing-bqnwud28d50

\- [ vim ]

Git Repo Backup -- 23.02.2020 %

Backup a git repo without thats not hosted on remote repo with:

$ git bundle create /tmp/pass_"$(date +%s)".bundle --all

Then to confirm bundle:

$ git bundle verify /tmp/pass_1582448923.bundle

When you need to restore the backup into a new repo folder:

$ git clone -b master /tmp/pass_1582448923.bundle newrepo

I recently used this to backup my ~/.pass-store pass repo. Basically it’s a folder full of .gpg files each encrypting a password. Don’t want to store it on a remote host so I back it up locally. Create a git bundle then I encrypt the resulting bundle file and store it somewhere safe.

$ gpg --symmetric ./pass_1582448923.bundle

source:

git-memo docs

\- [ git, pass ]

Vim FZF Plugin -- 30.01.2020 %

I’ve used several fuzzy finder utilities in vim over the years like Command T or CtrlP. They both have there pluses and minuses but never found them to be that fast especially with large code bases I’m often working in. Fzf for me is superior so I was excited to see a plugin that integrates Fzf so well into vim. Its not just useful for finding files but works great with buffers, files with git changes, commands, marks, and even lines in open buffers.

My vim config for Fzf is as follows:

nnoremap <Leader>pb :Buffers<CR>
nnoremap <Leader>pf :GFiles<CR>
nnoremap <Leader>pg :GFiles?<CR>
nnoremap <Leader>pm :Marks<CR>
nnoremap <Leader>pc :History:<CR>
nnoremap <Leader>pl :Lines<CR>

This allows me to easily zip around my code base with speed.

asciicast

demo using fzf in vim

sources:

\- [ vim, search ]

JQ JSON processor -- 30.01.2020 %

One of my oldest snippets is how to Pretty print JSON in the shell. This method works great for simple things where you just need to get an idea of the JSON structure, it has the bonus of using python which you probably already have installed. The problem is when you want to do more complex tasks it is quite limited in terms of parsing. Thats where jq comes in

$ curl https://review-noticket-xxxxxxxx.eu/xxxxx/static/loadable-stats.json  | jq '.entrypoints .sharedHeader .assets' | rg --invert-match map`

Simply piping to jq pretty prints the JSON but by passing a query string, ex ".entrypoints .sharedHeader .assets”, you dig into the JSON and easily get what you need. This is easily combinable with other shell utilities like in the example above which gets a list of asset URLs than uses ripgrep invert-match to clean out the source map URLs from the list. This is now my perfered way of working with JSON in the shell.

source:

\- [ JSON, curl, jq ]

Emoji Commit Messages -- 28.01.2020 %

Something code reviews can be a bit monotonous but I always found emojis spice things up a bit. Unfortunately my xterm based terminal does not support normal UTF-8 emojis. When pasted directly from apple emoji keyboard I get nothing. Today I sought out a solution to this and found you can simply use :wrench: like shortcodes, similar to how emojis are handled in slack. Seems to work for both gitlab and github!

Ex:

commit de3eb943b880c6f34e8398d22e6c7389e72cd30c
Author: Travis Shears <travis.shears@westwing.de>
Date:   Tue Jan 28 11:58:35 2020 +0100

    NOTICKET :sparkles: shared feature toggels POC
gitlab screencap

gitlab screencap

During this process I stumbled upon a open source project putting meaning behind the emojis in the development context.

| code                        | use case                                      |
| ----------------------------------------------------------------------------|
| :art:                       | Improving structure / format of the code.     |
| :zap:                       | Improving performance.                        |
| :fire:                      | Removing code or files.                       |
| :bug:                       | Fixing a bug.                                 |
| :ambulance:                 | Critical hotfix.                              |
| :sparkles:                  | Introducing new features.                     |
| :pencil:                    | Writing docs.                                 |
| :rocket:                    | Deploying stuff.                              |
| :lipstick:                  | Updating the UI and style files.              |
| :tada:                      | Initial commit.                               |
| :white-check-mark:          | Updating tests.                               |
| :lock:                      | Fixing security issues.                       |
| :apple:                     | Fixing something on macOS.                    |
| :penguin:                   | Fixing something on Linux.                    |
| :checkered-flag:            | Fixing something on Windows.                  |
| :robot:                     | Fixing something on Android.                  |
| :green-apple:               | Fixing something on iOS.                      |
| :bookmark:                  | Releasing / Version tags.                     |
| :rotating-light:            | Removing linter warnings.                     |
| :construction:              | Work in progress.                             |
| :green-heart:               | Fixing CI Build.                              |
| :arrow-down:                | Downgrading dependencies.                     |
| :arrow-up:                  | Upgrading dependencies.                       |
| :pushpin:                   | Pinning dependencies to specific versions.    |
| :construction-worker:       | Adding CI build system.                       |
| :chart-with-upwards-trend:  | Adding analytics or tracking code.            |
| :recycle:                   | Refactoring code.                             |
| :whale:                     | Work about Docker.                            |
| :heavy-plus-sign:           | Adding a dependency.                          |
| :heavy-minus-sign:          | Removing a dependency.                        |
| :wrench:                    | Changing configuration files.                 |
| :globe-with-meridians:      | Internationalization and localization.        |
| :pencil:                    | Fixing typos.                                 |
| :poop:                      | Writing bad code that needs to be improved.   |
| :rewind:                    | Reverting changes.                            |
| :twisted-rightwards-arrows: | Merging branches.                             |
| :package:                   | Updating compiled files or packages.          |
| :alien:                     | Updating code due to external API changes.    |
| :truck:                     | Moving or renaming files.                     |
| :page-facing-up:            | Adding or updating license.                   |
| :boom:                      | Introducing breaking changes.                 |
| :bento:                     | Adding or updating assets.                    |
| :ok-hand:                   | Updating code due to code review changes.     |
| :wheelchair:                | Improving accessibility.                      |
| :bulb:                      | Documenting source code.                      |
| :beers:                     | Writing code drunkenly.                       |
| :speech-balloon:            | Updating text and literals.                   |
| :card-file-box:             | Performing database related changes.          |
| :loud-sound:                | Adding logs.                                  |
| :mute:                      | Removing logs.                                |
| :busts-in-silhouette:       | Adding contributor(s).                        |
| :children-crossing:         | Improving user experience / usability.        |
| :building-construction:     | Making architectural changes.                 |
| :iphone:                    | Working on responsive design.                 |
| :clown-face:                | Mocking things.                               |
| :egg:                       | Adding an easter egg.                         |
| :see-no-evil:               | Adding or updating a .gitignore file          |
| :camera-flash:              | Adding or updating snapshots                  |
| :alembic:                   | Experimenting new things                      |
| :mag:                       | Improving SEO                                 |
| :wheel-of-dharma:           | Work about Kubernetes                         |
| :label:                     | Adding or updating types (Flow, TypeScript)   |
| :seedling:                  | Adding or updating seed files                 |
| :triangular-flag-on-post:   | Adding, updating, or removing feature flags   |
| :goal-net:                  | Catching errors                               |
| :animation:                 | Adding or updating animations and transitions |
| :wastebasket:               | Deprecating code that needs to be cleaned up. |

source:

\- [ git, emoji ]

Watch Command -- 27.01.2020 %

Currently at work we occasionally have to redeploy pods that have the same image tag but different SHA hashes. The problem stems from the k8s deployments not picking up the changes and thus a manually killing of the production pods is required. When they are restarted the SHA is checked and new image pulled, during this process I like to watch along at the running pods to make sure things are going smoothly.

I use to use the built in watch argument of k8s get pods command -w:

$ kubectl get pods -n shop -w

But this produces a really cluttered terminal output so I started just running the command on loop

$ while true; do; kubectl get pods -n shop ; sleep 5; done

Then a coworker showed me the watch command which has a much cleaner interface and outputs to a much clearer less/more like non scrolling output.

$ watch -n 5 'kubectl get pods -n shop'

The watch command is availble via brew

$ brew install watch

source:

https://gitlab.com/procps-ng/procps/blob/master/watch.c

\- [ watch ]

Move File Range -- 27.01.2020 %

Recently had to move a range of files and some zsh expansions came in handy.

$ for x in {1..6}; do mv AAAA.S01E0"$x".mkv ./a-$x.mkv; done

source:

https://serverfault.com/questions/370403/copy-a-range-of-files-in-command-line-zsh-bash

\- [ files, built-ins ]

ZSH git plugin -- 27.01.2020 %

I use git exclusive from the command line and while it’s interface is very clear since I spend so much time I’m willing to trade some of that clarity for speed. ZSH has a great plugin for really fast git tasks. Two of my favorite are pushing while setting a upstream branch and adding patches.

GPSUP, git set upstream and push shortcut

I push new git branches a few times a day and my previous work flow was to:

$ gp
fatal: The current branch NOTICKET-XXX has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin NOTICKET-XXX

followed by the classic fuck command:

$ fuck

which then runs the proper push command setting upstream. News flash! This call all be accomplished with a single plugin command:

$ gpsup

GAPA, git add patch

I like small commits and the key to that for me is adding by patch. This short cut turns

$ git add --patch

to

$ gapa

Can’t recommend the zsh git plugin enough! There are too many shortcut commands to be worth memorizing but I think gpsup and gapa are worth it. For a big list of commands it checkout the docs here

\- [ git, zsh ]

For loops in bash / zsh shells -- 12.01.2020 %

Looping directly in the shell is something I do at lease once a week and it’s a great time saver. Here I want to run through a concrete example of how to use for loops to mass move and rename files.

During process of getting this snippet section of my site off the ground I realize I had put a bunch of markdown files into named directories when I could have just named the files better and done without the directories. What I needed to do was mv the files out of these dirs and rename them to the name of the dir plus add the “.en.md” extension.

Here is the old directory structure:

$ tree
.
└── snippets
    ├── _index.en.md
    ├── aws-cloud-front-inval
    │   └── index.en.md
    ├── aws-s3-sync
    │   └── index.en.md
    ├── ffmpeg-screen-casts
    │   └── index.en.md
    ├── find-folder
    │   └── index.en.md
    ├── git-better-git-add
    │   └── index.en.md
    ├── git-log-grep
    │   └── index.en.md
    ├── git-move-branch
    │   └── index.en.md
    .
    .

To start with I preformed the operation in question a single time to prove it works:

$ mv ./who-is-using-that-port/index.en.md ./who-is-using-that-port.en.md && rm -r ./who-is-using-that-port

Then I copy this command to the clipboard for later and use command history pull up a previous for loop command and switch the shell command entry external editing in vim mode, for me <C-x><C-e>. As we are essentially writing a quick bash script in-line we need vim powers! While in vim if you need the file list a quick :r !ls does the trick. These little efficiencies like using command history are not just faster but make it so you don’t have to remember the bash syntax.

Resulting vim buffer with the for loop:

for x in \
aws-s3-sync \
ffmpeg-screen-casts \
find-folder \
git-better-git-add \
git-log-grep \
...
git-move-branch \
vim-window-resize ; do mv ./"$x"/index.en.md ./"$x".en.md && rm -r "$x" ; done

Resulting new file structure:

$ tree
.
└── 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
    .
    .

Perfect, all files have been renamed properly and the empty directories deleted. This technique has lots of other applications besides moving and renaming files. Earlier this week while debugging api at work I dumped bunch of json responses into a file so I could search for translation key.

$ for x in \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx ; do curl $x >> /tmp/translations.json; done
\- [ zsh, built-ins ]