Snippets

Collection of my code snippets

Tags:


jj log all

29-06-25

%

Love jj, but the default log is often not that useful because it collapses too much of the history.

ex:

$ jj log
@  xlostqoq t@travisshears.com 2025-06-29 11:48:29 6db402f6
│  (empty) (no description set)
◆  yxwtwyxv t@travisshears.com 2025-06-29 11:48:16 main* f058208c
│  move repo to homelab git
~

With this command you'll get the full history:

$ jj log -r ::@
@  xlostqoq t@travisshears.com 2025-06-29 11:48:29 6db402f6
│  (empty) (no description set)
◆  yxwtwyxv t@travisshears.com 2025-06-29 11:48:16 main* f058208c
│  move repo to homelab git
◆  sqlmknvu t@travisshears.com 2025-06-29 11:45:05 main@homelab 78cdf6c9
│  try and make a routine to draw line
◆  ksvnwswu t@travisshears.com 2025-06-28 15:06:08 632b88d2
│  start draw-canvas-bounds
...
...
◆  wmxzxqqq t@travisshears.com 2024-07-12 16:19:18 06daa57b
│  init repo with 99 bottles of beer and help tals
◆  zzzzzzzz root() 00000000

source

- [ jj]

custom zsh theme

26-06-25

%

To create a custom zsh theme simply create a .zsh-theme file under:

~/.oh-my-zsh/custom
└── themes
    └── cool_custom_theme.zsh-theme

Here is a theme I created to show jj version control description.

source

- [ zsh]

npm upgrade with nvm

24-06-25

%

I manage Node.js versions with nvm for all my JavaScript/TypeScript projets. Lots of .nvmrc files. NPM comes always comes with and I've never had to update it before now.

Here is how I got NVM to use the latest minor version of Node.js 22 and NPM v11:

$ nvm use
Found '' with version <v22>
Now using node v22.3.0 (npm v10.8.1)
$ nvm install 22
Downloading and installing node v22.16.0...
Downloading https://nodejs.org/dist/v22.16.0/node-v22.16.0-darwin-arm64.tar.xz...
#################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.16.0 (npm v10.9.2)
$ nvm use
Found '' with version <v22.16.0>
Now using node v22.16.0 (npm v10.9.2)
$ nvm install-latest-npm
Attempting to upgrade to the latest working version of npm...
* Installing latest `npm`; if this does not work on your node version, please report a bug!

removed 14 packages, and changed 67 packages in 6s

25 packages are looking for funding
  run `npm fund` for details
* npm upgraded to: v11.4.2

- [ nvm]

accessing url query params in astro.js

16-06-25

%

The documentation was not super clear on this one. I got side tracked by Astro.params for a long time.

const page = Number(Astro.url.searchParams.get("page") ?? "1");

- [ astro]

clojure syntax quote with unquote

16-06-25

%

The quote macro / fn can be a bit trickey. Say you want to you have a var and you want to put it into a new list. Simply quoting will quote the var name and not the value:

