Suite à un post sur coderwall.com, je me suis dit que cela valait le coup d’améliorer un peu mon fichier .bashrc
(et de le partager avec vous).
C’est ce fichier (caché puisqu’il débute par un point) qui contient vos préférences. Traditionnellement, le fichier ~/.bashrc
est chargé quand le shell est invoqué comme shell interactif sans fonction de connexion (pour la remise à niveau sur les fichiers de démarrage du shell Bash, c’est par ici).
Pour activer le chargement du contenu de ce fichier sous OS X automatiquement, il vous faut tout d’abord éditer le fichier ~/.profile.
Commencez par faire un
cd
dans votre Terminal pour vous situer à la racine de votre répertoire personnel.
Puis éditez le fichier ~/.profile
avec nano : nano .profile
.
Vous pouvez ensuite ajouter les lignes suivantes :.
if [ -n "$BASH_VERSION" ] then # include .bashrc if it exists if [ -f ~/.bashrc ] then . ~/.bashrc fi fi # set PATH so it includes user's private bin if it exists if [ -d ~/bin ] then PATH=~/bin:"${PATH}" fi
Une fois ceci entré dans l’éditeur nano, enregistrez la modification en faisant : [ctrl + x], puis y (yes), puis valider en tapant sur [entrée].
Concernant le fichier ~/.bashrc
en lui-même, voici pour exemple le mien. Il s’agit d’une agrégation de plusieurs alias, fonctions ou commandes dont vous retrouverez les auteurs dans le code.
Commencez par faire un cd
dans votre Terminal pour vous situer à la racine de votre répertoire personnel.
Puis éditez le fichier ~/.bashrc
avec nano : nano .bashrc
.
Collez ensuite votre code :
# Copyright (c) 2013 yvan godard - godardyvan@gmail.com - https://www.yvangodard.fr # This code is under the WTFPL license # Unless special mentions, this code uses some portions of code by Fernando Cea - https://coderwall.com/p/lyutxw # Easier navigation, thanks to josef jezek - http://alias.sh/control-cd-command-behavior # a quick way to get out of current directory alias ..='cd ..' alias .2='cd ../..' alias .3='cd ../../..' alias .4='cd ../../../../' alias .5='cd ../../../../../' alias .6='cd ../../../../../../' alias .7='cd ../../../../../../../' alias .8='cd ../../../../../../../../' alias ~="cd ~" # `cd` is probably faster to type though alias -- -="cd -" # Get rid of command not found alias cd..='cd ..' # Go back x directories # thanks to benjy & eric lucas - http://alias.sh/go-back-n-directories b() { str="" count=0 if [ $# -eq 0 ] then cd .. else while [ "$count" -lt "$1" ]; do str=$str"../" let count=count+1 done cd $str fi } # Shortcuts alias d="cd ~/Documents" alias dp="cd ~/Dropbox" alias dl="cd ~/Downloads" alias dt="cd ~/Desktop" alias g="git" alias h="history" alias j="jobs" alias n="nano" alias v="vim" alias m="mate ." alias s="subl ." alias o="open" alias oo="open ." # Detect which `ls` flavor is in use if ls --color > /dev/null 2>&1; then # GNU `ls` colorflag="--color" else # OS X `ls` colorflag="-G" fi # List all files colorized in long format alias l="ls -l ${colorflag}" # List all files colorized in long format, including dot files alias la="ls -la ${colorflag}" # List only directories alias lsd='ls -l ${colorflag} | grep "^d"' # Always use color output for `ls` alias ls="command ls ${colorflag}" export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:' # Enable aliases to be sudo’ed alias sudo='sudo ' # Gzip-enabled `curl` alias gurl="curl --compressed" # Get OS X Software Updates, Munki upadtes and update installed Ruby gems, Homebrew, npm, and their installed packages alias update='sudo softwareupdate -i -a if [ -e /usr/local/bin/brew ] then echo "" && echo ">>> Brew Updates :" brew update brew upgrade brew cleanup fi if [ -e /usr/local/munki/managedsoftwareupdate ] then echo "" && echo ">>> Munki Updates :" sudo /usr/local/munki/managedsoftwareupdate -v sudo /usr/local/munki/managedsoftwareupdate --installonly fi if [ -e /usr/local/bin/npm ] then echo "" && echo ">>> npm updates :" sudo npm update npm -g sudo npm update -g fi sudo gem update' # IP addresses alias ip="dig +short myip.opendns.com @resolver1.opendns.com" # alias ip="curl -s ifconfig.me" alias localip="ipconfig getifaddr en1" alias ips="ifconfig -a | grep -o 'inet6\? \(\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\|[a-fA-F0-9:]\+\)' | sed -e 's/inet6* //'" # Enhanced WHOIS lookups alias whois="whois -h whois-servers.net" # Flush Directory Service cache alias flush="dscacheutil -flushcache && killall -HUP mDNSResponder" # Clean up LaunchServices to remove duplicates in the “Open With” menu alias lscleanup="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user && killall Finder" # View HTTP traffic alias sniff="sudo ngrep -d 'en1' -t '^(GET|POST) ' 'tcp and port 80'" alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E \"Host\: .*|GET \/.*\"" # Canonical hex dump; some systems have this symlinked command -v hd > /dev/null || alias hd="hexdump -C" # OS X has no `md5sum`, so use `md5` as a fallback command -v md5sum > /dev/null || alias md5sum="md5" # OS X has no `sha1sum`, so use `shasum` as a fallback command -v sha1sum > /dev/null || alias sha1sum="shasum" # Trim new lines and copy to clipboard alias c="tr -d '\n' | pbcopy" # Recursively delete `.DS_Store` files alias cleanup="find . -type f -name '*.DS_Store' -ls -delete" # ROT13-encode text. Works for decoding, too! 😉 alias rot13='tr a-zA-Z n-za-mN-ZA-M' # Empty the Trash on all mounted volumes and the main HDD # Also, clear Apple’s System Logs to improve shell startup speed alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl" # Show/hide hidden files in Finder alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder" alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder" # Hide/show all desktop icons (useful when presenting) alias hidedesktop="defaults write com.apple.finder CreateDesktop -bool false && killall Finder" alias showdesktop="defaults write com.apple.finder CreateDesktop -bool true && killall Finder" # URL-encode strings alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"' # Merge PDF files # Usage: `mergepdf -o output.pdf input{1,2,3}.pdf` alias mergepdf='/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py' # Disable Spotlight alias spotoff="sudo mdutil -a -i off" # Enable Spotlight alias spoton="sudo mdutil -a -i on" # PlistBuddy alias, because sometimes `defaults` just doesn’t cut it alias plistbuddy="/usr/libexec/PlistBuddy" # Ring the terminal bell, and put a badge on Terminal.app’s Dock icon # (useful when executing time-consuming commands) alias badge="tput bel" # Intuitive map function # For example, to list all directories that contain a certain file: # find . -name .gitattributes | map dirname alias map="xargs -n1" # One of @janmoesen’s ProTip™s for method in GET HEAD POST PUT DELETE TRACE OPTIONS; do alias "$method"="lwp-request -m '$method'" done # Stuff I never really use but cannot delete either because of http://xkcd.com/530/ alias stfu="osascript -e 'set volume output muted true'" alias pumpitup="osascript -e 'set volume 7'" alias hax="growlnotify -a 'Activity Monitor' 'System error' -m 'WTF R U DOIN'" # configure my multi-line prompt # thanks to http://www.siteduzero.com/informatique/tutoriels/personnalisez-votre-shell-en-couleur/pilule-bleue-ou-rouge PS1=' [\033[35m$USER\033[30m@\033[32m$HOSTNAME\033[30m:\033[31m$PWD\033[30m] $ ==> ' # maintains a jump-list of the directories you actually use, thanks to rupa deadwyler - https://github.com/rupa/z # Tracks your most used directories, based on 'frecency'. After a short learning phase, z will take you to the most 'recent' directory that matches ALL of the regexes given on the command line. # # USE: # z foo # cd to most frecent dir matching foo # z foo bar # cd to most frecent dir matching foo and bar # z -r foo # cd to highest ranked dir matching foo # z -t foo # cd to most recently accessed dir matching foo # z -l foo # list matches instead of cd # z -c foo # restrict matches to subdirs of $PWD # # Optionally: # set $_Z_CMD in .bashrc/.zshrc to change the command (default z). # set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z). # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself. # set $_Z_EXCLUDE_DIRS to an array of directories to exclude. # [ -d "${_Z_DATA:-$HOME/.z}" ] && { echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory." } _z() { local datafile="${_Z_DATA:-$HOME/.z}" # bail if we don't own ~/.z (we're another user but our ENV is still set) [ -f "$datafile" -a ! -O "$datafile" ] && return # add entries if [ "$1" = "--add" ]; then shift # $HOME isn't worth matching [ "$*" = "$HOME" ] && return # don't track excluded dirs local exclude for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do [ "$*" = "$exclude" ] && return done # maintain the data file local tempfile="$datafile.$RANDOM" while read line; do # only count directories [ -d "${line%%\|*}" ] && echo $line done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" ' BEGIN { rank[path] = 1 time[path] = now } $2 >= 1 { # drop ranks below 1 if( $1 == path ) { rank[$1] = $2 + 1 time[$1] = now } else { rank[$1] = $2 time[$1] = $3 } count += $2 } END { if( count > 6000 ) { # aging for( x in rank ) print x "|" 0.99*rank[x] "|" time[x] } else for( x in rank ) print x "|" rank[x] "|" time[x] } ' 2>/dev/null >| "$tempfile" # do our best to avoid clobbering the datafile in a race condition if [ $? -ne 0 -a -f "$datafile" ]; then env rm -f "$tempfile" else env mv -f "$tempfile" "$datafile" || env rm -f "$tmpfile" fi # tab completion elif [ "$1" = "--complete" ]; then while read line; do [ -d "${line%%\|*}" ] && echo $line done < "$datafile" | awk -v q="$2" -F"|" ' BEGIN { if( q == tolower(q) ) imatch = 1 split(substr(q, 3), fnd, " ") } { if( imatch ) { for( x in fnd ) tolower($1) !~ tolower(fnd[x]) && $1 = "" } else { for( x in fnd ) $1 !~ fnd[x] && $1 = "" } if( $1 ) print $1 } ' 2>/dev/null else # list/go while [ "$1" ]; do case "$1" in --) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;; -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in c) local fnd="^$PWD $fnd";; h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;; x) sed -i "\:^${PWD}|.*:d" "$datafile";; l) local list=1;; r) local typ="rank";; t) local typ="recent";; esac; opt=${opt:1}; done;; *) local fnd="$fnd${fnd:+ }$1";; esac; local last=$1; shift; done [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1 # if we hit enter on a completion just go there case "$last" in # completions will always start with / /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;; esac # no file yet [ -f "$datafile" ] || return local cd cd="$(while read line; do [ -d "${line%%\|*}" ] && echo $line done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" ' function frecent(rank, time) { # relate frequency and time dx = t - time if( dx < 3600 ) return rank * 4 if( dx < 86400 ) return rank * 2 if( dx < 604800 ) return rank / 2 return rank / 4 } function output(files, out, common) { # list or return the desired directory if( list ) { cmd = "sort -n >&2" for( x in files ) { if( files[x] ) printf "%-10s %s\n", files[x], x | cmd } if( common ) { printf "%-10s %s\n", "common:", common > "/dev/stderr" } } else { if( common ) out = common print out } } function common(matches) { # find the common root of a list of matches, if it exists for( x in matches ) { if( matches[x] && (!short || length(x) < length(short)) ) { short = x } } if( short == "/" ) return # use a copy to escape special characters, as we want to return # the original. yeah, this escaping is awful. clean_short = short gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short) for( x in matches ) if( matches[x] && x !~ clean_short ) return return short } BEGIN { split(q, words, " "); hi_rank = ihi_rank = -9999999999 } { if( typ == "rank" ) { rank = $2 } else if( typ == "recent" ) { rank = $3 - t } else rank = frecent($2, $3) matches[$1] = imatches[$1] = rank for( x in words ) { if( $1 !~ words[x] ) delete matches[$1] if( tolower($1) !~ tolower(words[x]) ) delete imatches[$1] } if( matches[$1] && matches[$1] > hi_rank ) { best_match = $1 hi_rank = matches[$1] } else if( imatches[$1] && imatches[$1] > ihi_rank ) { ibest_match = $1 ihi_rank = imatches[$1] } } END { # prefer case sensitive if( best_match ) { output(matches, best_match, common(matches)) } else if( ibest_match ) { output(imatches, ibest_match, common(imatches)) } } ')" [ $? -gt 0 ] && return [ "$cd" ] && cd "$cd" fi } alias ${_Z_CMD:-z}='_z 2>&1' [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P" if compctl >/dev/null 2>&1; then # zsh [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list, avoid clobbering any other precmds. if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then _z_precmd() { _z --add "${PWD:a}" } else _z_precmd() { _z --add "${PWD:A}" } fi [[ -n "${precmd_functions[(r)_z_precmd]}" ]] || { precmd_functions+=(_z_precmd) } } _z_zsh_tab_completion() { # tab completion local compl read -l compl reply=(${(f)"$(_z --complete "$compl")"}) } compctl -U -K _z_zsh_tab_completion _z elif complete >/dev/null 2>&1; then # bash # tab completion complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z} [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list. avoid clobbering other PROMPT_COMMANDs. grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || { PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;' } } fi if [ "$TERM" != "dumb" ]; then cat <<-INFORMATION_ADMINS >>> Quelques commandes utiles : - Pour remonter dans l'arborescence, utilisez "cd .." ou "cd .3", "cd .4" etc. ou la fonction "b n" avec n=nombre de répertoire à remonter - Utilisez la commande "z" pour retrouver un dossier z foo # cd to most frecent dir matching foo z foo bar # cd to most frecent dir matching foo and bar z -r foo # cd to highest ranked dir matching foo z -t foo # cd to most recently accessed dir matching foo z -l foo # list matches instead of cd z -c foo # restrict matches to subdirs of $PWD - La commande "update" réaliser une mise à jour complète (Apple, Brew, Munki, npm, etc.) - Pour connaître l'adressage réseau : "ip" = votre ip publique, "localip" = votre ip locale, "ips" = toutes vos adresses - Pour forcer la vidange de toutes les corbeilles : "emptytrash" - Afficher / masquer les fichiers cachés dans le Finder : "show" / "hide" - Afficher / masquer les icônes sur votre bureau : "showdesktop" / "hidedesktop" - Activer / desactiver Spotlight : "spoton" / "spotoff" - Quelques raccourcis utiles : d="cd ~/Documents", dp="cd ~/Dropbox", dl="cd ~/Downloads", dt="cd ~/Desktop" g="git", h="history", j="jobs", n="nano", v="vim", m="mate .", s="subl .", o="open" l="ls -l", la="ls -la", lsd='ls -l | grep "^d"' gurl="curl --compressed" INFORMATION_ADMINS fi
Une fois ceci entré dans l’éditeur nano, enregistrez les modifications en faisant : [ctrl + x], puis y (yes), puis valider en tapant sur [entrée].
Pour charger la première fois cette modification, entrez la commande source ~/.bashrc
. J’ai personnalisé l’affichage du message de connexion de sorte de disposer d’un rappel des fonctions disponibles … cela devrait également vous aider.
Dans la série optimisation de l’usage du Terminal, je vous invite à relire aussi ce billet.
Et pour finir quelques sources utiles que j’ai utilisées :
- Le code de Fernando Cea pour les alias, les couleurs, etc.
- Les sources de la fonction que j’ai utilisée : “z”.
Ce n’est évidemment qu’un exemple, à chacun son usage de la console … à chacun sa personnalisation, alors n’hésitez pas à commenter !