10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Zsh Global Aliasのあれこれ

Posted at

global alias 概要

zshにはglobal aliasあります。

通常のaliasは最初のコマンドにしか作用しないのに対し、global aliasは以降のパラメータやオプションなどにも使用できるaliasです。

.zshrc
alias -g P='-c ~/.chef/knife_prod.rb'
alias -g D='-c ~/.chef/knife_dev.rb'

このようなaliasを定義しておくと、

knife node list P

などと実行できます。

pipeで使う

pipeと絡めたglobal aliasも便利です。

参考: oh-my-zshのplugin, common-aliases

global aliasの自動展開

コマンドを他の人に見せるときなどは、そのglobal aliasを知らない人にとって意味不明になってしまいます。
そういう観点から、以下のような設定を.zshrcに書いておくとよいでしょう。

.zshrc
# expand global aliases by space
# http://blog.patshead.com/2012/11/automatically-expaning-zsh-global-aliases---simplified.html
globalias() {
  if [[ $LBUFFER =~ ' [A-Z0-9]+$' ]]; then
    zle _expand_alias
    # zle expand-word
  fi
  zle self-insert
}

zle -N globalias

bindkey " " globalias
10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?