Welcome to my dotfiles, a life-long project of improving my computing environment.
Please help yourself to the many person-years of experience it took to compile and tweak these tender, juicy, and yet lean, configurations.
Below is a sampling of what to expect:
Do you use a Bash shell every day, all day? Here is a selection of some handy
tiny helpers to help you love life a little bit more. I invite you to adopt them
into your own dotfiles home, source
them, nourish them.
First, a quick table of contents:
- Searching, Replacing, Navigating
f
Find file named like $1 in the cwdfcd
Find directory named like $1 under cwd and cd into itp
Find a running process named like $1g
Grep in cwdgg
Double-grep: grep with files resulting of the first grepgreplace
Grep in cwd and replace $1 with $2 in-line.ingrep
Grep in files named like $1 for subpattern $2up
cd to root of the current repository (must have!)
- Other utilities
domain
Extract domains from URLswhois
A smarter drop-in replacement forwhois
dns
Look up DNS entries for a URLbak
Move target $1 to $1.bakunbak
Revert previously bak'd $1 targetw
Watch a command for diffs every second
f() {
find . -name "*$**"
}
$ f help
./helpers.bash
fcd() {
target=$(find . -name "*$**" -type d | head -n1)
if [[ "$target" ]]; then
cd "$target"
else
echo "Directory not found: $*"
return
fi
}
dotfiles $ fcd bin
dotfiles/local/bin $
p() {
ps aux | grep "$*"
}
$ p iterm
shazow 540 5.7 2.1 ... 23:03.17 /opt/homebrew-cask/Caskroom/iterm2-beta/1.0.0.20140629/iTerm.app/Contents/MacOS/iTerm
g() {
grep --exclude-dir='*/\.*' -Ir "$(echo $*)" .
}
$ g "cwd and cd"
./helpers.bash:# Find directory named like $1 under cwd and cd into it
gg() {
grep --exclude-dir='*/\.*' -Irl "${1}" . | xargs grep -I "${2}"
}
$ # Let's find all `source`-ing in files that mention `helpers.bash`
$ gg "helpers.bash" "source"
./.bashrc: source $DOTFILES_PATH/.bash_profile
...
greplace() {
grep --exclude-dir='*/\.*' -Irl "$1" . | while read i; do
echo "Replacing: $i"
perl -p -i -e "s/$1/$2/g" "$i"
done
}
$ greplace "fcd" "findcd"
Replacing: ./helpers.bash
ingrep() {
find . -name "*${1}*" -exec grep -I "${2}" {} +
}
$ ingrep bash randomline
./.bash_aliases:alias cdrandom='cd "$(lsdir | randomline $(lsdir | wc -l))"'
./helpers.bash:randomline() {
up() {
readonly old_pwd="$PWD"
while [[ 1 ]]; do
cd ..
if [[ "$PWD" == "/" ]]; then
cd "$old_pwd"
echo "No repository found, returned to $PWD"
return 1
fi
for repo in ".git" ".hg"; do
if [[ -d "$repo" ]]; then
echo "Found $repo at $PWD"
return 0
fi
done
done
}
dotfiles/local/bin $ up
Found .git at .../dotfiles
dotfiles $
Convert http://www.foo.com/bar
into foo.com
.
domain() {
local parts=(${1//\// });
local domain="${parts[1]}"
if [[ ! "$domain" ]] || [[ "$domain" != *.* ]]; then
domain="${parts[0]}"
fi
echo "${domain/www./}"
}
Drop-in replacement for whois
that accepts URLs and does the parsing for us.
whois() {
$(which whois) "$(domain $1)"
return $?
}
dns() {
dig +nocmd "$(domain $1)" any +multiline +noall +answer
}
bak() {
declare target=$1;
if [[ "${target:0-1}" = "/" ]]; then
target=${target%%/}; # Strip trailing / of directories
fi
mv -v $target{,.bak}
}
$ bak helpers.bash
helpers.bash -> helpers.bash.bak
unbak() {
declare target=$1
if [[ "${target:0-1}" = "/" ]]; then
# Strip trailing / of directories
target="${target%%/}"
fi
if [[ "${target:0-4}" = ".bak" ]]; then
mv -v "$target" "${target%%.bak}"
else
echo "No .bak extension, ignoring: $target"
fi
}
$ unbak *.bak
helpers.bash.bak -> helpers.bash
w() {
watch -dn1 $*
}
There is even more stuff in
helpers.bash,
like go
which deals with work environments and switching in/out of them. Dig
around, see what you find.
I recommend copypasta'ing the code into your own dotfiles and allowing them to evolve to suit your workstyle. Maybe ping @shazow in your commit message as a thanks. :)
Find a bug or a simple improvement? Please send a pull request.
MIT.