user=> (def val "test")
#'user/val
user=> '(val)
(val)
user=> (list val)
("test")
user=> `(~val)
("test")

- [ lispclojure]

quick way to push last jj commit to git

10-06-25

%

This handly snippet move the jj bookmark (branch) to the last commit and pushes that change to remote.

$ jj desc
$ jj new
$ jj bookmark m main --to @- && jj git push

- [ gitjj]

netcat over ping

08-06-25

%

When ping fails netcat comes to the rescue.

Instead of:

$ ping aemos:5008
ping: cannot resolve aemos:5008: Unknown host

Run:

$ nc -zv aemos 5008
Connection to aemos port 5008 [tcp/synapsis-edge] succeeded!

- [ netcatval]

choose over awk

04-03-24

%

Sometimes typing out awk '{print $1}' just takes too long. Today I decovered choose. It makes selecting a field from an output much faster.

Now instead of:

$ gss | rg awk | awk '{ print $2 }' | xargs hx

I run:

$ gss | rg awk | choose 1 | xargs hx

- [ awkchoose]

how to trust gpg keys

27-10-23

%

After moving some GPG keys to a new computer I kept getting these trust warnings.

It is NOT certain that the key belongs to the person named
in the user ID.If you * really * know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N)

To solve this one has to edit the key and set a trust level.

$ gpg--edit - key 't@coolsite.com'
trust
5

Source

- [ gpg]

random password

18-10-23

%

This will generate a six char password.

$ openssl rand -base64 6

s3u3fi

- [ openssl]

how to recover deleted file with git

12-09-23

%

I'd like to build a new nest.js command based off a file I deleted last week. Here is how I go about getting that old file back via git restore

Step 1, find the file:

$ git log --name-only
/command
fd8bc2a6 Merge branch 'XXX-1111-remove-search-queue' into 'main'
fd07ddc3 XXX-1111: Remove search queue
apps/cli/src/cli.module.ts
apps/cli/src/db/backfill-search-queue.command.ts
apps/cli/src/db/db.module.ts
...

Step 2, restore the file:

$ git restore --source fd07ddc3~1 apps/cli/src/db/backfill-search-queue.command.ts

Note the ~1. The file in question was deleted in commit fd07ddc3, so to restore it we need to go one commit before it was deleted. ~1 does exactly that.

- [ git]

remove common lines with comm

24-05-23

%

With help of ChatGPT I discovered this unix gem today.

Given two files

main_deck.txt:

1 Abzan Charm
1 Arcane Bombardment
1 Arcane Sanctum
1 Arcane Signet
1 Archmage Emeritus
1 Bant Charm
1 Boros Charm
1 Bring to Light
1 Brokers Charm
1 Brokers Confluence

considering.txt:

1 Abzan Charm
1 Arcane Sanctum
1 Taigam, Ojutai Master
1 Archmage Emeritus
1 Boros Charm
1 Tamanoa
1 Time Wipe
1 Trap Essence
1 Brokers Confluence

running

$ comm -23 <(cat considering.txt | sort) <(cat main_deck.txt | sort)
1 Taigam, Ojutai Master
1 Tamanoa
1 Time Wipe
1 Trap Essence

Returns a list of cards that are unique to the considering.txt.

If your text files are sorted you can skip the <(cat ..) magic.

And yes. I'm using the cli two help build Magic the Gathering Decks!

- [ comm]

percol to pick lines

16-05-23

%

Deep in a emac tutorial I discovered this hidden gem.

"percol adds flavor of interactive selection to the traditional pipe concept on UNIX."

project github

You can simply place it in the middle of a pipe and pick the options you want.

Example:

$ BUCKET="s3://cool-bucket" aws s3 ls $BUCKET | awk '{print $4}' | percol |sed "s;^;${BUCKET}/;" | xargs aws s3 rm
delete: ...

{{< asciicast-with-caption id="585386" title="demo" >}}

- [ percol]

sort numerically

11-05-23

%

Working with with sitemap xml files in AWS S3 today and the default sort is a bit hard to read.

Example:

$ aws s3 ls s3://cool-bucket | awk '{print $4}'
sitemap.brands.xml
sitemap.episode.0.xml
sitemap.episode.1.xml
sitemap.episode.10.xml
sitemap.episode.11.xml
sitemap.episode.2.xml
sitemap.episode.20.xml
sitemap.episode.21.xml
sitemap.episode.22.xml
sitemap.episode.23.xml
...

Using sort -V it sorts the lines numerically!

Working example:

$ aws s3 ls s3://cool-bucket | awk '{print $4}' | sort -V
sitemap.brands.xml
sitemap.xml
sitemap.episode.0.xml
sitemap.episode.1.xml
sitemap.episode.2.xml
sitemap.episode.3.xml
sitemap.episode.4.xml
sitemap.episode.5.xml
sitemap.episode.6.xml
sitemap.episode.7.xml
...

- [ sort]

auto nvm use

13-03-23

%

Simply adding:

[ -f ./.nvmrc ] && nvm use

to your .bashrc or .zshrc

Will automatically run the nvm use command should you start the shell in a dir with a .nvmrc file.

- [ nvm]

git trim

23-02-23

%

Easy clean up branches from local that are old or already merged on remote.

$ git trim -s -p

- [ git]

remove gps metadata from image

31-01-23

%

Recently I noticed some of the images on my site had GPS metadata info. Thats not very private! So I did some googling and found this solution.

$ exiftool -GPS*= $(fd -e jpg -e png -e jpeg)

source

- [ exiftool]

update pleroma server

04-12-22

%

I always forget how to upgrade my Pleroma servicer when a new version comes out so I'm writing it here. If you are unsure of what you are doing please consult the official docs.

My Pleroma instance: social.travisshears.xyz

Start by sshing into the server and switching to the pleroma user.

$ sudo su pleroma -s $SHELL 

Then stop the server request the update, migrate the db and start it again.

$ ./bin/pleroma stop
$ ./bin/pleroma_ctl update
$ ./bin/pleroma_ctl migrate
$ ./bin/pleroma daemon

Boom new version!

- [ pleroma]

discard unstaged changes

06-09-22

%

Have a bunch of changes staged and want to drop the rest? Easy:

$ git restore .

source

- [ git]

diy git remote on nas storage

26-06-22

%

Got a repo with sensitive data you don't want to push to a remote server you don't control? Have a NAS setup on our home network? Here is how to setup a folder on that NAS to act as a git remote.

*Step 1:

Change directorys to the NAS and clone the local folder with the --bare option.

$ cd /Volumes/travis/git
$ git clone --bare ~/.password-store

This creates /Volumes/travis/git/.password-store but without a working directory. Basically its just the /.git part of the repo.

Step 2:

Setup the NAS file path to be a git remote on the repo.

$ cd ~/.password-store
$ git remote add nas /Volumes/travis/git/travisshears.com.git
...

Step 3:

Done. Now jus push.

$ git push nas
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 10 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.30 KiB | 1.30 MiB/s, done.

- [ gitnas]

update npm packages to latest versions

15-06-22

%

I was doing some housekeeping on a Node.JS project today. Wanted to update all the dependencies to their latest versions. At first I tried npm update but come to find out that is ment more for upgrading single packages and not major versions. In the end after some googling I found npm-check-updates.

To upgrade the dependencies of a project without installing anything else I ran:

$ npx npm-check-updates -u

It updated the package.json so it must be followed up by a:

$ npm i

Which will install the new packages and update the package-lock.json.

source

- [ npmjsnode]

remap §/± to `/~ on max osx

