Skip to content

aliases/general & plugin/extract #2095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions aliases/available/directory.aliases.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# shellcheck shell=bash
about-alias 'Shortcuts for directory commands: ls, cd, &c.'

if command ls --color -d . &> /dev/null; then
alias ls='ls --color=auto'
# BSD `ls` doesn't need an argument (`-G`) when `$CLICOLOR` is set.
fi

# List directory contents
alias sl=ls
alias la='ls -AF' # Compact view, show hidden
alias ll='ls -Al'
alias l='ls -A'
alias l1='ls -1'
alias lf='ls -F'

# Change directory
alias ..='cd ..' # Go up one directory
alias cd..='cd ..' # Common misspelling for going up one directory
alias ...='cd ../..' # Go up two directories
alias ....='cd ../../..' # Go up three directories
alias -- -='cd -' # Go back

# Create or remove directory
alias md='mkdir -p'
alias rd='rmdir'
14 changes: 14 additions & 0 deletions aliases/available/editor.aliases.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# shellcheck shell=bash
about-alias 'shortcuts for editing'

alias edit='${EDITOR:-${ALTERNATE_EDITOR:-nano}}'
alias e='edit'

# sudo editors
alias svim='sudo ${VISUAL:-vim}'
alias snano='sudo ${ALTERNATE_EDITOR:-nano}'
alias sedit='sudo ${EDITOR:-${ALTERNATE_EDITOR:-nano}}'

# Shortcuts to edit startup files
alias vbrc='${VISUAL:-vim} ~/.bashrc'
alias vbpf='${VISUAL:-vim} ~/.bash_profile'
34 changes: 7 additions & 27 deletions aliases/available/general.aliases.bash
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
# shellcheck shell=bash
# shellcheck source-path=SCRIPTDIR
about-alias 'general aliases'

if command ls --color -d . &> /dev/null; then
alias ls='ls --color=auto'
# BSD `ls` doesn't need an argument (`-G`) when `$CLICOLOR` is set.
fi

# List directory contents
alias sl=ls
alias la='ls -AF' # Compact view, show hidden
alias ll='ls -al'
alias l='ls -a'
alias l1='ls -1'
alias lf='ls -F'

alias _='sudo'

# Shortcuts to edit startup files
alias vbrc='${VISUAL:-vim} ~/.bashrc'
alias vbpf='${VISUAL:-vim} ~/.bash_profile'

# colored grep
# Need to check an existing file for a pattern that will be found to ensure
# that the check works when on an OS that supports the color option
Expand All @@ -39,22 +23,17 @@ alias pager='${PAGER:=less}'

alias q='exit'

alias irc='${IRC_CLIENT:=irc}'
alias irc='${IRC_CLIENT:-irc}'

# Language aliases
alias rb='ruby'
alias py='python'
alias ipy='ipython'

# Pianobar can be found here: http://github.com/PromyLOPh/pianobar/

alias piano='pianobar'

alias ..='cd ..' # Go up one directory
alias cd..='cd ..' # Common misspelling for going up one directory
alias ...='cd ../..' # Go up two directories
alias ....='cd ../../..' # Go up three directories
alias -- -='cd -' # Go back
if _command_exists pianobar; then
alias piano='pianobar'
fi

# Shell History
alias h='history'
Expand Down Expand Up @@ -88,5 +67,6 @@ function catt() {
# aliases and enable just the ones for Bash-it explicitly:
# bash-it disable alias general
# bash-it enable alias bash-it
# shellcheck source-path=SCRIPTDIR
source "$BASH_IT/aliases/available/bash-it.aliases.bash"
source "$BASH_IT/aliases/available/directory.aliases.bash"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loading it here wont make it load it twice in case its enabled?
we can check if the alias is already enabled, and source only if its not

Copy link
Contributor Author

@gaelicWizard gaelicWizard Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one actually confused the heck out of me. If the user disabled it, we should not load it. If the user enabled it, we don't need to load it. How can we tell? No idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, I think that we should always check here if enabled, enable and source if not. If users dont want a certain alias, they should disable the general file. We can add a log here about it. I do not want to break backwards compat with this change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds right to me too.

source "$BASH_IT/aliases/available/editor.aliases.bash"
6 changes: 4 additions & 2 deletions aliases/available/vim.aliases.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# shellcheck shell=bash
about-alias 'vim abbreviations'

_command_exists vim || return
alias v='${VISUAL:-vim}'

alias v='vim'
if ! _command_exists vim; then
_log_warning "Without 'vim', these aliases just aren't that useful..."
fi
# open the vim help in fullscreen incorporated from
# https://stackoverflow.com/a/4687513
alias vimh='vim -c ":h | only"'
Expand Down
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ plugins/available/direnv.plugin.bash
plugins/available/dirs.plugin.bash
plugins/available/docker-compose.plugin.bash
plugins/available/docker-machine.plugin.bash
plugins/available/extract.plugin.bash
plugins/available/gif.plugin.bash
plugins/available/git-subrepo.plugin.bash
plugins/available/git.plugin.bash
Expand Down
133 changes: 72 additions & 61 deletions plugins/available/extract.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,73 +1,84 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'one command to extract them all...'

# extract file(s) from compressed status
extract() {
local opt
local OPTIND=1
while getopts "hv" opt; do
case "$opt" in
h)
cat <<End-Of-Usage
function extract() {
local opt OPTIND=1 verbose
local filename filedirname targetdirname
while getopts "hv" opt; do
case "$opt" in
h)
cat << EOU
Usage: ${FUNCNAME[0]} [option] <archives>
options:
-h show this message and exit
-v verbosely list files processed
End-Of-Usage
return
;;
v)
local -r verbose='v'
;;
?)
extract -h >&2
return 1
;;
esac
done
shift $((OPTIND-1))
EOU
return
;;
v)
local -r verbose='v'
;;
?)
extract -h >&2
return 1
;;
esac
done
shift $((OPTIND - 1))

