Skip to content

Instantly share code, notes, and snippets.

View zwimer's full-sized avatar
🤯
Learning

Zachary Wimer zwimer

🤯
Learning
View GitHub Profile
@zwimer
zwimer / bools.cpp
Created November 12, 2024 14:56
C++ bools define `true` as `value == 1`, not merely `value != 0`; casts are dangerous!
#include <iostream>
void f(bool b) {
if (b == true) { std::cout << "true\n"; }
if (b == false) { std::cout << "false\n"; }
}
int main() {
char c = 2;
bool * b = (bool*) &c;
@zwimer
zwimer / tricks.txt
Created November 12, 2024 14:53
A few useful bash tricks
Random useful bash stuff:
1. . ^foo^bar - bash !! but replaces the first occurrence of foo with bar
2. !!:/gs/foo/bar - bash !! but replaces all occurrences of foo with bar
3. cd -4 = cd; cd; cd; cd. cd -- = cd; cd. cd - = cd; pwd (or use pushd and popd)
4. diff <(sort a) <(sort b) The output of sort a is pushed into a pipe which pretends to be a file for diff. This is process substitution
5. < foo grep bar | sort is the same as cat foo | grep bar | sort
6. $'\167' is the same as w - bash interprets octal! Also works with hex!
7. If you do echo !!:4 will echo the 4th argument of the last command. !$ is the last arg and !^ is the fist, !* is all.
8. !g will autocomplete to the last command run that began with g
9. set -o vi - enable vim bindings for bash
@zwimer
zwimer / git-truncate-history.sh
Created November 5, 2024 07:40
Truncate `git` History
#!/bin/bash -eux
# Where to truncate from
export HASH=my-hash
# Truncate history
git checkout --orphan temp "${HASH}"
git commit -m "Truncate History"
git rebase --onto temp "${HASH}" master
git branch -D temp
@zwimer
zwimer / blame.patch
Created October 28, 2024 15:13
Adding a `pre-blame` hook to `git`
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index 0397dec64d..f9cce044fe 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -758,6 +758,12 @@ and "0" meaning they were not.
Only one parameter should be set to "1" when the hook runs. The hook
running passing "1", "1" should not be possible.
+pre-blame
+~~~~~~~~~~~~~~