Some Useful Bash Aliases (2022)
chuck.is
chuck.is
Instead, create alternative commands, like alias lsl='ls -lah', and put in the work to make it the default thing you type.
mv='mv -i' rm='rm -i'
simple enough to add /bin/ to the command if I really mean it. That fixes the pipe issue too. I've had new to linux new hires go 'uh well I shoulda done that thanks I've done it now' more than once.
This is insanely dangerous. Don't do this.
gio is a utility that makes many of the GIO features available
Ok, thanks I guess :DOn the other hand, KDE has a syntax that deserves an alias.
While that may work for `rm -rf *`, it won't for `rm -rf foo/*`, `rm -rf foo`, etc
I depend on the out of box behavior of rm. I’ll run “rm *” in a directory that has subdirectories I want to preserve, but where I want to erase the files.
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias .......='cd ../../../../../..'
Merry christmas, HN!shopt -s autocd
which makes it so naming a directory cd's to it, including .. ~ etc.
So you can just type something like `up 2` to move by two directories.
You can do just `cdb 5` to navigate 5 directories back.
shopt -s cdable_vars
u2=../.. u3=../../.. u4=../../../.. u5=../../../../.. function ..() {
local temp_dir=$PWD
cd ..
for token in "$@"; do
cd ..
done
OLDPWD=$temp_dir
}
so you can just type .. to go up once or .. [word] to go up twice and so onObviously, it might not be an issue for your use, but just noting a potential gotcha if I were to lift it for example.
[ Golf-y zsh, because vacation time and AoC has finished: ..() { cd ../${(pj:/:)${@/*/..}} } ]
¹ even just expensive setup/teardown delays would feel odd*
# -i: Cause mv to write a prompt to standard error before moving a file that would overwrite an existing file. If the response from stdin begins with 'y’ or ‘Y’, the move is attempted. (-i overrides any previous -f or -n)
alias mv='mv -i'
# -I: Request confirmation once if more than three files are being removed or if a directory is being recursively removed. This is a far less intrusive option than -i
alias rm='rm -I'
Usually I know that I'm doing.
Also like 'rm -fv' because feedback is good.
* bash has a glob-expand-word function which will perform inplace expansion of patterns, it is bound to C-x* out of the box. Using it will expand the pattern before point. Depending on the keymap you're using it is available via the same binding or the expand-word function in zsh too.
* C-xg (the glob-list-expansions function) will simply dump the expansion but retain the pattern in the command, if you'd prefer it didn't get added to history fully expanded for example. Again depending on keymap in zsh it is bound to the same key and calls the list-expand function.
* You can recall combinations of arguments from any line in history, so in your example 'rm !*' would recall all the args to previous command. The main reason this is useful is that you can also apply substitutions to fix incorrect results, and also because you can target other lines not just the previous one. You can even tack a modifier on to print the result without executing to double-double-check things, should you be feeling super careful(rm !*:p). zsh supports the same syntax, but has advanced more advanced modification options.
$ find foo/ -type f -name 'bar*' -exec echo rm {} \;¹ https://git.savannah.gnu.org/cgit/findutils.git/tree/NEWS#n2...
declare -x -A configs
configs=(
gdb "$XDG_CONFIG_HOME/gdb/gdbinit"
git "$XDG_CONFIG_HOME/git/config"
vim "$HOME/.vimrc"
wezterm "$XDG_CONFIG_HOME/wezterm"
zsh "$HOME/.zshrc"
)
for key value in ${(kv)configs}; do
if [[ $key == "zsh" ]]
then
alias ${key}config="$EDITOR $value && source $value && echo
$configs[zsh] has been sourced"
else
alias ${key}config="$EDITOR $value"
fi
done
Just use zshconfig now if you need to add a new config file to the array and get a new alias alias cd-gitroot=cd "$(git rev-parse --show-toplevel)" # f# like |> pipelining, example `echo "~/.bashrc" |: vim _`
# sed here is removing the last newline of the output
alias ::='sed "$ s/\n$//" | xargs -I_ --'
Usage example, where `_` is a marker `::` (xargs) uses to swap in the pipe'd value sudo docker ps -a | skip 1 | col 1 |:: sudo docker rm _
skip, col are other aliases/functions, function col {
eval "awk '{ print \$$1 }'"
}
function skip {
tail -n +$(($1 + 1))
}
function take {
head -n $1
}I also quite like,
function mkcd {
command mkdir -p "$1" && \
command cd "$1" && \
pwd
}
I'm thinking of writing one for git clone, `git clonecd` or something, to cd into a cloned directory. Note that having an executable named `git-foo` in your $PATH lets you call it like a git subcommand, `git foo`Use this instead : https://github.com/rushsteve1/trash-d
You can also accomplish this by adding `chuck` to your `~/.ssh/config`. IMO it's better to do it that way because you don't break tab completion. Indeed, if your shell is setup for it you should be able to tab complete any entry in your SSH config.
alias +x='chmod +x'I find myself using `!chmod +x` often from inside vim, so this will be quite nice, `!+x`
Edit: Sadly it doesn't work, trying to run `!+x %` in vim makes bash (my shell) treat `+x` like an option I believe.
Small problem: when I open a project in vscode, the terminal just opens already at the folder and doesn't trigger a cd.
So now I do `cd .` a lot and I don't remember off the top of my head how to activate a python venv anymore.
d=$(ls ~/Dev | fzf)
if [ -z $d ]; then return 0
cd "~/Dev/$d"
And this particular example is dangerous and full of footguns. Cp, rm dangerous by default.
SSH can be just `ssh c` or `ssh chuckbox` with a proper ssh config.
alias gcm=git commit -m
alias gaugcm = git add -u && gcm
alias gfp=“git commit --amend --no-edit && git push --force-with-lease”
For those “whoops, for got to add $x file” moments or typos
alias ga="git add"
alias gaw="git add -A && git diff --cached -w | git apply --cached -R"
alias gb="git branch"
alias gbl="git branch -l"
alias gbD="git branch -D"
alias gbu="git branch -u"
alias gc="git commit"
alias gca="git commit -a"
alias gcaa="git commit -a --amend"
alias gcam="git commit -a -m"
alias gce="git commit -e"
alias gcfu="git commit --fixup"
alias gcm="git commit -m"
alias gco="git checkout"
alias gcob="git checkout -b"
alias gcoB="git checkout -B"
alias gcp="git cherry-pick"
alias gcpc="git cherry-pick --continue"
alias gd="git diff"
alias gd^="git diff HEAD^ HEAD"
alias gds="git diff --staged"
alias gl="git lg" # TODO - make these options defaults for "git log" itself
alias glg="git log --graph --decorate --all" # TODO - make these options defaults for "git log" itself
alias gdc="git diff --cached"
alias gpom="git push origin master"
alias gr="git remote"
alias gra="git rebase --abort"
alias grb="git rebase --committer-date-is-author-date"
alias grbom="grb --onto master"
alias grbasi="git rebase --autosquash --interactive"
alias grc="git rebase --continue"
alias grs="git restore --staged"
alias grv="git remote -v"
alias grh="git reset --hard"
alias grH="git reset HEAD"
alias grH^="git reset HEAD^"
alias gs="git status -sb"
alias gsd="git stash drop"
alias gsl="git stash list --date=relative"
alias gsp="git stash pop"
alias gss="git stash show"
alias gst="git status"
alias gsu="git standup"
alias gforgotrecursive="git submodule update --init --recursive --remote"
# No arguments: `git status`
# With arguments: acts like `git`
function g() {
if [[ $# > 0 ]]; then
git $@
else
git st
fi
}
# Complete g like git
compdef g=git # Complete g like git
compdef g=git
That's brilliant.It's actually a oh-my-zsh plugin, called "magic-enter".
I defined MAGIC_ENTER_GIT_COMMAND="git status" and then included the plugin.
You can find a different but similar plugin here: https://github.com/zshzoo/magic-enter/blob/main/magic-enter....
And git lets you define custom subcommand if you just create a file like `git-foo` in your $PATH, and then you can run it like `git foo`
alias pst='ps -ef --sort=start_time'
alias rmgit='rm -rf ..?* .[!.]* *'
alias amend="git commit --amend --no-edit"
alias vim="nvim"
curry='xargs -I{}' sudo docker ps -a | skip 1 | col 1 |:: sudo docker rm _
Makes it look more like a functional language pipeline