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.


discard unstaged changes -- 06.09.2022 %

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.2022 %

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.
\- [ git, nas ]

update npm packages to latest versions -- 15.06.2022 %

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

\- [ npm, js, node ]

sign git commits -- 08.06.2022 %

Who doesn’t want the cool “verified” badge in Gitlab.

coolness badge

coolness badge

To get this we must sign our commits via gpg.

Step 1: Figure out which key you are going to use

$ gpg --list-keys
pub   rsa3072 2020-01-01 [SC] [expires: 2030-01-01]
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
uid           [ultimate] Travis Shears <travis.shears@cool-company.com>
sub   rsa3072 2020-01-01 [E] [expires: 2030-01-01]

pub   rsa2048 2020-01-01 [SC] [expires: 2030-01-01]
      BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
uid           [ultimate] Travis Shears <t@travisshears.com>
sub   rsa2048 2020-01-01 [E] [expires: 2030-01-01]

In this case I’ll use key travis.shears@cool-company.com.

Step two: Configure git to sign commits with the gpg key

Edit your ~/.gitconfig to look something like this

   [user]
   name = Travis Shears
   email = travis.shears@cool-company.com
   signingkey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
   
   [commit]
       gpgsign = true

We added the signingkey so git knows which key to use and we specified gpgsign so git knows we want to sign all commits.

Step three: Copy your public gpg key to clipboard

$ gpg --armor --export AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | pbcopy

You can also do it with the email of the key

$ gpg --armor --export travis.shears@cool-company.com | pbcopy

Step four: Paste your public gpg key into settings page of your favorite version control site, ex: Github, Gitlab, Source Hut.


source

\- [ git, gpg ]

remap §/± to `/~ on max osx -- 05.06.2022 %

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:

\- [ osx ]

fix git file mode changes -- 04.06.2022 %

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.2022 %

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.2022 %

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 ]

ios xcode simulator dark mode -- 02.05.2022 %

Recently I was testing an IOS app on my wifes phone. The UI was completly broken. Turns out she had dark mode enabled. That led me down the path of adding dark mode support to the app. Which is testable via the Xcode simulator if you know how to enable it.

Command + Shift + A: Toggles dark mode

source: https://www.kindacode.com/article/how-to-toggle-dark-mode-on-ios-simulator/

\- [ xcode ]

bash extract file name -- 17.03.2022 %

Comparing the following shows how to use basename to extract just the file name from a full path.

$ for file in ./content/**/*.md ; do echo $file ; done | head -10
./content/_index.de.md
./content/_index.en.md
./content/_index.ru.md
./content/blog/_index.de.md
./content/blog/_index.en.md
./content/blog/ahrn-valley/index.en.md
./content/blog/archiving-corona-cal/index.en.md
./content/blog/arco/index.de.md
./content/blog/arco/index.en.md
./content/blog/armycookbot/index.de.md


$ for file in ./content/**/*.md ; do file=$(basename $file) && echo $file ; done
_index.de.md
_index.en.md
_index.ru.md
_index.de.md
_index.en.md
index.en.md
index.en.md
index.de.md
index.en.md
index.de.md
\- [ sh, bash ]