Skip to content

Instantly share code, notes, and snippets.

View knu's full-sized avatar

Akinori Musha knu

  • Tokyo, Japan
  • 18:35 (UTC +09:00)
  • X @knu
View GitHub Profile
@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 March 10, 2024 06:21
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"),"]",""))))))
@knu
knu / open-chrome-in-profile.applescript
Last active June 21, 2022 03:18
Open a Google Chrome window in a specified profile
if application "Google Chrome" is not running then
tell application "Google Chrome" to activate
end if
tell application "System Events" to tell process "Google Chrome"
set frontmost to true
click (menu item 1 where its name contains "****") of (menu 1 where its name is "プロファイル" or its name is "Profiles") of menu bar 1
end tell
@knu
knu / README.md
Last active June 16, 2022 01:38
Audio Input fixer for AirPods users

Audio Input fixer for AirPods users

This is a Hammerspoon snippet with a utility script to fix the common problem with the AirPods where connecting AirPods to a MacBook forcibly switches the audio input device to the AirPods mic when you have a better mic on it.

Requirements

brew install hammerspoon jq switchaudio-osx