Protip: don't create aliases that override common commands (e.g. alias cp='cp -r'). Months down the road, you'll try to copy/paste some long piped command, and be confused as to why it works on everyone else's machine but not yours
Instead, create alternative commands, like alias lsl='ls -lah', and put in the work to make it the default thing you type.
uhh, hmm. For thirty years or so I've lived by these two (shell immaterial):
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.
> Alias: rm
> Command: rm -r
Purpose: Because when I type "rm" I don't want to always have to specify "-r" for a directory.
Alias: cp
Command: cp -r
Purpose: Same as above. When I say "copy this" I always want it to copy whatever I'm specifying, even if it's a directory.
Do this in important directories for added safety:
$ touch -- - i
This creates a file called "-i" which will add the interactive flag, prompting you before deleting every file that rm was going to delete.
Agree. (I actually feel the same way about aliasing rm to “rm -i“).
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 ../../../../../..'
I think it's a terrible idea, because one day you'll try to enter a directory that shares its name with a command you don't know, and it will execute this command instead
That seems potentially unpredictable versus building the string and calling cd once. For example, if you use direnv-type tools there may be side-effects along the route¹. zsh users could have state changing via the chpwd functions too, although that could be mitigated with "cd -q" at least.
Obviously, 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
There are few more general solutions to this interaction pattern(that also don't involve redundant history entries if you're OCD).
* 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.
Helpfully modern ssh, at least 7.x IIRC, can handle Include statements in the main config. You need to put includes before any host configs you have in the main config file lest it think the include is part of a host's configuration. But this makes managing cloud host configs for instance much easier.
I have aliases for zsh to quickly edit all my configs, it's pretty neat :
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
I use a variation of this to enable my conda environments - I always name the conda env the same as the top level git folder and can then quickly enable my env with an alias
I have almost the exact same but with "--restrict-filename" lest you end up with emojis and other Unicode characters you do t necessarily want on your file system.
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.
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.
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.
Deleted Comment
You can do just `cdb 5` to navigate 5 directories back.
Obviously, 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*
Deleted Comment
# -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.