Home
Packages
Forums
Wiki
GitLab
Security
AUR
Download
コンテンツにスキップ
メインメニュー
メインメニュー
サイドバーに移動
非表示
案内
メインページ
目次
コミュニティに貢献
最近の出来事
おまかせ表示
特別ページ
交流
ヘルプ
貢献
最近の更新
最近の議論
新しいページ
統計
リクエスト
ArchWiki
検索
検索
表示
アカウント作成
ログイン
個人用ツール
アカウント作成
ログイン
Zshのソースを表示
ページ
議論
日本語
閲覧
ソースを閲覧
履歴を表示
ツール
ツール
サイドバーに移動
非表示
操作
閲覧
ソースを閲覧
履歴を表示
全般
リンク元
関連ページの更新状況
ページ情報
表示
サイドバーに移動
非表示
←
Zsh
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
[[Category:コマンドラインシェル]] [[cs:Zsh]] [[de:Zsh]] [[en:Zsh]] [[es:Zsh]] [[fr:Zsh]] [[ru:Zsh]] [[zh-hans:Zsh]] [http://www.zsh.org Zsh] は対話式シェルとしてもスクリプト言語のインタプリタとしても使えるパワフルな[[コマンドシェル|シェル]]です。POSIX sh と互換性がありながら (デフォルトではありません、{{ic|emulate sh}} を実行した時だけです)、[http://zsh.sourceforge.net/Guide/zshguide06.html タブ補完]の改善や [http://zsh.sourceforge.net/Doc/Release/Expansion.html グロビング]などの利点があります。 [http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4 Zsh FAQ] には他にも Zsh をあなたのシェルとして使うべき理由が列挙されています。 == インストール == インストールをする前に現在使っているシェルが何なのか知っておくとよいでしょう: $ echo $SHELL [[公式リポジトリ]]にある {{Pkg|zsh}} パッケージを[[pacman|インストール]]してください。追加の補完定義を使いたい場合は、{{pkg|zsh-completions}} パッケージもインストールします。 === 初期設定 === ターミナルで次のコマンドを実行して Zsh が正しくインストールされたか確認してください: $ zsh 基本的な設定を説明する '''zsh-newuser-install''' が表示されるはずです。これをスキップしたい場合、{{ic|q}} を押して下さい。表示されない場合は、次のコマンドで手動で呼び出すことができます: $ autoload -Uz zsh-newuser-install $ zsh-newuser-install -f {{Note|仮想端末のサイズが少なくとも 72×15 であることを確認してください。そうでないと ''zsh-newuser-install'' は実行されません。}} === Zsh をデフォルトのシェルにする === シェルが {{ic|/etc/shells}} に記載されていれば、{{ic|chsh}} コマンドを使って root 権限なしでデフォルトシェルを変更することができます。[[公式リポジトリ]]から Zsh をインストールしたのなら、既に {{ic|/etc/shells}} にエントリが作られているはずです。 現在使っているユーザーのデフォルトシェルを変更するには: $ chsh -s $(which zsh) {{Note|Zsh をデフォルトシェルとして起動するには、一度ログアウトしてからログインしなおす必要があります。}} 再ログイン後、Bash とは見た目が異なる Zsh のプロンプトが表示されていることに気づくでしょう。また、次のコマンドを実行することで Zsh が現在のシェルになっていることを確認できます: $ echo $SHELL {{Tip|{{Pkg|bash}} を置き換える場合、{{ic|~/.bashrc}} から {{ic|~/.zshrc}} に (例: プロンプトと[[Bash#エイリアス|エイリアス]]) また {{ic|~/.bash_profile}} から {{ic|~/.zprofile}} に (例: [[ログイン時に X を起動|X Window System を起動するコード]]) にコードを移動するとよいでしょう。}} == スタートアップ/シャットダウン ファイル == {{Tip| * インタラクティブシェルとログインシェルの説明、およびスタートアップファイルに何を入れるかについては、[http://zsh.sourceforge.net/Guide/zshguide02.html A User's Guide to the Z-Shell] を参照してください。 * Zsh 設定ファイルには [https://wiki.archlinux.jp/index.php/%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%82%B7%E3%82%A7%E3%83%AB#.E6.A8.99.E6.BA.96.E5.8C.96 標準パスの実装] を検討できます。}} {{Note| *{{ic|$ZDOTDIR}} が設定されていない場合は、代わりに {{ic|$HOME}} が使用されます。 *オプション {{ic|RCS}} がいずれかのファイルで設定されていない場合、そのファイルの後に設定ファイルは読み取りません。 *オプション {{ic |GLOBAL_RCS}} がいずれかのファイルで設定されていない場合、そのファイルの後にグローバル設定ファイル({{ic|/etc/zsh/*}})は読み取りません。 }} ログイン時、Zsh は以下のファイルをこの順番で読み込みます: ;{{ic|/etc/zsh/zshenv}}:このファイルにはグローバルな[[#$PATH の設定|コマンド検索パス]]やシステム共通の[[環境変数]]を設定するコマンドを記述します。出力を発生させるコマンドや tty に基づいているコマンドを含めてはいけません。 ;{{ic|~/.zshenv}}:このファイルには[[#$PATH の設定|コマンドの検索パス]]を設定するコマンドや、重要な環境変数を含めます; 出力をするコマンドや tty に基づいているコマンドを含めてはいけません。 ;{{ic|/etc/zsh/zprofile}}:グローバルな設定ファイルであり、ログイン時に読み込まれます。ログイン時に毎回実行するコマンドを記述するのに使われます。Arch Linux では、初めから {{ic|/etc/profile}} を source する[https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h=packages/zsh 一行]が入っています。詳しくは[[#全ユーザー共通の設定ファイル|下]]を参照。 ;{{ic|/etc/profile}}:このファイルはログイン時に Bourne 互換シェルによって読み込まれます: ログイン時やアプリケーション固有の設定 {{ic|/etc/profile.d/*.sh}}) を設定します。 ;{{ic|~/.zprofile}}:このファイルは一般的にユーザーのスクリプトを自動で実行するために使われます。 ;{{ic|/etc/zsh/zshrc}}:グローバルな設定ファイルであり、インタラクティブシェルとして起動した場合に読み込まれます。 ;{{ic|~/.zshrc}}:これは Zsh のメインの設定ファイルです。 ;{{ic|/etc/zsh/zlogin}}:グローバルな設定ファイルであり、ログインシェルとして起動した場合に、初期化の最後に読み込まれます。 ;{{ic|~/.zlogin}}:{{ic|/etc/zsh/zlogin}} と同じ設定ファイルですが、ユーザーごとの設定を記述するところが違います。 ;{{ic|/etc/zsh/zlogout}}:グローバルな設定ファイルであり、ログインシェルが終了するときに読み込まれます。 ;{{ic|~/.zlogout}}:{{ic|/etc/zsh/zlogout}} と同じ設定ファイルですが、ユーザーごとの設定を記述するところが違います。 {{Note| * Arch の {{Pkg|zsh}} パッケージで使われているパスは [[man ページ]]で使われているデフォルトのパスとは異なっています。 * {{ic|$ZDOTDIR}} のデフォルトは {{ic|$HOME}} です。 * {{ic|/etc/profile}} は Zsh のスタートアップファイルの標準リストに含まれていませんが、{{Pkg|zsh}} パッケージに入っている {{ic|/etc/zsh/zprofile}} から読み込まれます。{{ic|/etc/profile}} で {{ic|$PATH}} 変数を設定すると {{ic|~/.zshenv}} に設定した {{ic|$PATH}} 変数を全て上書きしてしまうので注意してください。これを防ぐには、{{ic|/etc/zsh/zprofile}} をあなたの作ったファイルに置き換えるか、{{ic|~/.zshrc}} から {{ic|$PATH}} 変数を設定してください。 }} === 全ユーザー共通の設定ファイル === 場合によっては Zsh ユーザー全てに設定を適用させたいときもあると思われます。zsh(1) によれば {{ic|/etc/zshrc}} などグローバルな設定ファイルはいくつか存在します。ただし、これは Arch では少し事情が異なっており、代わりに {{ic|/etc/zsh/}} を使うようにコンパイルされています [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/zsh#n34]。 従って、ユーザー共通の設定をしたいときは {{ic|/etc/zshrc}} ではなく {{ic|/etc/zsh/zshrc}} を使って下さい。これは {{ic|/etc/zsh/zshenv}}, {{ic|/etc/zsh/zlogin}}, {{ic|/etc/zsh/zlogout}} でも同じです。これらのファイルはデフォルトではインストールされないので、必要なときに作成してください。 唯一の例外は {{ic|zprofile}} で、これは {{ic|/etc/profile}} を使います。 == ~/.zshrc の設定 == Zsh は何も設定しなくても使うことができますが、あなたが使いたい機能はほとんど設定されていないでしょう。ただし Zsh で利用できるカスタム化の道は険しく、Zsh の設定は困難をきわめ多くの時間を浪費するかもしれません。 === シンプルな .zshrc === 下にはサンプルの設定ファイルが含まれており、Zsh のカスタマイズの方法の例だけでなくデフォルトオプションのセットも提供しています。この設定を使うには {{ic|.zshrc}} という名前でファイルを保存してください。ログインしなおさなくても次を実行することで変更を適用できます: $ source ~/.zshrc 以下はシンプルな {{ic|.zshrc}} です、基点としてはピッタリでしょう: {{hc|~/.zshrc| autoload -U compinit promptinit compinit promptinit # This will set the default prompt to the walters theme prompt walters}} === $PATH の設定 === 通常、PATH は {{ic|~/.zshenv}} で設定すべきですが、Arch Linux は {{ic|~/.zshenv}} を読み込んだ後に {{ic|/etc/profile}} を読み込みます (全体の流れは[[#設定ファイル|設定ファイル]]を見てください)。 {{ic|$PATH}} の上書きを防ぐために、{{ic|~/.zprofile}} で設定するようにしてください: {{hc|1=~/.zprofile|2= typeset -U path path=(~/bin /other/things/in/path $path[@])}} 詳しくは [http://zsh.sourceforge.net/Guide/zshguide02.html#l24 Z-Shell のユーザーガイド] を参照してください。 === コマンド補完 === Zsh の一番魅力的な機能はおそらく先進的な自動補完機能でしょう。少なくとも、{{ic|.zshrc}} で自動補完を有効にしたいと思うはずです。自動補完を有効にするには、以下を追加してください: {{hc|~/.zshrc| autoload -U compinit compinit}} この設定は ssh/scp/sftp のホスト名の補完も含んでいますが、この機能を動かすには {{ic|~/.ssh/known_hosts}} で ssh がホスト名をハッシュ化しないようにする必要があります。 {{Warning|ハッシュ化を解除すると [http://blog.rootshell.be/2010/11/03/bruteforcing-ssh-known_hosts-files/ "Island-hopping" 攻撃] に無防備になります。そのことを前もって知った上で、次の行をコメントアウトするか値を {{ıc|no}} に設定してください: {{hc|/etc/ssh/ssh_config| #HashKnownHosts yes}} これで {{ic|~/.ssh/known_hosts}} をどこか他のところに移動すれば ssh はハッシュ化されてないホスト名を使って新しいファイルを作成します (以前のホストは消失します)。詳細については、[http://nms.lcs.mit.edu/projects/ssh/README.hashed-hosts hashed-hosts] に関する SSH の readme を見て下さい。 }} 矢印キーのインターフェイスを使って自動補完するには、以下を追加して下さい: {{hc|~/.zshrc| zstyle ':completion:*' menu select}} :''メニューを有効にするには、タブを二度押して下さい。'' エイリアスでコマンドラインの自動補完を切り替えるには、以下を追加して下さい: {{hc|~/.zshrc| setopt completealiases}} === キーバインド === Zsh は readline を使っていません、代わりに自身のパワフルな zle を使っています。zle は {{ic|/etc/inputrc}} や {{ic|~/.inputrc}} を読みません。 zle には [[emacs]] モードと [[vi]] モードがあります。デフォルトでは、{{ic|$EDITOR}} 環境変数から emacs と vi どちらのキーをあなたが使いたいのか考えます。この変数が空の場合、デフォルトは emacs モードです。 {{ic|bindkey -v}} や {{ic|bindkey -e}} でモードを変更することが可能です。 特殊なキーを動作させるには: {{hc|~/.zshrc|2= # create a zkbd compatible hash; # to add other keys to this hash, see: man 5 terminfo typeset -A key key[Home]=${terminfo[khome]} key[End]=${terminfo[kend]} key[Insert]=${terminfo[kich1]} key[Delete]=${terminfo[kdch1]} key[Up]=${terminfo[kcuu1]} key[Down]=${terminfo[kcud1]} key[Left]=${terminfo[kcub1]} key[Right]=${terminfo[kcuf1]} key[PageUp]=${terminfo[kpp]} key[PageDown]=${terminfo[knp]} # setup key accordingly [[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line [[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line [[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode [[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" delete-char [[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history [[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history [[ -n "${key[Left]}" ]] && bindkey "${key[Left]}" backward-char [[ -n "${key[Right]}" ]] && bindkey "${key[Right]}" forward-char [[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" beginning-of-buffer-or-history [[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" end-of-buffer-or-history # Finally, make sure the terminal is in application mode, when zle is # active. Only then are the values from $terminfo valid. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then function zle-line-init () { printf '%s' "${terminfo[smkx]}" } function zle-line-finish () { printf '%s' "${terminfo[rmkx]}" } zle -N zle-line-init zle -N zle-line-finish fi }} {{Note|特定のキーの組み合わせに対して適切なシーケンスを取得するには、 {{ic|cat}} を起動するか、パラメーターなしで {{ic|read}} を押します。 その後、端末に印刷する必要があります。 {{ic|Ctrl+c}} を使用して両方を再び閉じることができます。}} ==== 履歴検索 ==== {{ic|key}} 配列を設定し、ZLEがアプリケーションモードになっている事を確認して、以下の手順を使用してください。[[#キーバインド]] を参照してください。 履歴検索を有効にするには、以下の行を {{ic|.zshrc}} ファイルに追加します: {{hc|~/.zshrc| autoload -Uz up-line-or-beginning-search down-line-or-beginning-search zle -N up-line-or-beginning-search zle -N down-line-or-beginning-search [[ -n "${key[Up]}" ]] && bindkey -- "${key[Up]}" up-line-or-beginning-search [[ -n "${key[Down]}" ]] && bindkey -- "${key[Down]}" down-line-or-beginning-search }} これにより、{{ic|up}} または {{ic|down}} キーを押すと、現在の行から現在のカーソル位置までに一致する過去のコマンドのみが表示されます。 ==== Shift, Alt, Ctrl and Meta 修飾キー ==== xterm 互換端末は {{man|5|user_caps}} からの拡張キー定義を使うことができます。 {{ic|Shift}}、 {{ic|Alt}}、 {{ic|Ctrl}}、 および {{ic|Meta}} と、 {{ic|Up}}、 {{ic|Down}}、 {{ic|Left}}、 {{ic|Right}}、 {{ic|PageUp}}、 {{ic|PageDown}}、 {{ic|Home}}、 {{ic|End}}、 または {{ic|Del}} を組み合わせたものです。修飾キーとキーの組み合わせに推奨される名前の一覧については、 [https://sourceforge.net/p/zsh/code/ci/master/tree/Functions/Misc/zkbd zkbd ソース] を参照してください。 たとえば、 {{ic|Ctrl+Left}} を前の単語の先頭に移動し、 {{ic|Ctrl+Right}} を次の単語の先頭に移動するには、次のように指定します: {{hc|~/.zshrc|2= key[Control-Left]="${terminfo[kLFT5]}" key[Control-Right]="${terminfo[kRIT5]}" [[ -n "${key[Control-Left]}" ]] && bindkey -- "${key[Control-Left]}" backward-word [[ -n "${key[Control-Right]}" ]] && bindkey -- "${key[Control-Right]}" forward-word }} === プロンプト === Zsh は、プロンプトテーマを使用するか、テーマに不満がある (またはその有用性を拡張したい) ユーザーのために、カスタムプロンプトを構築するオプションを提供しています。 ==== プロンプトテーマ ==== プロンプトテーマは、Zshで色付きのプロンプトを設定するためのすばやく簡単な方法です。それらの詳細については、{{man|1|zshcontrib|PROMPT THEMES}} を参照してください。 テーマを使用するには、プロンプトテーマシステムが、{{ic|.zshrc}} に自動でロードされるよう設定されていることを確認してください。自動ロードを行うには、以下の行を追加します。 {{hc|~/.zshrc| autoload -Uz promptinit promptinit }} 次のコマンドを実行することで利用可能なプロンプトを見ることができます: $ prompt -l 例えば、walters プロンプトを使うには、次のように入力します: $ prompt walters 全ての利用可能なテーマをプレビューするには次のコマンドを使って下さい: $ prompt -p ===== プロンプトテーマの手動インストール ===== 外部の設定管理ツールを使わずに、テーマを手動でインストールすることもできます。ローカルインストールの場合、最初にフォルダを使用し、それを {{ic|fpath}} 配列に通過します。例: $ mkdir ~/.zprompts $ fpath=("$HOME/.zprompts" "$fpath[@]") 次に、このフォルダにテーマファイルのシンボリックリンクを作成します: $ ln -s mytheme.zsh ~/.zprompts/prompt_mytheme_setup 代わりに、テーマをグローバルにインストールする場合は、次のようにします: # ln -s mytheme.zsh /usr/share/zsh/functions/Prompts/prompt_mytheme_setup これで、次のコマンドを使ってアクティブ化できるはずです: $ prompt mytheme すべてが機能する場合は、{{ic|.zshrc}} をそれに応じて編集することができます。 ===== ファイルの無いプロンプトテーマを追加する ===== プロンプトテーマを独自のファイルから追加するだけでなく、次のように別のファイル ({{ic|.zshrc のように}})からテーマを追加することもできます。 例えば: {{hc|~/.zshrc|2= # Load promptinit autoload -Uz promptinit && promptinit # Define the theme prompt_mytheme_setup() { PS1="%~%# " } # Add the theme to promptsys prompt_themes+=( mytheme ) # Load the theme prompt mytheme }} ==== プロンプトのカスタマイズ ==== 上述のプロンプトに満足がいかない(もしくはプロンプトを拡張したい)場合、Zsh にはカスタムプロンプトを作る機能が備わっています。全てのシェルに共通な左側だけのプロンプトに加え、Zsh は左右両側を使うプロンプトをサポートしています。{{ic|1=PROMPT=}} と以下の変数を使うことでプロンプトをカスタマイズできます: ===== プロンプト変数 ===== {| class="wikitable" ! scope="col" width="10%"| コマンド || !scope="col" width="60%"|説明 || !scope="col" width="30%"|コメント |- ! colspan="3" | 一般 |- |{{ic|%n}} || ユーザー名 |- |{{ic|%m}} || コンピュータのホスト名 (ドットの前の部分まで) |- |{{ic|%M}} || コンピュータのホスト名 |- |{{ic|%l}} || 現在の tty |- |{{ic|%?}} || 最後に実行したアプリケーションのリターンコード |- |{{ic|%#}} || ユーザー特権によるプロンプト (root なら {{ic|#}} その他なら {{ic|%}}) |- ! colspan="3" | 時間 |- | {{ic|%T}}|| システム時刻 (HH:MM) |- | {{ic|%*}}|| システム時刻 (HH:MM:SS) |- | {{ic|%D}}|| システム日付 (YY-MM-DD) |- ! colspan="3" | ディレクトリ |- | {{ic|%d}} || カレントディレクトリ。 | rowspan="2" | 数字を前に付けることでパスの一部だけを表示することが可能です。例えば {{ic|%1d}} と入力した場合、{{ic|/usr/bin}} にいるときは {{ic|bin}} と表示されます。負の値も使えます: {{ic|%-1d}} は上の例だと {{ic|/}} と表示されます。 |- | {{ic|%~}}|| カレントディレクトリ。{{ic|$HOME}} やそのサブディレクトリにいる場合、{{ic|$HOME}} の部分は {{ic|~}} に置き換えられます。 |- ! colspan="3" | 整形 |- | {{ic|%U [...] %u}} || 下線表示の始まりと終わり |- | {{ic|%B [...] %b}} || 太字表示の始まりと終わり |- | {{ic|<nowiki>%{ [...] %}</nowiki>}} || 表示されない領域。色の設定をするときに使います。このタグによって Zsh はタグの中にあるものを全て無視します。使用しないとプロンプトの縁やインデントに妙な影響が出ることがあります。 |- ! colspan="3" | カラー |- | {{ic|$fg[color]}}||文字色を設定します (red, green, blue など - 太字デフォルト)。 | rowspan="4" | Zsh は [[Bash カラープロンプト|Bash]] とは違ったふうに色を設定します。色を使うには {{ic|.zshrc}} で {{ic|1=PROMPT=}} の前に {{ic|autoload -U colors && colors}} を追加してください。通常、カーソルが動かないように色の設定は {{ic|%{ [...] %} }} の中に記述します。 |- | {{ic|$fg_no_bold[color]}}|| 太字でない文字色を設定します。 |- | {{ic|$fg_bold[color]}}|| 太字の文字色を設定します。 |- | {{ic|$reset_color}}|| 文字色をデフォルトの色に戻します。 |} {| class="wikitable" |- ! colspan="2" | 利用できるカラー |- | {{ic|black}} または {{ic|0}} || {{ic|red}} または {{ic|1}} |- | {{ic|green}} または {{ic|2}} || {{ic|yellow}} または {{ic|3}} |- | {{ic|blue}} または {{ic|4}} || {{ic|magenta}} または {{ic|5}} |- | {{ic|cyan}} または {{ic|6}} || {{ic|white}} または {{ic|7}} |} {{Note|太字の文字色は通常の文字色と必ずしも同じ色になるわけではありません。例えば、{{ic|$fg['yellow']}} は褐色や濃黄のようになりますが、{{ic|$fg_bold['yellow']}} はやや明るい普通の黄色のようになります。}} ===== サンプル ===== 以下のようにすることで左右両側にプロンプトを表示できます: PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%#" RPROMPT="[%{$fg_no_bold[yellow]%}%?%{$reset_color%}]" 次のようになります (色は省略): username@host ~ % [0] === サンプル .zshrc ファイル === 以下は {{ic|.zshrc}} ファイルのリストです。自由に自分のファイルを追加してください: * 公式リポジトリにある {{Pkg|grml-zsh-config}} パッケージは http://grml.org/zsh から取られて来たもので Zshell の多数の調整が含まれた zshrc ファイルが入っています。これは[https://www.archlinux.org/download/ 毎月リリースされている ISO] のデフォルト設定です。 * 動的なプロンプトとウィンドウタイトル、hardinfo を使う、基本セットアップ => https://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc * https://github.com/slashbeast/things/blob/master/configs/DOTzshrc - 複数の機能を有効にした zshrc、コメントを見て下さい。特徴: ユーザーが電源オフ・再起動・ハイバネートをすることを確認する関数、プロンプトの GIT サポート (vcsinfo を使わずに実現)、メニューの付いたタブ補完、実行中のコマンドをウィンドウのタイトルバーに表示など。 == ヒントとテクニック == === ログイン時に X を自動起動 === [[xinit#ログイン時に X を自動起動]]を参照してください。 === Restore terminal settings after a program exits abnormally === Many programs change the terminal state, and often do not restore terminal settings on exiting abnormally (e.g. when crashing or encountering SIGINT). This can typically be solved by executing {{man|1|reset}}: $ reset The following sections describe ways to avoid the need to manually reset the terminal. ==== The ttyctl command ==== The [http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-tty_002c-freezing ttyctl] command can be used to "freeze/unfreeze" the terminal. To freeze the interactive shell on launch, use the following: {{hc|~/.zshrc| ttyctl -f }} ==== Resetting the terminal with escape sequences ==== [https://www.in-ulm.de/~mascheck/various/alternate_charset/ Alternate linedrawing character set] can screw up the terminal in a way which ttyctl cannot prevent. A simple solution is to output the escape sequences that reset the terminal from the {{ic|precmd}} hook function, so that they are executed every time before the prompt is drawn. For example, using [https://www.in-ulm.de/~mascheck/various/alternate_charset/#solution the escape sequence] {{ic|\e[0m\e(B\e)0\017\e[?5l\e7\e[0;0r\e8}}: {{hc|~/.zshrc|2= autoload -Uz add-zsh-hook function reset_broken_terminal () { printf '%b' '\e[0m\e(B\e)0\017\e[?5l\e7\e[0;0r\e8' } add-zsh-hook -Uz precmd reset_broken_terminal }} To test if it works, run: $ print '\e(0\e)B' === Remembering recent directories === ==== Dirstack ==== Zsh can be configured to remember the DIRSTACKSIZE last visited folders. This can then be used to ''cd'' them very quickly. You need to add some lines to your configuration file: {{hc|~/.zshrc|<nowiki> autoload -Uz add-zsh-hook DIRSTACKFILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/dirs" if [[ -f "$DIRSTACKFILE" ]] && (( ${#dirstack} == 0 )); then dirstack=("${(@f)"$(< "$DIRSTACKFILE")"}") [[ -d "${dirstack[1]}" ]] && cd -- "${dirstack[1]}" fi chpwd_dirstack() { print -l -- "$PWD" "${(u)dirstack[@]}" > "$DIRSTACKFILE" } add-zsh-hook -Uz chpwd chpwd_dirstack DIRSTACKSIZE='20' setopt AUTO_PUSHD PUSHD_SILENT PUSHD_TO_HOME ## Remove duplicate entries setopt PUSHD_IGNORE_DUPS ## This reverts the +/- operators. setopt PUSHD_MINUS </nowiki>}} Now use $ dirs -v to print the dirstack. Use {{ic|cd -<NUM>}} to go back to a visited folder. Use autocompletion after the dash. This proves very handy if using the autocompletion menu. {{Note|This will not work if you have more than one ''zsh'' session open, and attempt to {{ic|cd}}, due to a conflict in both sessions writing to the same file.}} ==== cdr ==== cdr allows you to change the working directory to a previous working directory from a list maintained automatically. It stores all entries in files that are maintained across sessions and (by default) between terminal emulators in the current session. See {{man|1|zshcontrib|REMEMBERING RECENT DIRECTORIES}} for setup instructions. ==== zoxide ==== {{Pkg|zoxide}} is a smarter ''cd'' command that lets you navigate anywhere in just a few keystrokes. It remembers your frequently used directories and uses a scoring mechanism to guess where you want to go. === Help command === Unlike [[Bash]], Zsh does not enable a built in {{ic|help}} command, instead it provides {{ic|run-help}}. By default {{ic|run-help}} is an alias to {{ic|man}}, it can be either executed manually by prepending it to a command or it can be invoked for the currently typed command with the keyboard shortcuts {{ic|Alt+h}} or {{ic|Esc}} {{ic|h}}. Since by default it is just an alias to [[man]], it will only work on external commands. To improve its functionality, so that it works on shell builtins and other shell features, you need to use the {{ic|run-help}} function. See {{man|1|zshcontrib}} for more information on the {{ic|run-help}} and its assistant functions. First load the {{ic|run-help}} function and then remove the existing {{ic|run-help}} alias. For convenience {{ic|help}} can be aliased to {{ic|run-help}}. For example, add following to your {{ic|zshrc}}: autoload -Uz run-help (( ${+aliases[run-help]} )) && unalias run-help alias help=run-help Assistant functions have to be enabled separately: autoload -Uz run-help-git run-help-ip run-help-openssl run-help-p4 run-help-sudo run-help-svk run-help-svn For example, {{ic|run-help git commit}} command will now open the [[man page]] {{man|1|git-commit}} instead of {{man|1|git}}. === Persistent rehash === Typically, compinit will not automatically find new executables in the {{ic|$PATH}}. For example, after you install a new package, the files in {{ic|/usr/bin/}} would not be immediately or automatically included in the completion. Thus, to have these new executables included, one would run: $ rehash This 'rehash' can be set to happen automatically.[https://github.com/robbyrussell/oh-my-zsh/issues/3440] Simply include the following in your {{ic|zshrc}}: {{hc|~/.zshrc| zstyle ':completion:*' rehash true }} ==== On-demand rehash ==== As above, however [[pacman]] can be configured with [[Pacman#Hooks|hooks]] to automatically request a {{ic|rehash}}, which does not incur the performance penalty of constant rehashing as above. To enable this, create the {{ic|/etc/pacman.d/hooks}} directory, and a {{ic|/var/cache/zsh}} directory, then create a hook file: {{hc|head=/etc/pacman.d/hooks/zsh.hook|output= [Trigger] Operation = Install Operation = Upgrade Operation = Remove Type = Path Target = usr/bin/* [Action] Depends = zsh When = PostTransaction Exec = /usr/bin/install -Dm644 /dev/null /var/cache/zsh/pacman }} This keeps the modification date of the file {{ic|/var/cache/zsh/pacman}} consistent with the last time a package was installed, upgraded or removed. Then, {{ic|zsh}} must be coaxed into rehashing its own command cache when it goes out of date, by adding to your {{ic|~/.zshrc}}: {{hc|~/.zshrc|<nowiki> zshcache_time="$(date +%s%N)" autoload -Uz add-zsh-hook rehash_precmd() { if [[ -a /var/cache/zsh/pacman ]]; then local paccache_time="$(date -r /var/cache/zsh/pacman +%s%N)" if (( zshcache_time < paccache_time )); then rehash zshcache_time="$paccache_time" fi fi } add-zsh-hook -Uz precmd rehash_precmd </nowiki>}} If the {{ic|precmd}} hook is triggered before {{ic|/var/cache/zsh/pacman}} is updated, completion may not work until a new prompt is initiated. Running an empty command, e.g. pressing {{ic|enter}}, should be sufficient. ==== Alternative on-demand rehash using SIGUSR1 ==== As above, however the hook file looks like this: {{hc|/etc/pacman.d/hooks/zsh-rehash.hook|output= [Trigger] Operation = Install Operation = Upgrade Operation = Remove Type = Path Target = usr/bin/* [Action] Depends = zsh Depends = procps-ng When = PostTransaction Exec = /usr/bin/pkill zsh --signal=USR1 }} {{Warning|This sends SIGUSR1 to all running {{ic|zsh}} instances. Note that the default behavior for SIGUSR1 is terminate so when you first configure this all running {{ic|zsh}} instances of all users (including login shells) will terminate if they have not sourced the trap below.}} {{hc|~/.zshrc| TRAPUSR1() { rehash } }} The ''function trap'' above can be replaced with a ''list trap'' {{ic|trap 'rehash' USR1}}. See {{man|1|zshmisc|Trap Functions}} for differences between types of traps. This method will instantly {{ic|rehash}} all {{ic|zsh}} instances, removing the need to press enter to trigger {{ic|precmd}}. === Bind key to ncurses application === Bind a ncurses application to a keystroke, but it will not accept interaction. Use {{ic|BUFFER}} variable to make it work. The following example lets users open [[ncmpcpp]] using {{ic|Alt+\}}: {{hc|~/.zshrc|2= ncmpcppShow() { BUFFER="ncmpcpp" zle accept-line } zle -N ncmpcppShow bindkey '^[\' ncmpcppShow }} An alternate method, that will keep everything you entered in the line before calling application: {{hc|~/.zshrc|2= ncmpcppShow() { ncmpcpp <$TTY zle redisplay } zle -N ncmpcppShow bindkey '^[\' ncmpcppShow }} === File manager key binds === Key binds like those used in graphic file managers may come handy. The first comes back in directory history ({{ic|Alt+Left}}), the second let the user go to the parent directory ({{ic|Alt+Up}}). They also display the directory content. {{hc|~/.zshrc|<nowiki> cdUndoKey() { popd zle reset-prompt print ls zle reset-prompt } cdParentKey() { pushd .. zle reset-prompt print ls zle reset-prompt } zle -N cdParentKey zle -N cdUndoKey bindkey '^[[1;3A' cdParentKey bindkey '^[[1;3D' cdUndoKey </nowiki>}} === xterm title === If your terminal emulator supports it, you can set its title from Zsh. This allows dynamically changing the title to display relevant information about the shell state, for example showing the user name and current directory or the currently executing command. The xterm title is set with the [https://www.tldp.org/HOWTO/Xterm-Title-3.html#ss3.1 xterm escape sequence] {{ic|\e]2;}}{{ic|\a}}. For example: $ print -n '\e]2;My xterm title\a' will set the title to My xterm title A simple way to have a dynamic title is to set the title in the {{ic|precmd}} and {{ic|preexec}} hook functions. See {{man|1|zshmisc|Hook Functions}} for a list of available hook functions and their descriptions. By using {{ic|print -P}} you can additionally take advantage of Zsh's prompt escapes. {{Tip| * Title printing can be split up in multiple commands as long as they are sequential. * [[GNU Screen]] sends the xterm title to the hardstatus ({{ic|%h}}). If you want to use Screen's [https://www.gnu.org/software/screen/manual/html_node/String-Escapes.html string escapes] (e.g. for colors) you should set the hardstatus with the {{ic|\e_}}{{ic|\e\\}} escape sequence. Otherwise, if string escapes are used in {{ic|\e]2;}}{{ic|\a}}, the terminal emulator will get a garbled title due to it being incapable of interpreting Screen's string escapes. }} {{Note| * Do not use the {{ic|-P}} option of {{ic|print}} when printing variables to prevent them from being parsed as prompt escapes. * Use the {{ic|q}} [http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags parameter expansion flag] when printing variables to prevent them from being parsed as escape sequences. }} {{hc|~/.zshrc|<nowiki> autoload -Uz add-zsh-hook function xterm_title_precmd () { print -Pn -- '\e]2;%n@%m %~\a' [[ "$TERM" == 'screen'* ]] && print -Pn -- '\e_\005{g}%n\005{-}@\005{m}%m\005{-} \005{B}%~\005{-}\e\\' } function xterm_title_preexec () { print -Pn -- '\e]2;%n@%m %~ %# ' && print -n -- "${(q)1}\a" [[ "$TERM" == 'screen'* ]] && { print -Pn -- '\e_\005{g}%n\005{-}@\005{m}%m\005{-} \005{B}%~\005{-} %# ' && print -n -- "${(q)1}\e\\"; } } if [[ "$TERM" == (Eterm*|alacritty*|aterm*|gnome*|konsole*|kterm*|putty*|rxvt*|screen*|tmux*|xterm*) ]]; then add-zsh-hook -Uz precmd xterm_title_precmd add-zsh-hook -Uz preexec xterm_title_preexec fi </nowiki>}} ==== Terminal emulator tab title ==== Some terminal emulators and multiplexers support setting the title of the tab. The escape sequences depend on the terminal: {| class="wikitable sortable" ! Terminal ! Escape sequences ! Description |- ! [[GNU Screen]] | {{ic|\ek}}{{ic|\e\\}} | Screen's window title ({{ic|%t}}). |- ! [[Konsole]] | {{ic|\e]30;}}{{ic|\a}} | Konsole's tab title. |} === Shell environment detection === See [https://gitlab.com/jdorel-documentation/shell-environment-detection a repository about shell environment detection] for tests to detect the shell environment. This includes login/interactive shell, Xorg session, TTY and SSH session. === /dev/tcp equivalent: ztcp === Use the {{ic|zsh/net/tcp}} module: $ zmodload zsh/net/tcp You can now establish TCP connections: $ ztcp example.com 80 === Shortcut to exit shell on partial command line === By default, {{ic|Ctrl+d}} will not close your shell if the command line is filled, this fixes it: {{hc|.zshrc| exit_zsh() { exit } zle -N exit_zsh bindkey '^D' exit_zsh }} === pacman -F "command not found" handler === [[pacman]] includes functionality to search for packages containing a file. The following command-not-found handler will use pacman directly to search for matching packages when an unknown command is executed. {{hc|1=~/.zshrc|2= ... function command_not_found_handler { local purple='\e[1;35m' bright='\e[0;1m' green='\e[1;32m' reset='\e[0m' printf 'zsh: command not found: %s\n' "$1" local entries=( ${(f)"$(/usr/bin/pacman -F --machinereadable -- "/usr/bin/$1")"} ) if (( ${#entries[@]} )) then printf "${bright}$1${reset} may be found in the following packages:\n" local pkg for entry in "${entries[@]}" do # (repo package version file) local fields=( ${(0)entry} ) if [[ "$pkg" != "${fields[2]}" ]] then printf "${purple}%s/${bright}%s ${green}%s${reset}\n" "${fields[1]}" "${fields[2]}" "${fields[3]}" fi printf ' /%s\n' "${fields[4]}" pkg="${fields[2]}" done fi } ... }} {{Note|The files database of pacman is separate from the normal sync database and it needs to be fetched using {{ic|pacman -Fy}}. See [[pacman#Search for a package that contains a specific file]] for details.}} For an alternative using ''pkgfile'', see [[#pkgfile "command not found" handler]]. == サードパーティ拡張 == === 設定フレームワーク === * [https://github.com/robbyrussell/oh-my-zsh oh-my-zsh] は Zsh の設定を管理するための、人気のあるコミュニティドリブンなフレームワークです。便利な関数、ヘルパー、プラグイン、テーマが山ほどバンドルされています。 * [https://github.com/sorin-ionescu/prezto Prezto - Instantly Awesome Zsh] (AUR の {{AUR|prezto-git}} でインストール可能) は Zsh の設定フレームワークです。デフォルトでリッチなコマンドラインインターフェース環境を構築するモジュール・エイリアス・関数・自動補完・プロンプトテーマが付いています。 * [https://github.com/zimfw/zimfw Zim] (AUR の {{AUR|zsh-zim-git}} でインストール可能) は、驚異的な速度とモジュラー拡張機能を備えたZsh構成フレームワークです。 === プラグインマネージャー === * {{App|Antibody|Antigen に似たパフォーマンス重視のプラグインマネージャー |https://github.com/getantibody/antibody|{{AUR|antibody}}}} * {{App|zinit (previously "zplugin")|柔軟で高速な Zsh プラグインマネージャー |http://github.com/zdharma/zinit|{{AUR|zsh-zplugin-git}}}} * {{App|zr|Rust で書かれたシンプルで高速なプラグインマネージャー |https://github.com/jedahan/zr|{{AUR|zr-git}}}} * {{App|sheldon|Rust で書かれた高速で柔軟なプラグインマネージャー |https://github.com/rossmacarthur/sheldon|{{AUR|sheldon-bin}}}} * {{App|zpm|キャッシュを生成し柔軟で高速なプラグインマネージャー |https://github.com/zpm-zsh/zpm|{{AUR|zpm-git}}}} * {{App|Antigen|oh-my-zsh と vundle に触発された Zsh のプラグインマネージャー [https://github.com/zsh-users/antigen/issues/673 ABANDONED]|https://github.com/zsh-users/antigen|{{AUR|antigen-git}}}} * {{App|zgen|Zsh 用の軽量でシンプルなプラグインマネージャー [https://github.com/tarjoilija/zgen/issues/123 ABANDONED]|https://github.com/tarjoilija/zgen|{{AUR|zgen-git}}}} * {{App|zplug|Zsh 用の次世代プラグインマネージャー [https://github.com/zplug/zplug/issues/403#issuecomment-477520784 ABANDONED]|https://github.com/zplug/zplug|{{AUR|zplug}}}} === Fish のようなシンタックスハイライト === [[Fish]] には強力なシェルシンタックスハイライトがあります。これを zsh で使うには、公式リポジトリから {{pkg|zsh-syntax-highlighting}} をインストールして以下を zshrc に追加します: {{bc| source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh }} === "command not found" フック === [[pkgfile]] には、認識できないコマンドを入力したときに、自動的に公式リポジトリを検索する {{ic|command_not_found_handler}} 関数を提供する Zsh スクリプトファイルが含まれています。 有効にするには、スクリプトを [[source]] する必要があります。例: {{hc|~/.zshrc| source /usr/share/doc/pkgfile/command-not-found.zsh }} {{Note|pkgfile データベースの更新が必要な場合があります。[[pkgfile#Installation]] で詳細をご確認ください。}} == アンインストール == Zsh があなたに合わないと感じて Bash に戻りたいときは、Zsh パッケージを削除する前に必ず、まずデフォルトシェルを変更して下さい。 {{Warning|以下の手順に従わないと、あらゆる種類の問題が発生します。ユーザーはシェルプログラムを使用できなくなります。}} ターミナルで次のコマンドを root で実行して下さい: # chsh -s /bin/bash user Zsh を使っている全てのユーザーで実行して下さい。 これで安全に Zsh パッケージを削除することができます。 以上を行わなかった場合、root で {{ic|/etc/passwd}} を編集することで Bash にデフォルトシェルを戻すことができます。 {{Warning|ユーザー情報を編集するときは、不適切な形式のエントリを防ぐため、 {{ic|vipw}} を使用することを '''強く''' お勧めします。}} 例: 編集前: ''username'':x:1000:1000:Full Name,,,:/home/''username'':/bin/zsh 編集後: ''username'':x:1000:1000:Full Name,,,:/home/''username'':/bin/bash == 参照 == *[[Wikipedia:Zsh]] *[http://zsh.sourceforge.net/Intro/intro_toc.html An Introduction to the Z Shell] *[http://zsh.sourceforge.net/Guide/zshguide.html ユーザーガイド] *[http://zsh.sourceforge.net/Doc/Release/index-frame.html The Z Shell Manual] (different format available [http://zsh.sourceforge.net/Doc/ here]) *[http://zsh.sourceforge.net/FAQ/zshfaq01.html Zsh FAQ] *[http://zshwiki.org/home/ Zsh Wiki] *{{man|1|zsh-lovers}} (公式リポジトリの {{pkg|zsh-lovers}} で利用できます) *[https://wiki.gentoo.org/wiki/Zsh/HOWTO Gentoo Wiki: Zsh/HOWTO] *[http://www.bash2zsh.com/zsh_refcard/refcard.pdf Bash2Zsh Reference Card]
このページで使用されているテンプレート:
テンプレート:AUR
(
ソースを閲覧
)
テンプレート:App
(
ソースを閲覧
)
テンプレート:Bc
(
ソースを閲覧
)
テンプレート:Hc
(
ソースを閲覧
)
テンプレート:Ic
(
ソースを閲覧
)
テンプレート:META Missing package
(
ソースを閲覧
)
テンプレート:Man
(
ソースを閲覧
)
テンプレート:Note
(
ソースを閲覧
)
テンプレート:Pkg
(
ソースを閲覧
)
テンプレート:Tip
(
ソースを閲覧
)
テンプレート:TranslationStatus
(
ソースを閲覧
)
テンプレート:Warning
(
ソースを閲覧
)
Zsh
に戻る。
検索
検索
Zshのソースを表示
話題を追加