05-06-22

%

Got a new Macbook Pro with the start of my new job and I love it. Except. It did not come with and english keyboard. Now every time I try to type the backslash (`) or tilde (~) instead I get "§" or "±" 😔. Lucky for me there are a bunch of people with the same issue.

This base command to remap the key is:

$ hidutil property --set '{"UserKeyMapping":
    [{"HIDKeyboardModifierMappingSrc":0x700000064,
      "HIDKeyboardModifierMappingDst":0x700000035}]
}'

but to make this survive a computer restart things get a little more complicated. For that you need you need a LaunchDaemon.

/Library/LaunchDaemons/org.custom.backslash-key-remap-fix.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.custom.backslash-key-remap-fix</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/travis.shears/projects/scripts/bin/backslash-key-remap-fix.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
  </dict>
</plist>

/Users/travis.shears/projects/scripts/bin/backslash-key-remap-fix.sh

#/bin/sh -e

hidutil property --set '{"UserKeyMapping":
    [{"HIDKeyboardModifierMappingSrc":0x700000064,
      "HIDKeyboardModifierMappingDst":0x700000035}]
}'

Sources:

- [ macosx]

fix git file mode changes

04-06-22

%

Before changing laptops I backed up all my personal projects to my NAS. When I transfer them back the file modes got messed up and a git status returned this:

diff --git a/docker/Dockerfile b/docker/Dockerfile
old mode 100644
new mode 100755
diff --git a/lib/DeployTool/CLI.rakumod b/lib/DeployTool/CLI.rakumogpg --list-secret-keys --keyid-format LONG <EMAIL>d
old mode 100644
new mode 100755
diff --git a/lib/DeployTool/Config.rakumod b/lib/DeployTool/Config.rakumod
old mode 100644
new mode 100755

Git diff shell magic, thanks to Stanislav Khromov, to the rescue!

$ git diff -p -R --no-ext-diff --no-color \
    | grep -E "^(diff|(old|new) mode)" --color=never  \
    | git apply

This command uses git diff and some clever grep logic to swap the file modes back to what git remembers them as.

I also converted the snippet to a shell script here

source: Stanislav Khromov's blog

- [ git]

trust gpg key

04-06-22

%

Just moved computers and thus moved my pass store. After importing my gpg keys, explained in this snippet, I had to trust them in order to stop the annoying warnings everytime I created a new password.

source

- [ gpg]

per company git config

03-06-22

%

Started new job this week and I wanted to have a seprate email on my work related repos then my personal ones. Cool thing is git supports conditional config file includes!

~/.gitconfig

# per-user git config
[user]
name = Travis Shears
email = t@travisshears.com

[includeIf "gitdir:~/company-x/"]
  path = .gitconfig-company-x

~/.gitconfig-company-x

# Company X spefic git config
[user]
name = Travis Shears
email = travis.shears@company-x.com

Now any commits made under the directory ~/company-x will use the email travis.shears@company-x.com and not my personal email.

source

- [ git]

next page