Skip to content

Instantly share code, notes, and snippets.

@YodasWs
Last active January 22, 2019 00:26
Show Gist options
  • Save YodasWs/b4f5932c0ca8a9d5f2e31a6524704718 to your computer and use it in GitHub Desktop.
Save YodasWs/b4f5932c0ca8a9d5f2e31a6524704718 to your computer and use it in GitHub Desktop.
Simplifying my work in the terminal
# Display useful information in command line prompt
export PS1="\n\d \t\n\n\w !\! >"
# Files with further utilities
files=(
# Auto-complete commands
~/git-completion.sh
~/gulp-completion.sh
)
for i in "${!files[@]}"; do
[ -s "${files[$i]}" ] && source "${files[$i]}"
done
# Edit this file and load it
unset -f customize
function customize {
if [ -z "$1" ]; then
vim ~/.bashrc
source ~/.bashrc
else
case $1 in
# Or instead edit .vimrc
vim)
vim ~/.vimrc
;;
esac
fi
}
complete -W "vim" customize
# Show directory contents when moving
unset -f goto
function goto {
local -a _ls
local -a dir
echo
if [ $# -gt 0 ]; then
for a in "$*"; do
case $a in
git)
dir_=("~/GitHub/")
;;
-a)
_ls+=("-A")
;;
-*)
_ls+=("$a")
;;
*)
dir+=("$a");
;;
esac
done
\cd "$(IFS=/; echo "${dir[*]}")"
fi
echo -e "$(tput setaf 5)$(pwd)$(tput sgr0)\n"
ls $_ls -G
}
# Check directory contents quickly and easily
unset -f .
function . {
local dir=`pwd`
goto "$@"
\cd "$dir"
}
# Move up a directory
unset -f ..
function .. {
\cd ..
args=""
while (( $# )); do
if [ -d "$1" ]; then
\cd "$1"
else
args="$args \"$1\""
fi
shift
done
. $args
}
# Simplify common commands
alias rm="rm -iv"
alias cp="cp -i -v"
unalias cd || clear;
alias cd=goto
alias c=clear
alias x=exit
alias grep=egrep
# Keep history clean
export HISTCONTROL=ignorespace:ignoredups
export HISTIGNORE='customize*:c:.:. :ls:ls :exit*:x'
#!bash
#
# bash/zsh completion support for Gulp.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
#
# Copyright © 2016–2017 Samuel B Grundman <[email protected]>
# Released under Creative Commons BY-NC-SA 4.0 License
# (https://creativecommons.org/licenses/by-nc-sa/4.0/)
#
# The contained completion routines provide support for completing:
#
# *) gulp 'subcommands'
# *) common --long-options
#
# To use these routines:
#
# 1) Copy this file to somewhere (e.g. ~/.gulp-completion.sh).
# 2) Add the following line to your .bashrc/.zshrc:
# source ~/.gulp-completion.sh
if [[ -n ${ZSH_VERSION-} ]]; then
autoload -U +X bashcompinit && bashcompinit
fi
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac
if [ -z "$(type -t __comp_1)" ] || [ "$(type -t __comp_1)" != "function" ]; then
__comp_1 ()
{
local c IFS=$' \t\n'
for c in $1; do
c="$c$2"
case $c in
--*=*|*.) ;;
*) c="$c " ;;
esac
printf '%s\n' "$c"
done
}
fi
# The following function is based on code from:
#
# bash_completion - programmable completion functions for bash 3.2+
#
# Copyright © 2006-2008, Ian Macdonald <[email protected]>
# © 2009-2010, Bash Completion Maintainers
# <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# The latest version of this software can be obtained here:
#
# http://bash-completion.alioth.debian.org/
#
# RELEASE: 2.x
# This function can be used to access a tokenized list of words
# on the command line:
#
# __gulp_reassemble_comp_words_by_ref '=:'
# if test "${words_[cword_-1]}" = -w
# then
# ...
# fi
#
# The argument should be a collection of characters from the list of
# word completion separators (COMP_WORDBREAKS) to treat as ordinary
# characters.
#
# This is roughly equivalent to going back in time and setting
# COMP_WORDBREAKS to exclude those characters. The intent is to
# make option types like --date=<type> and <rev>:<path> easy to
# recognize by treating each shell word as a single token.
#
# It is best not to set COMP_WORDBREAKS directly because the value is
# shared with other completion scripts. By the time the completion
# function gets called, COMP_WORDS has already been populated so local
# changes to COMP_WORDBREAKS have no effect.
#
# Output: words_, cword_, cur_.
__gulp_reassemble_comp_words_by_ref()
{
local exclude i j first
# Which word separators to exclude?
exclude="${1//[^$COMP_WORDBREAKS]}"
cword_=$COMP_CWORD
if [ -z "$exclude" ]; then
words_=("${COMP_WORDS[@]}")
return
fi
# List of word completion separators has shrunk;
# re-assemble words to complete.
for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
# Append each nonempty word consisting of just
# word separator characters to the current word.
first=t
while
[ $i -gt 0 ] &&
[ -n "${COMP_WORDS[$i]}" ] &&
# word consists of excluded word separators
[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
do
# Attach to the previous token,
# unless the previous token is the command name.
if [ $j -ge 2 ] && [ -n "$first" ]; then
((j--))
fi
first=
words_[$j]=${words_[j]}${COMP_WORDS[i]}
if [ $i = $COMP_CWORD ]; then
cword_=$j
fi
if (($i < ${#COMP_WORDS[@]} - 1)); then
((i++))
else
# Done.
return
fi
done
words_[$j]=${words_[j]}${COMP_WORDS[i]}
if [ $i = $COMP_CWORD ]; then
cword_=$j
fi
done
}
if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
if [[ -z ${ZSH_VERSION:+set} ]]; then
_get_comp_words_by_ref ()
{
local exclude cur_ words_ cword_
if [ "$1" = "-n" ]; then
exclude=$2
shift 2
fi
__gulp_reassemble_comp_words_by_ref "$exclude"
cur_=${words_[cword_]}
while [ $# -gt 0 ]; do
case "$1" in
cur)
cur=$cur_
;;
prev)
prev=${words_[$cword_-1]}
;;
words)
words=("${words_[@]}")
;;
cword)
cword=$cword_
;;
esac
shift
done
}
else
_get_comp_words_by_ref ()
{
while [ $# -gt 0 ]; do
case "$1" in
cur)
cur=${COMP_WORDS[COMP_CWORD]}
;;
prev)
prev=${COMP_WORDS[COMP_CWORD-1]}
;;
words)
words=("${COMP_WORDS[@]}")
;;
cword)
cword=$COMP_CWORD
;;
-n)
# assume COMP_WORDBREAKS is already set sanely
shift
;;
esac
shift
done
}
fi
fi
# Generates completion reply with compgen, appending a space to possible
# completion words, if necessary.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word (optional).
if [ -z "$(type -t __comp)" ] || [ "$(type -t __comp)" != "function" ]; then
__comp ()
{
local cur_="${3-$cur}"
case "$cur_" in
--*=)
COMPREPLY=()
;;
*)
local c i=0 IFS=$' \t\n'
for c in $1; do
c="$c${4-}"
if [[ $c == "$cur_"* ]]; then
case $c in
--*=*|*.) ;;
*) c="$c " ;;
esac
COMPREPLY[i++]="${2-}$c"
fi
done
;;
esac
}
fi
# Generates completion reply with compgen from newline-separated possible
# completion words by appending a space to all of them.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words, separated by a single newline.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word instead of
# the default space (optional). If specified but empty, nothing is
# appended.
if [ -z "$(type -t __nl)" ] || [ "$(type -t __nl)" != "function" ]; then
__nl ()
{
local IFS=$'\n'
COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
}
fi
__gulp ()
{
for i in $(gulp -? ${1-})
do
echo $(echo $i | egrep '\-\-[a-zA-Z0-9-]+' -o)
done
return
}
__gulp_list_all_commands ()
{
local i IFS=" "$'\n'
for i in $(gulp -h)
do
echo $(echo $i | egrep '\-\-[a-zA-Z0-9-]+' -o)
done
for i in $(gulp -?)
do
echo $(echo $i | egrep '\-\-[a-zA-Z0-9-]+' -o)
done
if [ -e "gulpfile.js" ]; then
echo $(ack --output='$1' "\bgulp\.task\(['\"]([a-zA-Z0-9\-_:]+)['\"]" gulpfile.js)
fi
if [ -d "gulp-tasks" ]; then
for i in $(ls gulp-tasks/*.js)
do
echo $(ack --output='$1' "\bgulp\.task\(['\"]([a-zA-Z0-9\-_:]+)['\"]" $i)
done
fi
}
__gulp_all_commands=
__gulp_compute_all_commands ()
{
test -n "$__gulp_all_commands" ||
__gulp_all_commands=$(__gulp_list_all_commands)
}
__gulp_list_porcelain_options ()
{
local i IFS=" "$'\n'
__gulp_compute_all_commands
for i in $__gulp_all_commands
do
case $i in
# List only global options
--*) echo $i;;
esac
done
}
__gulp_list_porcelain_commands ()
{
local i IFS=" "$'\n'
__gulp_compute_all_commands
for i in $__gulp_all_commands
do
case $i in
# List anything here we don't want to normally list
--*) : option ;;
*) echo $i;;
esac
done
}
__gulp_porcelain_commands=
__gulp_compute_porcelain_commands ()
{
__gulp_compute_all_commands
test -n "$__gulp_porcelain_options" ||
__gulp_porcelain_options=$(__gulp_list_porcelain_options)
test -n "$__gulp_porcelain_commands" ||
__gulp_porcelain_commands=$(__gulp_list_porcelain_commands)
}
## Gulp Command Expansions
_gulp_minifyfile ()
{
if [[ "$cur" =~ "--" ]] || [ -z "$prev" ] || [ "$prev" == "$command" ]; then
__comp "--file"
fi
}
_gulp_localize ()
{
if [[ "$cur" =~ "--" ]] || [ -z "$prev" ] || [ "$prev" == "$command" ]; then
__comp "--path --env --verbose"
elif [ "$prev" == "--path" ]; then
return
fi
}
## Main Gulp Expansion
__gulp_main ()
{
local i c=1 command
COMP_WORDBREAKS="${COMP_WORDBREAKS//:}"
while [ $c -lt $cword ]; do
i="${words[c]}"
case "$i" in
--help) command="help"; break ;;
-c) c=$((++c)) ;;
-*) ;;
*) command="$i"; break ;;
esac
((c++))
done
if [ -n "$prev" ] && [ "$prev" != "gulp" ]; then
case "$prev" in
-n|--name)
local dir="addon/components" components
if [ -d "$dir" ] && [ -x "$dir" ]; then
components=$(\ls $dir)
__comp "${components//.js/}"
return
else
__comp "no-op transactions-table"
fi
return ;;
-?)
__gulp_compute_porcelain_commands
__comp "$__gulp_porcelain_commands"
return ;;
*)
__comp $(__gulp $prev)
return ;;
esac
fi
if [ -z "$command" ]; then
case "$cur" in
*)
__gulp_compute_porcelain_commands
__comp "$__gulp_porcelain_commands $__gulp_porcelain_options"
;;
esac
return
fi
local completion_func="_gulp_${command//-/_}"
declare -f $completion_func >/dev/null && $completion_func && return
}
__gulp_func_wrap ()
{
if [[ -n ${ZSH_VERSION-} ]]; then
emulate -L bash
setopt KSH_TYPESET
# workaround zsh's bug that leaves 'words' as a special
# variable in versions < 4.3.12
typeset -h words
# workaround zsh's bug that quotes spaces in the COMPREPLY
# array if IFS doesn't contain spaces.
typeset -h IFS
fi
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
$1
}
# Setup completion for certain functions defined above by setting common
# variables and workarounds.
# This is NOT a public function; use at your own risk.
__gulp_complete ()
{
local wrapper="__gulp_wrap${2}"
eval "$wrapper () { __gulp_func_wrap $2 ; }"
complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
|| complete -o default -o nospace -F $wrapper $1
}
__gulp_complete gulp __gulp_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment