Skip to content

Instantly share code, notes, and snippets.

View knu's full-sized avatar

Akinori Musha knu

View GitHub Profile
#!/bin/sh
set -eu
app=${1:-ArchTest.app}
rm -rf "$app"
mkdir -p "$app/Contents/MacOS"
cat <<'PLIST' >"$app/Contents/Info.plist"
@knu
knu / mise-safe-upgrade
Last active March 1, 2025 09:04
Zero-downtime mise upgrade
#!/bin/sh
set -e
config=$HOME/.config/mise/config.toml
config_bak=${config%.toml}.bak.toml
while getopts "f:" opt; do
case $opt in
f)
@knu
knu / comparing_dot-separated_version_numbers.sh
Created April 8, 2024 13:25
Comparing dot-separated version numbers in shell script (one-liners)
# Comparing dot-separated version numbers in one line
# How to say $a >= $b using various tools and languages?
a=1.10.0
b=1.9.5
set -e
# POSIX
[ "$({ echo $a; echo $b; } | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n1)" = "$a" ]
#!/bin/sh
Verbose=
DryRun=
run() {
if [ -n "$DryRun" ]; then
echo "$*"
elif [ -n "$Verbose" ]; then
echo "$*"
@knu
knu / open-chromium
Last active December 10, 2024 15:23
JXA script to open a URL in a specified Chromium-based browser
#!/usr/bin/osascript -l JavaScript
// -*- javascript -*-
//
// open-chromium: Open a URL in a Chromium-based browser in a specified profile
//
// usage: open-chromium [(-a | --application) <app>] [(-p | --profile) <profile>] <url>
//
// <app>: "Chromium" (default), "Google Chrome", "Microsoft Edge", etc.
// <profile>: "Default", "Profile 1", etc.
//
@knu
knu / Importing ghq directories to projectile.md
Created November 29, 2023 12:19
Importing ghq directories to projectile.md

Create a timer that periodically imports ghq directories to projectile-known-projects.

(defun projectile-update-known-projects-with-ghq ()
  (interactive)
  (when (executable-find "ghq")
    (make-process :name "ghq"
                  :buffer (get-buffer-create "*ghq*")
@knu
knu / tweak-eval-defun.el
Created September 11, 2023 15:55
Get eval-defun to find a non-toplevel defun/defvar nearest to the point
;; (require 'smartparens)
(defun eval-defun:inner-def (orig-func &rest args)
(cl-letf*
(((symbol-function #'beginning-of-defun)
(lambda (&optional arg)
(let (pos)
(save-match-data
(cl-loop
do
@knu
knu / exit-minibuffer--prompt-for-make-directory.el
Last active September 5, 2023 13:20
Emacs snippet: Prompt user for make-directory when a non-existent directory is input in read-file-name
;; Prompt user for make-directory when a non-existent directory is input in read-file-name
(defun exit-minibuffer:prompt-for-make-directory (&rest args)
(and (cl-loop
for (_ func) in (backtrace-frames)
with n = 0
do (cl-case func
(read-file-name
(cl-return t))
(read-char-from-minibuffer
(cl-return nil))
@knu
knu / ijq
Created January 16, 2023 07:55
ijq - interactive jq
#!/bin/sh
#
# ijq - interactive jq
#
# usage: ijq [jq options] < file
#
# e.g. curl ... | ijq | tee output.json
#
# Copyright (c) 2023 Akinori MUSHA
#
@knu
knu / excel-formula-to-get-the-current-sheet-name.txt
Created July 5, 2022 11:32
Excel formula to get the current sheet name
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("_"&REPT("]",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"]",""))),SUBSTITUTE(CELL("filename"),"]","_"&REPT("]",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"]",""))),(LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"]",""))))))