Skip to content

Instantly share code, notes, and snippets.

@robmiller
robmiller / .gitconfig
Created July 17, 2013 07:52
Some useful Git aliases that I use every day
#
# Working with branches
#
# Get the current branch name (not so useful in itself, but used in
# other aliases)
branch-name = "!git rev-parse --abbrev-ref HEAD"
# Push the current branch to the remote "origin", and set it to track
# the upstream branch
publish = "!git push -u origin $(git branch-name)"
@robmiller
robmiller / git-cleanup-repo
Last active October 14, 2025 20:53
A script for cleaning up Git repositories; it deletes branches that are fully merged into `origin/master`, prunes obsolete remote tracking branches, and as an added bonus will replicate these changes on the remote.
#!/bin/bash
# git-cleanup-repo
#
# Author: Rob Miller <rob@bigfish.co.uk>
# Adapted from the original by Yorick Sijsling
git checkout master &> /dev/null
# Make sure we're working with the most up-to-date version of master.
git fetch
@robmiller
robmiller / msnify
Created December 27, 2024 22:06
msnify – convert text into an alternate-caps masterpiece befitting of a 2002 MSN status
#!/usr/bin/env -S ruby -n
#
# echo "I find it kind of funny, I find it kind of sad, the dreams in which I'm dying are the best I've ever had" | msnify
# #=> ~*~i fInD It kInD Of fUnNy, I FiNd iT KiNd oF SaD, tHe dReAmS In wHiCh i'm dYiNg aRe tHe bEsT I'Ve eVeR HaD~*~
puts "~*~" + $_.chars.each_slice(2).map { |s1, s2| [s1.downcase, s2.to_s.upcase] }.join.chomp + "~*~"
@robmiller
robmiller / hanging-punctuation-polyfill.js
Created August 20, 2024 16:05
Extremely simple polyfill for hanging-punctuation in Chrome
/*
I only really use hanging-punctuation: first and only expect it to
work in blockquotes, which makes the usage extremely simple. And yet
for some reason I've never done a simple polyfill. Now I have.
*/
(function() {
if ( !CSS.supports("hanging-punctuation", "first") ) {
document.querySelectorAll("p").forEach(e => { if ( e.innerText.trim()[0] == "“" ) { e.style.textIndent = "-0.725ch"; } });
}
})();
@robmiller
robmiller / mdtable.rb
Created February 6, 2024 17:55
Script for aligning a Markdown table neatly. Put in your PATH as mdtable then use in Vim visual mode: !mdtable
#!/usr/bin/env ruby
table = ARGF.read.chomp
parse_row = ->(row) { row.gsub(/^\||\|$/, "").split("|").map(&:strip) }
rows = table.each_line.map(&parse_row)
# Normalise column count
column_count = rows.map(&:length).max
rows = rows.map { |row| (row + ([""] * column_count)).take(column_count) }
@robmiller
robmiller / pangram.rb
Created July 28, 2023 08:43
Check if a string is a pangram in Ruby.
class String
def pangram?
(("a".."z").to_a - self.downcase.chars.uniq).empty?
end
end
["The quick brown fox jumped over the lazy dogs!", "The slow dog didn't"].each do |text|
puts "#{text}: #{text.pangram?}"
end
@robmiller
robmiller / gist:214b3adf820b41a847c2
Last active August 7, 2022 18:53
Text processing tutorial: splitting files, rows to columns, line endings

For this tutorial, we have the following brief. That is: to take a file that contains many millions of lines of text, each one of them a single column — something that looks like this:

POPLKLMNE
GKITLOPOM
QASSLKOPI
== snip ==

...into multiple text files, each with a fixed number of lines in them, each with — instead of one column per line — several columns, separated with a comma. In other words, something that looks like this:

POPLKLMNE,GKITLOPOM,QASSLKOPI
@robmiller
robmiller / .vimrc
Last active February 18, 2022 11:53
Autoload sessions created by tpope's vim-obsession when starting Vim.
augroup sourcesession
autocmd!
autocmd VimEnter * nested
\ if !argc() && empty(v:this_session) && filereadable('Session.vim') |
\ source Session.vim |
\ endif
augroup END
@robmiller
robmiller / play-wordle.rb
Last active February 15, 2022 19:40
A command-line, offline version of Wordle. A new word every time you run it.
#!/usr/bin/env ruby
#
# Play a command-line version of Wordle
#
# Original game by Josh Wardle: https://www.powerlanguage.co.uk/wordle/
#
# Installation and usage:
#
# 1. Save this file somewhere as play-wordle.rb
# 2. Run `ruby play-wordle.rb`
@robmiller
robmiller / kramdown_block_ids.rb
Created January 20, 2022 21:11
Add automatically generated unique IDs to block elements (p, ul, blockquote, etc.) generated by kramdown, which allows you to link to any paragraph on a page
class Kramdown::Converter::Html
def add_block_count(attr)
@block_count ||= 0
@block_count += 1
unless attr["id"]
attr["id"] = "b:#{@block_count}"
end
end
%i(format_as_block_html format_as_indented_block_html).each do |method|