Terminal.app
目次
ターミナル(Terminal)とは?
Terminal.app(ターミナル.app)は、Mac OS X にバンドルされている、ターミナルエミュレータと呼ばれる種類のアプリケーションプログラムです。 Mac OS X で UNIX 的機能を使用したいときには、このターミナルを利用します。 /Applications/Utilities (アプリケーション/ユーティリティ)フォルダの中にインストールされています。
操作
- スクロールバッファのキーボードショートカット
- ページアップ(ダウン):⇞(⇟) , ノートはfn+↑(↓)
- 上端(下端)までスクロール: ↖(↘) , home(end)、ノートはfn+←(→)
- 1行上(下)へ:⌘↑(↓)
- スクロールバッファを消去:⌘K
- ウィンドウの切り替え
- ⌘数字で、ウィンドウが切り替えられます。ターミナルインスペクタの「ウィンドウ」で「コマンドキー」にチェックを入れるとウィンドウのタイトル部分に数字が表示されます。
- ⌘←(→) で、ウィンドウを順番に行き来することができます。
トラブルシューティング
デフォルトのログインシェルを変更したい場合
Mac OS X 10.4 の NetInfo マネージャでデフォルトのログインシェルを変更 (/usr/local/bin/tcsh にインストールした tcsh-6.14) したところ、 「このアプリケーションの実行が認証されていません。管理者が不正なシェル値を設定しています。」 という警告が表示され、ターミナルが起動できなくなりました。
これはターミナルが /etc/shells に書かれた設定を読み込み、そこに書かれたシェルのみを起動するようになったためです。
このようなエラーが出た場合、デフォルトのログインシェルを /bin/bash なり /bin/tcsh に戻した上で、ターミナルを起動し,pico /etc/shells として
/usr/local/bin/tcsh
の一行を追加し、再びデフォルトのログインシェルを /usr/local/bin/tcsh にすることで、ログインシェルを変更した状態でターミナルを起動できるようになります。 一般に、10.4 において、ログインシェルを自分でビルドした hoge (/usr/local/bin にインストールしてあるものとする) に変更する場合、/etc/shells に
/usr/local/bin/hoge
と書いておく必要があります。
特定の文字を扱う際に画面が乱れる
Mac OS X 10.4 までの Terminal.app では、文字エンコードを UTF-8 に設定している場合、特定の記号類を用いた際に画面の表示が乱れることがあるようです。
この問題が気になる場合には、Mac OS X 10.5 にアップグレードするか、uxterm や rxvt-unicode (urxvt) など、より厳密な動作の期待できる他のターミナルソフトの利用をお奨めします。 uxterm は Apple の X11 に標準で付いています。また、rxvt-unicode は MacPorts や Fink で導入できます。
新規ウィンドウを開く際の自動実行コマンド
~/Library/Preferences/com.apple.Terminal.plist の内部の "ExecutionString" 欄に設定されたコマンドは,Terminalの新規ウィンドウを開く際に自動実行されます.
AppleScript
カレントディレクトリをFinderで開く
tell application "Terminal" activate if (count (every window whose visible is true)) = 0 then return do script "open ." in front window end tell
tell application "Terminal" activate if (count (every window whose visible is true)) = 0 then return do script "pwd" in front window -- pwd コマンドを実行 -- delay 2 -- 環境によっては pwd コマンドの実行結果の表示を待つ set theContents to contents in front window set strList to paragraphs of theContents set idxMax to length of strList repeat with idx from idxMax to 1 by -1 if (item idx of strList) is not "" then set idxPwd to idx - 1 exit repeat end if end repeat set currDir to item idxPwd of strList -- 表示内容の最終行の文字列(pwd コマンドの実行で表示される文字列) set currDir to currDir as Unicode text do script ("open " & currDir) in front window end tell
参考:
- ターミナルのcurrentをFinderでひらく - AS Hole(AppleScriptの穴)(2008-03-29)
- Terminal.appで指定コマンドを実行する - AS Hole(AppleScriptの穴)(2010-12-15)
前面のウィンドウの表示内容から、最終行の1つ上の行だけを文字列としてクリップボードにコピーする
ただし,このスクリプトでうまく動作しない人もいるようなので,下記もご参照.
set LF to (ASCII character 10) tell application "Terminal" set x to contents of front window as text end tell set lastDelimit to AppleScript's text item delimiters set AppleScript's text item delimiters to LF set x to every text item of x set AppleScript's text item delimiters to lastDelimit set n to the count of x set the clipboard to the item (n - 2) of x
contentsで得られる表示内容は、なぜか最後に空行がいくつか付加されてしまうので、上記のスクリプトではうまく動作しないのではないでしょうか?(Mac OS X 10.5.8で確認)- MebaR 「カレントディレクトリをFinderで開く」のスクリプトとほとんど同じですが、以下のようなものはどうでしょうか?- MebaR
tell application "Terminal" if (count (every window whose visible is true)) = 0 then return set theContents to contents of front window end tell set strList to paragraphs of theContents set idxMax to length of strList repeat with idx from idxMax to 1 by -1 if (item idx of strList) is not "" then set idxLast to idx - 1 exit repeat end if end repeat set strSecondLast to item (idxLast - 1) of strList -- 表示内容の最終行の1つ上の行の文字列 set the clipboard to strSecondLast
Finderのフォルダーをターミナルで開く
Finder上で選択したフォルダー(のディレクトリ)をターミナルで開く,もしくはディレクトリ移動等するための各種方法.
- Finderからフォルダー/ファイルアイコンをターミナルウィンドウへdrag&dropするとそのパスがpasteされます.
- 「ターミナルで開く」(Automator アクション) http://www.fan.gr.jp/~sakai/unix.php
- OpenTerminalHere (Finderのツールバー用) http://prototypecreative.com/blog/2008/01/openterminalhereapp-update/ リンク切れ
- Terminal Here Plugin (コンテクストメニュー, 10.4以前で動作) http://www.pyehouse.com/lynn/termopen.php リンク切れ
- AppleScript を利用する http://piyocast.com/as/archives/92
- cdto http://code.google.com/p/cdto/
- 下記のシェルスクリプトをsourceコマンドで動かす.~/.bash_profile内でコマンドエイリアス設定するならば,
alias コマンドエイリアス名='source このシェルスクリプトパス'
#!/bin/sh cd $( echo " tell application \"Finder\" set theItem to selection if number of theItem is 0 then set theItem to (target of window 1) else set theItem to item 1 of theItem end if if (class of theItem) is not folder then set theItem to container of theItem end if end tell get POSIX path of (theItem as string) " |osascript - ) pwd
ファイルのディレクトリをターミナルで開く
- DTerm: 一行型専用ターミナルが開かれる(キーボードショートカットで).Finder最上面表示のディレクトリ,もしくはwindow title bar機能付きアプリケーションで開いているファイルが対象. http://www.decimus.net/dterm.php