让你懒到逆天的 Bash 别名

file
Bash 别名是让你用易记的单词、缩写或者字符来引用命令的另一种方式。 例如,如果你使用 Git,你可能会在一天内运行很多次 git status,而为了节省时间和按键的次数,你可以将 git status 命名为 gs,它自动继承和调用正确的命令。

这些年我看到很多与众不同的的别名,每个人都是独一无二的。 也许你会对我用的快捷键感到迷惑,甚至会觉得不可思议的。「咦?有这种命令么?」 诸如此类的事情,而这可能正是别名好玩的地方。(我就喜欢看你一脸懵 B 再一副觉得好神奇的样子)

我请社区的人和我分享他们的别名配置,居然有不少的回应,更令我惊讶的是,几乎每个人都为 Artisan 命令做了快捷方式,还有几个是相似的。 然而每个人都还是有一个不同的快捷方式,例如 php artisan 命令的别名会有「a」、「pa」或「art」。 另外还有一些人为很巧合地为同一个命令赋予了同一个别名「nah」:

nah='git reset --hard;git clean -df'

话说回来,这个命令真的挺好用的。假设你开始在项目里研究一个新的功能(比如尝试使用别人开发的包),这个过程中你可能会添加一些新的文件,折腾过后你可能会发现这个功能并不怎么好用。然后你就可以通过运行「nah」命令来一口气删除这个过程中你所做的一切。

如何创建自己的别名

对于那些也想尝试创建 bash 别名的人来说,这个过程很简单。 首先,你要在文本编辑器中打开 〜/ .bashrc 文件。取消注释或添加以下的内容:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

编辑保存之后在你的终端执行:

source ~/.bashrc

这个命令让终端加载一个 .bash_aliases 文件,然后你可以把所有的别名放在这个文件里面,使它们更容易分享和追踪。现在,让我们来创建 〜/.bash_aliases 文件,并添加以下内容作为你的第一个别名:

alias art="php artisan"

编辑保存之后再在你的终端执行一次:

source ~/.bashrc

如此之后,你就可以输入「art」,它会调用 php artisan 命令。 另外,要记得每次修改 bash_aliases 文件之后,你都需要运行 source 命令或重新启动终端,让更改的内容得以生效。

来自 Laravel 社区的 Larave Bash 别名

以下列出了所有国外 Laravel 社区的贡献者以及他们正在使用的内容。

WaveHack

# Laravel

artisan() {
  if [ -f bin/artisan ]; then
    php bin/artisan "$@"
  else
    php artisan "$@"
  fi
}

alias serve='artisan serve'
alias tinker='artisan tinker'

# Misc PHP

t() {
  if [ -f vendor/bin/phpunit ]; then
    vendor/bin/phpunit "$@"
  else
    phpunit "$@"
  fi
}

bmadigan

nah='git reset --hard;git clean -df'
vm='ssh vagrant@127.0.0.1 -p 2222'

Tainmar

pa='php artisan'

Mohamed Said

alias dracarys="git reset --hard && git clean -df"
alias copyssh="pbcopy < $HOME/.ssh/id_rsa.pub"
alias reloadcli="source $HOME/.zshrc"
alias zshrc="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/.zshrc "
alias shrug="echo '¯\_(ツ)_/¯' | pbcopy";
alias fight="echo '(ง'̀-'́)ง' | pbcopy";

*** This one opens a PR from the current branch
function openpr() {
  br=`git branch | grep "*"`
  repo=$1
  parentBranch=$2

  open -a /Applications/Google\ Chrome.app  https://github.com/${repo/* /}/compare/${parentBranch/* /}...themsaid:${br/* /}\?expand\=1
}

Jeffrey Way

alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias wip="git add . && git commit -m 'wip'"
alias nah="git reset --hard && git clean -df"
alias p="phpunit"
alias pf="phpunit --filter "
alias art="php artisan"
alias migrate="php artisan migrate"

Bill Mitchell

alias a="php artisan"    
alias pu="vendor/bin/phpunit"
alias puf="vendor/bin/phpunit --filter "
alias pug="vendor/bin/phpunit --group "
alias cdo="composer dump-autoload -o"
alias serve="php artisan serve"

Jesús Amieiro

alias pa='php artisan'
alias par:l='php artisan route:list'
alias pam='php artisan migrate'
alias pam:r='php artisan migrate:refresh'
alias pam:rs='php artisan migrate:refresh --seed'
alias cu='composer update'
alias ci='composer install'
alias cda='composer dump-autoload -o'
alias vu='cd ~/Homestead && vagrant up'
alias vs='vagrant suspend'
alias vssh='vagrant ssh'

Piotr

alias artisan = "php artisan"
alias db-reset="php artisan migrate:reset && php artisan migrate --seed"

freekmurze

alias a="php artisan"

paulredmond

alias _='sudo'
alias art='php artisan'
alias tinker='php artisan tinker'
alias ll="ls -lh"
alias la='ls -lAh'
alias c='composer'
alias iphp='psysh' # repl
alias g='git'
alias gs='git status'
alias d='docker'
alias dc='docker-compose'
alias dm='docker-machine'
alias k='kubectl'
alias publicip='dig +short myip.opendns.com @resolver1.opendns.com'
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"

# Show file and folder permissions as octal
# Usage: `octal file.txt` or `octal my/path`
alias octal="stat -f '%A %a %N'"

# Mac conveniences for Linux
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
if type "xdg-open" &> /dev/null; then
    alias open="xdg-open"
fi

TJ Miller

nah: aliased to git reset --hard && git clean -fd
aa: aliased to php artisan

