Your best shell aliases

Bring your A game, here’s my contribution

  alias gi='goimports -w .'

Not exactly an alias, but the shell command in my profile that does the most for me:

export CDPATH=.:$HOME:$GOROOT/src:$GOPATH/src/$GOPATH/src/$GOPATH/src/

@robpike That one brings me joy every day.

I recently had to unset this one, but for the longest time

alias gb='go install -v'

alias fr=‘find . -type f -print0 | xargs -0’

alias gtvc="go test -v -cover"
alias todo="godoc -notes="TODO" ."
cover() { 
  go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -func=$t && unlink $t

cover-web() {
  go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -html=$t && unlink $t

I don’t have very many aliases, just for things I can’t seem to remember or is difficult to look up. For example (sort-of related to go), not an alias but i do keep it in my ~/.bash_aliases anyway

docker() {
	if [ "$1" != "shell" ]; then
		command docker "$@"
	if [ -n "$2" ]; then
		command docker exec -it "$2" bash
		echo 'docker: "shell" requires 1 argument.'
		echo "Usage:  docker shell CONTAINER"
		echo "Open bash shell in container"
1 Like

I like this one (only for zsh):

alias -s go="go run"

Now if you type hello.go and hit enter, it will execute go run hello.go


also worth mentioning to install bash-completion to provide TAB completion on CDPATH

after working through a day, i just noticed why i don’t use very many aliases, this in my .inputrc

"\ep": history-search-backward
"\en": history-search-forward

I’ll type a few letters and then Alt-p or Alt-n to find what i want, making any specific changes if necessary. A bit dumb, but flexible.

Rather exotic use-case - if you have an executable and forgot where it was built from, import-path tells you its import path:

import-path() {
        [[ -z "$1" ]] && { echo "usage: import-path EXECUTABLE" >&2; return 1; }
        go tool objdump -s main.main "$(which $1)" |
                grep -E '^TEXT main.main' | cut -d' ' -f3 |
                sed -e 's/.*\/src\/\(.*\)\/[^\/]*/\1/'
$ import-path godoc
1 Like

Not an alias, but a bash completion program for the “go” command. It’s especially useful with “go doc” since it will tab complete packages, symbols, and methods for you.

complete -C gotab -o nospace go

It requires the gotab binary to produce completion suggestions.

1 Like

Also not an alias but still useful for me sometimes.

It will give you all dependencies of a package that are not in the standard library using go list. Unfortunately I can’t copy paste here using my mobile :confused: but I have put ît on github anyway:

Alias and shell script for setting GOPATH and appending $GOPATH\bin to $PATH:

alias goenv="source /home/dgnorton/bin/"


# Source this script from within any directory within a valid potential
# GOPATH and it will set it.  It looks for the first parent directory
# named "go" or "Go" and sets GOPATH to it.

while :
	d=$(basename $(dirname $c))
	if [ "$d" == "/" ]; then
		echo not a valid GOPATH
	elif [ "$d" == "go" ] || [ "$d" == "Go" ]; then
		export GOPATH=$(dirname $c)
		export PATH=$PATH:$GOPATH/bin
		echo GOPATH set to $GOPATH
		echo Appended $GOPATH/bin to PATH
		c=$(dirname $c)
depgraph() {
   pkg=$(go list $@)
   (   echo "digraph G {"
       go list -f '{{range .Imports}}{{printf "\t%q -> %q;\n" $.ImportPath .}}{{end}}' \
          $(go list -f '{{join .Deps " "}}' $pkg) $pkg
       echo "}"
   ) | dot -Tsvg -o /tmp/deps.svg

Adapted from @adg’s GolangUK talk,

1 Like

Do you know of an equivalent trick for p9p rc ? (specifically for rc in an acme win, if that matters).

Change your shell prompt to % and alias it to the empty string.

$ PS1='% ' 
% alias %=

Then on X11 you can triple-click to copy a complete shell command, including the prompt, and paste it using a middle click to execute it again. The extra % is ignored.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.