V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wuhuaji
V2EX  ›  问与答

你给 Shell 配置了哪些 alias?

  •  
  •   wuhuaji · 2019-09-04 23:14:11 +08:00 · 4048 次点击
    这是一个创建于 1667 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚写的一篇博文,记录我积累的一些 alias,自觉日积月累还是能省下不少时间。

    分享到这里,我定义的部分通用 alias :

    alias pg='ps aux | grep $1'
    //查找进程,通过 ps aux | grep xxx,来查找进程应该是个非常高频的操作,自定义了一个`pg`来代替,非常省事。eg: pg nginx
    
    alias untar='tar -zxvf' #解压 tar
    
    alias msq='mysql -uroot -pPASSPWORD'
    #本地有一个 MySQL 测试用,而命令行中是进 MySQL 也是比较高频的操作,直接配一个 msq 操作
    
    alias sshxxx='ssh [email protected]'
    #登录远程机器,通过设置别名,拷贝密钥之后登录机器就是敲几个字的事
    
    alias mem="telnet 127.0.0.1 11211"
    #公司在用 memcached,配置 mem 直接连接 memcached
    
    alias python='python3'
    #避免每次写敲 python 运行的是 Python 2
    
    # git 的一些快捷操作
    alias gs='git status'
    alias ga='git add'
    alias gc='git commit'
    alias gp='git push'
    alias gpo='git push origin master'
    alias gpd='git push origin dev'
    alias gpu='git pull upstream master'
    
    #前端开发
    alias nrs='npm run serve'
    alias nrb='npm run build'
    
    # 也写一点 laravel
    alias art="php artisan"
    alias phpunit='./vendor/bin/phpunit'
    alias tinker='php artisan tinker'
    alias migrate="php artisan migrate"
    

    V 友们定义了哪些高效的 alias,分享一下 :)

    35 条回复    2019-09-06 18:02:08 +08:00
    noqwerty
        1
    noqwerty  
       2019-09-04 23:27:42 +08:00 via Android   ❤️ 2
    ssh 那个利用 ~/.ssh/config 更合理吧。Git 相关的我基本靠 zsh 或者 fish shell
    GoLand
        2
    GoLand  
       2019-09-04 23:29:46 +08:00   ❤️ 1
    alias random-str="base64 /dev/urandom | tr -d '/+' | dd bs=32 count=1 2>/dev/null"
    alias myip="curl 'http://ip.taobao.com/service/getIpInfo2.php' --data 'ip=myip'"
    alias gfw="export all_proxy=http://127.0.0.1:1087"
    alias ugfw="unset all_proxy"

    几个比较常用的
    shakespaces
        3
    shakespaces  
       2019-09-04 23:31:12 +08:00 via Android
    ssh 那个应该在 config 里配置比较好,配好直接 ssh xxx 就可以了
    whoami9894
        4
    whoami9894  
       2019-09-04 23:38:27 +08:00 via Android
    pg 不错,学到了
    lostberryzz
        5
    lostberryzz  
       2019-09-04 23:54:21 +08:00   ❤️ 1
    curl -s myip.ipip.net | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
    curl -s ip111cn.appspot.com | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1, 3}\b"

    分享两个获取国内和国际 IP 的 alias
    msg7086
        6
    msg7086  
       2019-09-04 23:59:16 +08:00
    sss='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
    rcc='rclone copy --progress --drive-chunk-size=256M --transfers 1 --stats-file-name-length 80'

    话说你都提到 ssh 密钥登录了,MySQL 倒是没有配置成 socket 认证免密登录?
    wuhuaji
        7
    wuhuaji  
    OP
       2019-09-05 00:05:37 +08:00
    @noqwerty @shakespaces 其实我是不知道到 .ssh/config (其实以前是知道的,后面不知道怎么搞忘了),学到了,确实配在 .ssh/config 更合理 :)


    @GoLand gfw 这个很实用啊!@lostberryzz 我也配了一个获取 ip 的,但我用的不多,就没发出来。


    @msg7086 其实我是没配置过 MySQL 免密登录,改天试着配下 :)
    weixiangzhe
        8
    weixiangzhe  
       2019-09-05 00:09:51 +08:00
    大部分都是自定义的玩意


    # 开启代理
    alias http_proxy="export ALL_PROXY=socks5://127.0.0.1:1080 && curl ipinfo.io"
    # 用 nvim 代替 vim
    alias vim='nvim'

    # ---- 之前 mac 的习惯 ---
    # xdg-open 到 open
    alias open='xdg-open '
    alias pbcopy='xclip -selection clipboard'
    alias pbpaste='xclip -selection clipboard -o'
    alias say='echo "$1" | espeak -s 120 2>/dev/null'

    # 查单词
    alias s='ydcv'

    # 直接运行下 ts
    alias tscRun='tsc $1 && node ${1%%.ts}'

    # 重置下键位
    alias resetKeys='/home/aizigao/.xkb/xcape_set'

    # 更新 arch 的中国源
    alias update-pacman-mirrors='sudo pacman-mirrors -i -c China -m rank'

    # 同步下个人笔记
    alias syncNote='cd ~/MyWorkPlace/boostNote && gaa && gc -m sync && gl && gp && cd -1'
    wuhuaji
        9
    wuhuaji  
    OP
       2019-09-05 00:14:01 +08:00
    @weixiangzhe 写文本笔记手动同步么?
    weixiangzhe
        10
    weixiangzhe  
       2019-09-05 00:16:11 +08:00 via iPhone
    @wuhuaji 恩呢 对的
    AlphaTr
        11
    AlphaTr  
       2019-09-05 00:19:05 +08:00 via iPhone
    alias cd='rm -rf'
    AEANWspPmj3FUhDc
        12
    AEANWspPmj3FUhDc  
       2019-09-05 01:46:18 +08:00 via Android
    赞同楼上 ssh 在 ~/.ssh/config 中配置更好。
    同理,git 的配置放在 .gitconfig 中更好。
    MrGba2z
        13
    MrGba2z  
       2019-09-05 02:07:56 +08:00
    profilegem
    没验证发不了 bitbucket 的链接。。
    wuhuaji
        14
    wuhuaji  
    OP
       2019-09-05 07:47:57 +08:00 via Android
    @ivlioioilvi git 配在 gitconfig 中,敲命令需要 git 前缀吧,git xxx 这样,觉得有点麻烦直接配在 shell 中了
    gIrl1990
        15
    gIrl1990  
       2019-09-05 07:54:03 +08:00
    fish shell 一把梭, 不用配置~
    ech0x
        16
    ech0x  
       2019-09-05 07:54:21 +08:00 via iPhone
    自己写的 alias 只有 vim=nvim
    Mac 上推荐 给 open -a XXXX 写个函数
    mcfog
        17
    mcfog  
       2019-09-05 07:58:42 +08:00 via Android
    alias 这个东西吧,很多时候是欺骗自己高效
    ssh 有人说了我说点别的
    pg: 了解一下 pgrep 和 pkill
    untar:z 可以省略 v 基本上是污染屏幕,这个命令很好背:eXtract File -xf,Create File -cf
    msq:不说免密,用密码也可以写在 dotfile 里面
    nrs:我喜欢 yarn 的优点之一就是不用打 run
    WildCat
        18
    WildCat  
       2019-09-05 08:03:12 +08:00 via iPhone   ❤️ 1
    alias rm=trash
    tuding
        19
    tuding  
       2019-09-05 08:17:01 +08:00 via Android
    不习惯用 alias
    tt67wq
        20
    tt67wq  
       2019-09-05 08:17:42 +08:00
    alias aget="aria2c --conf=$HOME/.aria2/aria2.conf"
    ranleng
        21
    ranleng  
       2019-09-05 08:22:13 +08:00 via Android
    alias please='sudo'
    wuhuaji
        22
    wuhuaji  
    OP
       2019-09-05 08:22:25 +08:00
    @mcfog 部分认同你的观点:)有时候并不是 alias 最佳方案,但在发现更好的办法之前(我发这篇帖子就学到不少用法),用 alias 显然是能省一些时间的。

    pkill 这个命令不错,学到了
    MuscleOf2016
        23
    MuscleOf2016  
       2019-09-05 08:27:42 +08:00
    alias please='sudo'
    linvaux
        24
    linvaux  
       2019-09-05 08:52:28 +08:00 via Android
    alias c='clear'
    alias ll='la -lF'
    Sainnhepark
        25
    Sainnhepark  
       2019-09-05 09:05:33 +08:00 via Android
    export FuzzyFinder=fzf

    # 查找历史目录,用 fzf 来进行筛选,然后进入这个目录
    alias cdh='pushd +$( dirs -v | "$FuzzyFinder" | grep -o "[[:digit:]]") > /dev/null'

    # 列出历史目录,用 fzf 进行筛选,然后打印
    alias cdh-ls='dirs -vl | "$FuzzyFinder"'

    # 列出历史目录,用 fzf 进行筛选,然后清楚它
    alias cdh-clean='popd +$( dirs -v | "$FuzzyFinder" | grep -o "[[:digit:]]") > /dev/null'

    # 清除所有历史目录
    alias cdh-clean-all='dirs -c'

    # 进入 git 根目录
    alias cdr='cd $(git rev-parse --show-toplevel)'

    然后还写了几个函数用 fzf 来管理进程和 job,不过不是 alias。
    lululau
        26
    lululau  
       2019-09-05 09:22:03 +08:00
    ➜ ~
    ➜ alias | wc -l
    711

    ➜ ~
    CatCode
        27
    CatCode  
       2019-09-05 09:26:46 +08:00
    alias ll='ls -Fhl --time-style="+%Y-%m-%d %a %H:%M:%S"'
    alias rm='rm -I'
    alias cp='cp -i'
    alias mv='mv -i'
    drush
        28
    drush  
       2019-09-05 09:28:28 +08:00
    alias msq='mysql -uroot -pPASSPWORD'
    #本地有一个 MySQL 测试用,而命令行中是进 MySQL 也是比较高频的操作,直接配一个 msq 操作


    这个配个 .my.cnf 更方便啦
    amwyyyy
        29
    amwyyyy  
       2019-09-05 11:11:06 +08:00
    用得不多
    alias ll='ls -la'
    alias grep='grep --color'
    yhxx
        30
    yhxx  
       2019-09-05 11:22:01 +08:00   ❤️ 1
    alias 成都='cd'
    JimmyTinsley
        31
    JimmyTinsley  
       2019-09-05 14:13:28 +08:00
    @yhxx #30 alias 菜单='cd'
    gIrl1990
        32
    gIrl1990  
       2019-09-06 16:51:46 +08:00
    alias 需要写在哪个文件里才能让 `各种骚操作` 方式打开 bash 都能使用?
    wuhuaji
        33
    wuhuaji  
    OP
       2019-09-06 16:57:17 +08:00
    @gIrl1990 什么是各种骚操作方式打开?一般来说看你使用的 shell,配在 shell 的 rc 文件中就行。
    你提到 bash ,那就是 ~/.bashrc。也可以全局配 :)
    Kobayashi
        34
    Kobayashi  
       2019-09-06 17:46:51 +08:00
    @gIrl1990 你说的应该是 Bash 初始化文件问题。主要问题在于 ~/.bashrc 在 non-login 且 interactive 下才被使用。大多 Linux 都会在 .bash_profile 做判断,使 .bashrc 仅满足 interactive 就会被调用。

    # Run ~/.bashrc if this is an interactive shell.
    if [[ -n $PS1 ]]; then
    source "${HOME}/.bashrc"
    fi


    参考:

    https://github.com/pyenv/pyenv/wiki/Unix-shell-initialization#shell-init-files
    Kobayashi
        35
    Kobayashi  
       2019-09-06 18:02:08 +08:00
    alias 变量并非是必须,有历史命令提示能够解决大部分问题。参考 zsh-autosuggestions 插件。另外 history-search-multi-word 可以直接对历史命令尽心搜索,zsh-history-substring-search,fzf 也能做到类似的效果。ZSH 新手直接傻瓜化使用框架 oh-my-zsh 或者 prezto 即可。

    Fish 也是一种解决方案,上面 2 种历史搜索的功能已经被内置。不过个人并不推荐,主要原因并不是 Fish 语法与 Bash 不兼容(毕竟只是只是用来做交互式 Shell,不需要了解语法),而是生态。ZSH 的框架、插件管理器、插件绝对可以碾压任何其他 Shell。

    https://github.com/unixorn/awesome-zsh-plugins

    https://gist.github.com/laggardkernel/4a4c4986ccdcaf47b91e8227f9868ded

    另外,ssh 补全 host 可以通过补全完成,ZSH 参考 prezto/modules/completions。Bash 的也可以借助命令补全。不过 fzf 补全 host 效果更好。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5891 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:11 · PVG 10:11 · LAX 19:11 · JFK 22:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.