Your best shell aliases

Bring your A game, here’s my contribution

  alias gi='goimports -w .'
2 Likes

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

export CDPATH=.:$HOME:$GOROOT/src:$GOPATH/src/golang.org:$GOPATH/src/github.com:$GOPATH/src/robpike.io
9 Likes

@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" ."
3 Likes
cover() { 
  t=$(tempfile)
  go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -func=$t && unlink $t
}

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

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 "$@"
		return
	fi
	if [ -n "$2" ]; then
		command docker exec -it "$2" bash
	else
		echo 'docker: "shell" requires 1 argument.'
		echo
		echo "Usage:  docker shell CONTAINER"
		echo
		echo "Open bash shell in container"
	fi
}
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

3 Likes

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
golang.org/x/tools/cmd/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/goenv.sh"

goenv.sh:

#!/bin/bash

# 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.

c=$(pwd)/dummy
while :
do
	d=$(basename $(dirname $c))
	if [ "$d" == "/" ]; then
		echo not a valid GOPATH
		break
	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
		break
	else
		c=$(dirname $c)
	fi
done
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, http://talks.golang.org/2015/tricks.slide#51

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.