[HN Gopher] Some Useful Bash Aliases (2022)
       ___________________________________________________________________
        
       Some Useful Bash Aliases (2022)
        
       Author : todsacerdoti
       Score  : 49 points
       Date   : 2023-12-25 09:25 UTC (13 hours ago)
        
 (HTM) web link (chuck.is)
 (TXT) w3m dump (chuck.is)
        
       | gdevenyi wrote:
       | > 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.
        
         | deafpolygon wrote:
         | 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 wrote:
         | 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 wrote:
           | 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
        
             | metal wrote:
             | That's the point. You're not relying on it, it's a safety
             | to save yourself from an unintended glob expansion.
        
           | Groxx wrote:
           | Or for far better reliability, just use safety and add that
           | to the config.
        
       | moritzwarhier wrote:
       | I like my alias                 alias cd-gitroot=cd "$(git rev-
       | parse --show-toplevel)"
        
         | jerpint wrote:
         | 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
        
       | jerpint wrote:
       | Don't forget a bunch of useful git aliases
       | 
       | alias gcm=git commit -m
       | 
       | alias gaugcm = git add -u && gcm
        
         | dfinninger wrote:
         | My favorite:
         | 
         | alias gfp="git commit --amend --no-edit && git push --force-
         | with-lease"
         | 
         | For those "whoops, for got to add $x file" moments or typos
        
           | ithkuil wrote:
           | I have basically that set up as gitconfig alias "git amen"
        
       | account-5 wrote:
       | I love this sort of thing I always like to read about people's
       | terminal usage, and the terminal programs.
        
         | walth wrote:
         | These setups are intensely personal though and very limited
         | applicability to anyone else.
         | 
         | 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.
        
       | euroderf wrote:
       | 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 wrote:
         | 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.
        
           | Galanwe wrote:
           | 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 {} \;
        
             | smitty1e wrote:
             | Yes, but I usually need to look up the -exec clause,
             | because `{} \;` just feels hinky.
        
       | ForkMeOnTinder wrote:
       | alias ..='cd ..'       alias ...='cd ../..'       alias ....='cd
       | ../../..'       alias .....='cd ../../../..'       alias
       | ......='cd ../../../../..'       alias .......='cd
       | ../../../../../..'
       | 
       | Merry christmas, HN!
        
         | jafarlihi wrote:
         | I devised `cdb` for this:
         | https://github.com/jafarlihi/dotfiles/blob/a5133f49d67732199...
         | 
         | You can do just `cdb 5` to navigate 5 directories back.
        
         | pokler wrote:
         | up() { cd $(eval printf '../'%.0s {1..$1}); }
         | 
         | So you can just type something like `up 2` to move by two
         | directories.
        
         | ibiza wrote:
         | I did not realize cd'ing up was an esoteric sport. My own take:
         | shopt -s cdable_vars       u2=../.. u3=../../.. u4=../../../..
         | u5=../../../../..
        
         | jbaber wrote:
         | 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.
        
       | woodruffw wrote:
       | I almost regret this one because of how often I use it on systems
       | that I haven't set up yet:                   alias +x='chmod +x'
        
       | gnoack wrote:
       | Bash aliases for specific ssh hosts are seldomly needed, you can
       | just as well configure hostname aliases in your .ssh/config file
        
       | vondur wrote:
       | Here's one I use pretty often: alias yt-dl="yt-dlp -f
       | 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4'"
        
       | superb-owl wrote:
       | 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 wrote:
         | I can't stress this enough. It will also kinda poison your
         | intuition.
        
         | downut wrote:
         | 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.
        
       | jiehong wrote:
       | I have a bunch of directories I always go to, and instead of cd-
       | ing on a relative path from where I am, I prefer using an
       | interactive way to select where to go (I've aliased this script
       | as "c"):
       | 
       | d=$(ls ~/Dev | fzf)
       | 
       | if [ -z $d ]; then return 0
       | 
       | cd "~/Dev/$d"
        
         | mrbombastic wrote:
         | Not quite the same but you should check out autojump if you
         | haven't before: https://github.com/wting/autojump
        
         | beeburrt wrote:
         | Why not just customize $CDPATH
        
       | pokler wrote:
       | alias please="sudo"
       | 
       | alias amend="git commit --amend --no-edit"
       | 
       | alias vim="nvim"
        
       ___________________________________________________________________
       (page generated 2023-12-25 23:00 UTC)