[ $# -eq 0 ] && extract -h && return 1
while [ $# -gt 0 ]; do
if [[ ! -f "$1" ]]; then
echo "extract: '$1' is not a valid file" >&2
shift
continue
fi
if [[ $# -eq 0 ]]; then
extract -h
return 1
fi

local -r filename=$(basename -- $1)
local -r filedirname=$(dirname -- $1)
local targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< $filename)
if [ "$filename" = "$targetdirname" ]; then
# archive type either not supported or it doesn't need dir creation
targetdirname=""
else
mkdir -v "$filedirname/$targetdirname"
fi
while [[ $# -gt 0 ]]; do
if [[ ! -f "${1:-}" ]]; then
echo "extract: '$1' is not a valid file" >&2
shift
continue
fi

if [ -f "$1" ]; then
case "$1" in
*.tar.bz2|*.tbz|*.tbz2) tar "x${verbose}jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.gz|*.tgz) tar "x${verbose}zf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.xz|*.txz) tar "x${verbose}Jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.Z) tar "x${verbose}Zf" "$1" -C "$filedirname/$targetdirname" ;;
*.bz2) bunzip2 "$1" ;;
*.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;;
*.pax.gz) gunzip "$1"; set -- "$@" "${1:0:-3}" ;;
*.gz) gunzip "$1" ;;
*.pax) pax -r -f "$1" ;;
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;;
*.rar) unrar x "$1" ;;
*.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;;
*.tar) tar "x${verbose}f" "$1" -C "$filedirname/$targetdirname" ;;
*.xz) xz --decompress "$1" ;;
*.zip|*.war|*.jar|*.nupkg) unzip "$1" -d "$filedirname/$targetdirname" ;;
*.Z) uncompress "$1" ;;
*.7z) 7za x -o"$filedirname/$targetdirname" "$1" ;;
*) echo "'$1' cannot be extracted via extract" >&2;;
esac
fi
local -r filename=$(basename -- "$1")
local -r filedirname=$(dirname -- "$1")
# shellcheck disable=SC2001 # we don't depend on `extglob`...
targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< "$filename")
if [[ "$filename" == "$targetdirname" ]]; then
# archive type either not supported or it doesn't need dir creation
targetdirname=""
else
mkdir -v "$filedirname/$targetdirname"
fi

shift
done
if [[ -f "$1" ]]; then
case "$1" in
*.tar.bz2 | *.tbz | *.tbz2) tar "x${verbose}jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.gz | *.tgz) tar "x${verbose}zf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.xz | *.txz) tar "x${verbose}Jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.Z) tar "x${verbose}Zf" "$1" -C "$filedirname/$targetdirname" ;;
*.bz2) bunzip2 "$1" ;;
*.deb) dpkg-deb -x"${verbose}" "$1" "${1:0:-4}" ;;
*.pax.gz)
gunzip "$1"
set -- "$@" "${1:0:-3}"
;;
*.gz) gunzip "$1" ;;
*.pax) pax -r -f "$1" ;;
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;;
*.rar) unrar x "$1" ;;
*.rpm) rpm2cpio "$1" | cpio -idm"${verbose}" ;;
*.tar) tar "x${verbose}f" "$1" -C "$filedirname/$targetdirname" ;;
*.xz) xz --decompress "$1" ;;
*.zip | *.war | *.jar | *.nupkg) unzip "$1" -d "$filedirname/$targetdirname" ;;
*.Z) uncompress "$1" ;;
*.7z) 7za x -o"$filedirname/$targetdirname" "$1" ;;
*) echo "'$1' cannot be extracted via extract" >&2 ;;
esac
fi

shift
done
}

# Shorten extract
alias xt='extract'
3 changes: 3 additions & 0 deletions profiles/default.bash_it
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ completion system

# aliases
aliases general
aliases bash-it
aliases directory
aliases editor
Loading