Readit News logoReadit News
superb-owl · 2 years ago
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.

_def · 2 years ago
I can't stress this enough. It will also kinda poison your intuition.
mixmastamyk · 2 years ago
Don't paste long piped commands unless you know exactly what they do.
downut · 2 years ago
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.

adr1an · 2 years ago
You can also do backslash to skip the aliases, e.g. \rm wouldn't ask for confirmation (skip the -i)
gdevenyi · 2 years ago
> 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.

This is insanely dangerous. Don't do this.

cassepipe · 2 years ago
A way better solution is to alias trash-d as a drop-in replacement for `rm` : https://github.com/rushsteve1/trash-d
ttyprintk · 2 years ago
Also, ‘gio trash’ is widespread.
deafpolygon · 2 years ago
Agreed. I like that you need to use '-r' ... it's that extra layer of safety and intention. A whole directory gone is a bit hard to recover from.
metal · 2 years ago
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.
Galanwe · 2 years ago
That seems flaky at best. Basically you rely on "*" expansion to inject the -i.

While that may work for `rm -rf *`, it won't for `rm -rf foo/*`, `rm -rf foo`, etc

Groxx · 2 years ago
Or for far better reliability, just use safety and add that to the config.
mturmon · 2 years ago
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.

ForkMeOnTinder · 2 years ago

  alias ..='cd ..'
  alias ...='cd ../..'
  alias ....='cd ../../..'
  alias .....='cd ../../../..'
  alias ......='cd ../../../../..'
  alias .......='cd ../../../../../..'
Merry christmas, HN!

jbaber · 2 years ago
I used to do things like this until I found bash has

shopt -s autocd

which makes it so naming a directory cd's to it, including .. ~ etc.

Biganon · 2 years ago
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
pokler · 2 years ago
up() { cd $(eval printf '../'%.0s {1..$1}); }

So you can just type something like `up 2` to move by two directories.

fsckboy · 2 years ago
eval printf? um.... is your name little bobby tables by any chance?

Deleted Comment

jafarlihi · 2 years ago
I devised `cdb` for this: https://github.com/jafarlihi/dotfiles/blob/a5133f49d67732199...

You can do just `cdb 5` to navigate 5 directories back.

POiNTx · 2 years ago
Not bash but if you're using fish you can use https://github.com/nickeb96/puffer-fish
ibiza · 2 years ago
I did not realize cd'ing up was an esoteric sport. My own take:

  shopt -s cdable_vars
  u2=../.. u3=../../.. u4=../../../.. u5=../../../../..

adr1an · 2 years ago
I use numbers to avoid counting how many dots I am writing, just use 1. 2. 3. 4. to call these cd 1, 2, 3, or 4 levels up.
dw_arthur · 2 years ago
my version of ..

  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 on

JNRowe · 2 years ago
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*

Deleted Comment

euroderf · 2 years ago
avoid disasters!

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

smitty1e · 2 years ago
I always like to do 'ls <pattern>' to see what I'm blowing away, then CTRL-a CTRL-D x2 rm

Usually I know that I'm doing.

Also like 'rm -fv' because feedback is good.

JNRowe · 2 years ago
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.

Galanwe · 2 years ago
I have the same habit. I also use a somewhat similar "dry run" trick in `find` calls, such as:

    $ find foo/ -type f -name 'bar*' -exec echo rm {} \;

gnoack · 2 years ago
Bash aliases for specific ssh hosts are seldomly needed, you can just as well configure hostname aliases in your .ssh/config file
giantrobot · 2 years ago
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.
cassepipe · 2 years ago
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

yanzhang0219 · 2 years ago
fzf is the final solution.
moritzwarhier · 2 years ago
I like my alias

  alias cd-gitroot=cd "$(git rev-parse --show-toplevel)"

jerpint · 2 years ago
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
vondur · 2 years ago
Here's one I use pretty often: alias yt-dl="yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4'"
giantrobot · 2 years ago
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.