sebastiaanluca

# Hub (extend git commands)
alias git=hub

# Directories
alias ll='ls -FGlAhp'
alias ..="cd ../"
alias ...="cd ../../"
alias ....="cd ../../../"
alias .....="cd ../../../../"

alias df="df -h"
alias diskusage="df"
alias fu="du -ch"
alias folderusage="fu"
alias tfu="du -sh"
alias totalfolderusage="tfu"

alias finder='open -a 'Finder' .'

# Vagrant
alias vagrantgo="vagrant up && vagrant ssh"
alias vgo="vagrantgo"
alias vhalt="vagrant halt"
alias vreload="vagrant reload && vgo"

# PHP
alias c='composer'
alias cr='composer require'
alias cda='composer dumpautoload'
alias co='composer outdated --direct'
alias update-global-composer='cd ~/.composer && composer update'
alias composer-update-global='update-global-composer'

alias a='php artisan'
alias pa='php artisan'
alias phpa='php artisan'
alias art='php artisan'
alias arti='php artisan'

alias test='vendor/bin/phpunit'

alias y='yarn'
alias yr='yarn run'

# Homestead
alias edithomestead='open -a "Visual Studio Code" ~/Homestead/Homestead.yaml'
alias homesteadedit='edithomestead'
alias dev-homestead='cd ~/Homestead && vgo'
alias homestead-update='cd ~/Homestead && vagrant box update && git pull origin master'
alias update-homestead='homestead-update'

# Various
alias editaliases='open -a "Visual Studio Code" ~/.bash_aliases'
alias showpublickey='cat ~/.ssh/id_ed25519.pub'
alias ip="curl icanhazip.com"
alias localip="ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'"
alias copy='rsync -avv --stats --human-readable --itemize-changes --progress --partial'

# Functions
mkcdir ()
{
    mkdir -p -- "$1" &&
    cd -P -- "$1"
}

function homestead() {
    ( cd ~/Homestead && vagrant $* )
}

Alexander Melihov

alias ars="php artisan serve"
alias art="php artisan tinker"

jordonbaade

alias l="php artisan"

Deleu

alias unit='php vendor/phpunit/phpunit/phpunit'

alias unitreport='php -d xdebug.profiler_enable=On vendor/phpunit/phpunit/phpunit --coverage-html=./public/report'

alias laravel-installer='composer create-project --prefer-dist laravel/laravel'

curieuxmurray

alias artisan="php artisan"
alias cclear='php artisan cache:clear'
# now with 5.5
alias fresh="artisan migrate:fresh --seed"

wilburpowery

alias pf="phpunit --filter"
alias artisan="php artisan"
alias tinker="php artisan tinker"

waunakeesoccer1

alias mfs="php artisan migrate:fresh --seed'

说说看你们都有哪些懒到逆天的别名~

本文译自:https://laravel-news.com/bash-aliases

本作品采用《CC 协议》,转载必须注明作者和本文链接
Stay Hungry, Stay Foolish.
本帖由 Summer 于 6年前 加精
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 17
awesee

懒得去设置别名。另外用原生的更有利于学习和成长,通用性更好,要不然换台电脑就不知道命令怎么敲了。

6年前 评论

本来就有用别名的习惯,看完这篇文章我又增加了一波新别名,加上用了zsh 跟 autojump,感觉能力还没跟上大神,但装逼技能快到位了有木有!:satisfied:

6年前 评论

@Openset 嗯哈,天天在敲的命令,如果换了台电脑就不知道怎么敲那种,确实不建议使用别名~

6年前 评论

非常棒,大神才会用的,菜鸟还是乖乖使用全命令吧,哈哈

6年前 评论
awesee

@JokerLinly 好受伤啊:joy:

6年前 评论

表示还是习惯全称加自动补全功能。有助于加深记忆。

6年前 评论

@Openset :satisfied: 你会爱上别名的,因为就算不是为敲几个命令偷懒,你也可以用类似 alias subl='open -a "Sublime Text"' 这种别名很轻松快速地在 bash 里面打开 Sublime。这种体验是很愉悦的。我只是想说别名的用处不止于此。哈哈~

6年前 评论

经常直连服务器:

alias _serverName='ssh -p 端口号 -t 用户名@ip "cd /dir_path;bash"' 
# 可以登陆后直接进入某个目录'dir_path'

还有git的缩写,==我一直以为我用的是系统alias刚看了下原来是~/.gitconfig里面配的,也算吧

[alias]
        co = checkout
        st = status
        br = branch
        ci = commit
        last = log -l HEAD
        fa = fetch --all
        merge = merge --no-ff

用些alias用习惯了之后换电脑也必须先配上。。。不然真的很难受。。。

6年前 评论
awesee

@JokerLinly 谢谢你!一直有用,刚开始用的多,那时候也有虚荣心在作怪,现在用的少了,大多数重复的工作都用脚本处理了。现在完成整个项目很少敲命令了,从服务器到项目部署都是半自动化完成的。

6年前 评论

@Openset :pensive: 看来我才是渣渣

6年前 评论
awesee

@JokerLinly 像你这么优秀的程序媛,还妄自菲薄。:+1:

6年前 评论

看起来蛮丰富的别名,不过记忆的成本还是有的,zsh-autosuggestions自动提示也可以省一些事:thumbsup:

6年前 评论

却是厉害,mark

6年前 评论

编程字体好萌啊

6年前 评论
 这个真赞, 够全面。
6年前 评论

在命令行下用 Ctrl + R 搜索命令(例如:php artisan route:list, Ctrl + R 后输入 route)。不建议用别名。

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!