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.


emacs mac umlauts -- 05.11.2021 %

Recently I’ve been writing a lot for the German side of my personal site. When typing in German I perfer to use the English QWERTY keyboard and just alt-u-u to type “ΓΌ”. The problem I was having was emacs would intercept this and execute the capitalize-word function 😞. After some digging into my configs, ~/.doom.d/config.el, I was able to unset M-u only problem is it still didn’t activate the mac system umlaut feature.

(global-unset-key (kbd "M-u"))

Finally after some more digging I found:

(setq ns-alternate-modifier 'none
      ns-right-alternate-modifier 'meta)

It works by un-assigning the left alt to meta, allowing the system keyboard feature to kick in.

source: https://emacs.stackexchange.com/questions/61019/umlauts-in-emacs-on-mac

\- [ emacs ]

update a local zef module -- 20.10.2021 %

Lately I’ve been working locally with raku/zef modules for my CLI apps. I install them with:

$ zef install .

Turns out you can update a package very similarity. Just bump the version in the META6.json and run the same command again.

$ zef install .

docs

\- [ raku, zef ]

make emacs words closer to vim -- 20.10.2021 %

I love doomacs but jumping around words it bugged me underscores were not considered part of the word like in vim. After six months of dealing with it I stumbled upon a solution!

Simply put this in my .doom.d/config.el.

(modify-syntax-entry ?_ "w")

source: emacs.stackexchange

\- [ emacs ]

describing a raku variable -- 15.10.2021 %

My go to way to figure out what I’m working with in Raku.

my $res = cool_thing();

say $res.WHAT;
say $res.^attributes;
say $res;
\- [ raku ]

re-export javascript function -- 05.10.2021 %

When splitting logic into files in JavaScript it sometimes happens you have a folder of files; say for a single domain like Google analytics tracking. There comes a time in another part of the code where you need to import some functions and types from this GATracking folder but you may not want to dig into which file in the folder that function is. Re-exporting allows us to keep the API of the GATracking logic consise and allow the rest of the program to not worry about how exactly the files are structured. Example:

src/client/hooks/GATracking/index.tsx

export { default as TrackingAction } from './actions';
export const track = (action: TrackingAction) =>
...

Then somewhere else: src/client/components/GoButton/index.tsx

import {TrackingAction, track} from '../hooks/GATracking';
...

source: https://stackoverflow.com/questions/34576276/re-export-a-default-export

\- [ js ]

images to animation -- 23.09.2021 %

Today I had a set of jpg images I wanted to turn into a .webm animation for my poverty line blog post. After some searching I found a nice solution on hamelot blog.

With image arranged in a folder naming like map_0001, map_0002 and so on. I ran the following command

$ ffmpeg -r 1 -f image2  -i map_%04d.png -vcodec libx264 -crf 25  -pix_fmt yuv420p map.mp4

Then I converted that map.mp4 into a smaller .webm with my helper script.

https://paste.sr.ht/~travisshears/1742232acc9c8458370e7b81c140a46a12f61ba4

source: hamelot post

\- [ ffmpeg ]

creating a temporary tabel in sqlite -- 21.09.2021 %

Today I needed to export some data from an sqlite tabel to csv, as part of my poverty line project. Using sqlitebrowser I could see the table had to much data and I only wanted to export a few columns. To accomplish this I learned how to create a temporary table and exported that.

DROP TABLE IF EXISTS processing_data_export;
CREATE TEMPORARY TABLE processing_data_export AS
SELECT x,y, county_id FROM grid;

source: stackoverflow

\- [ sqlite, sql ]

wipe dynamodb table via terraform -- 17.09.2021 %

Recently I came across the problem of how to wipe a dynamodb table. I though there would be an ui button for this or at lease an api/sdk method but from what I found the only way it to individually delete the items by key. This method can be batched up to 10 items per call but is still very slow and incurs write costs.

Since I’m used Terraform to create the table in the first place I thought I’ll just destroy it and remake it… Then came the problem of not wanting to destroy everything but simply remake the table. After some digging in Terraform docs I found a pretty clean solution.

main.tf

module "dynamodb_table" {
  source   = "terraform-aws-modules/dynamodb-table/aws"

  name     = "poverty-line-county-gps-map"
  hash_key = "id"
  stream_enabled = false

  attributes = [
    {
      name = "id"
      type = "S"
    }
  ]

  tags = {
    Terraform   = "true"
    Project = "poverty_line"
  }
}

In this case I wanted to remake “poverty-line-county-gps-map”. To find out what exactly it is called I ran terraform state:

$ terraform state list
aws_sqs_queue.dead_letter_queue
aws_sqs_queue.terraform_queue
module.dynamodb_table.aws_dynamodb_table.this[0]

from here the resource can be remake like:

$ terraform apply -replace="module.dynamodb_table.aws_dynamodb_table.this[0]"

Happy times remaking things. πŸ‘‹

pushing to remote branch with different name -- 08.09.2021 %

Some times you rename a branch locally and want to push it to a remote branch with a different name. This is how:

$ git push -u origin localBranch:remoteBranch

source: stack-overflow

\- [ git ]

measuring node.js function performance -- 23.08.2021 %

How fast is that new function?

 import { xx } from 'xx';
+import { performance } from 'perf_hooks';
@@ -160,7 +161,10 @@ const userSessionMiddleware = async (req: XRequest, res: ExpressResponse, ne
+        var t0 = performance.now();
         req.isBot = headersIndicateBot(req.headers);
+        var t1 = performance.now();
+        console.log('Took', (t1 - t0).toFixed(4), 'milliseconds to calculate is bot');

source: https://www.sitepoint.com/measuring-javascript-functions-performance/

\- [ js, node-js ]