Skip to content

Instantly share code, notes, and snippets.

@robmiller
robmiller / git-cleanup-repo
Last active February 27, 2024 10:09
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 / 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 / .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 / 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|
#!/usr/bin/env ruby
#
# Trying to guess what the best first-choice word in Wordle might be.
#
# Author: Rob Miller <r@robm.me.uk>
# Boosts the score of a word that matches the first letter, with the
# rationale that getting the first letter makes the word easier for
# a human being to guess
FIRST_LETTER_WEIGHT = 2
@robmiller
robmiller / delete-tweets.rb
Created July 15, 2021 15:59
Script for deleting tweets from a Twitter data export
#!/usr/bin/env ruby
gem "twitter", "~> 7.0"
gem "http", "~> 4.4"
require "twitter"
require "http"
require "json"
require "yaml"