Skip to content

Instantly share code, notes, and snippets.

@dropofwill
Last active August 29, 2015 14:25
Show Gist options
  • Save dropofwill/473b4bfdd4b2359ebf5f to your computer and use it in GitHub Desktop.
Save dropofwill/473b4bfdd4b2359ebf5f to your computer and use it in GitHub Desktop.
Some fun zsh scripts to add to your .zshrc
# Run a simple ruby server on port 3000 in the current dir
# or another port that you pass in
#
# usage default port: $ serve
# custom port: $ serve 4000
#
function serve {
port="${1:-3000}"
ruby -run -e httpd . -p $port
}
# Hide dotfiles in Finder (OSX)
function hidefiles(){
defaults write com.apple.finder AppleShowAllFiles -bool NO
killall Finder
}
# Show dotfiles in Finder (OSX)
function showfiles(){
defaults write com.apple.finder AppleShowAllFiles -bool YES
killall Finder
}
# Start mpd and ncmpcpp
function tunes() {
mpd
ncmpcpp
}
# Swap between vim and zsh with <C-z> seamlessly
# Vim already throws itself in the background with <C-z>
# To bring it back, use foreground `fg`
# This simply adds some safety logic and binds fg to <C-z>
# So you can swap back and forth
fancy-ctrl-z () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
# Open files in text edit from the terminal
textedit() {
if [[ $# = 0 ]]
then
open -t
else
[[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
open -t "$F"
fi
}
# Convert flacs in a folder to mp3 of the same name
flac_to_mp3 () {
for a in *.flac; do
ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done
}
# When browsing directory structures I often change my mind about whether
# I want to interact with a directory or file, this function checks
# If the first argument is a regular file (-f) and opens it with vim instead
# of cd if it is, otherwise it cds as normal.
# usage $ pwd
# -> /Users/blah/blah
# # works normally when given a directory
# $ cd ../directory && pwd
# -> /Users/blah/blah/directory
# # opens a vim session when given a regular file
# $ cd text.txt
# -> vim session with text.txt
# # doesn't change the directory
# $ pwd
# -> /Users/blah/blah/directory
#
function smart_cd () {
if [[ -f $1 ]]
then
vim $1
else
cd $1
fi
}
alias cd='smart_cd'
# If the first argument is a regular file (-f) and views it with less instead
# of ls if it is, otherwise it ls as normal.
function smart_ls () {
if [[ -f $1 ]]
then
less $1
else
ls $1
fi
}
alias ls='smart_ls'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment