Colours with Linux terminals 日本語版 <author>Thorbjørn Ravn Andersen, <tt/ravn@dit.ou.dk/ <date>v1.4, 7 August 1997 <trans>佐藤亮一, GFG02131@niftyserve.or.jp, 鴨澤 眞夫, JCD00743@niftyserve.or.jp 共訳 <tdate>13 March 1999 <abstract> <!-- Most Linux distributions have a '<tt>ls</tt>' command for listing the contents of a directory that can visually enhance their output by using different colours, but configuring this to taste may not be a trivial task. This document explains the various aspects and approaches of altering the setup by configuring existing software, plus locations of alternative software usually not included with Slackware or RedHat, which may be used on most versions of Unix. --> Linux ディストリビューションのほとんどには、ディレクトリの内容を表示す る '<tt>ls</tt>' コマンドが同梱されています。様々な色を使うことで ls コマンドの出力を見やすくすることができるのですが、好みの設定にするのは 簡単ではないかもしれません。この文書では、ls のカラー設定の変更につい て様々な角度から解説しており、パッケージに含まれる ls の設定変更の他、 Slackware や RedHat には含まれていない ls 風ソフトウェア(これらは大抵 のバージョンのUnix でも使えます)のありかも記しました。 <!-- The HTML version is also available from my own source at <url url="http://www.mip.ou.dk/~ravn/colour-ls">. --> 本文書の HTML 版は筆者の WWW ページ <url url="http://www.mip.ou.dk/~ravn/colour-ls"> からも入手できます。 </abstract> <toc> <!-- --> <!-- <sect>Introduction<p> --> <sect>はじめに<p> <!-- In recent years colour displays have become very common, and users are beginning to exploit this by using programs that utilizes colours to give quick visual feedback on e.g. reserved keywords in programming languages, or instant notification of misspelled words. --> 近年はカラーディスプレイが非常に普及し、ユーザもこれを生かして色を利用 するプログラムを使うようになり始めました。このようなプログラムを使えば、 例えばプログラミング言語の予約語を見てすぐ分かるようにしたり、綴り間違 いのある単語をすぐに知ることができます。 <p> <!-- As the Linux text console supports colour, the original GNU ls was quickly modified to output colour information and included in Slackware around version 2.0. Improved versions of these patches have now migrated into the standard GNU distribution of ls, and should therefore be a part of all new Linux distributions by now. --> Linux のテキストコンソールがカラー表示をサポートしていたので、すぐに GNU ls がカラー情報を出力するように変更され、バージョン 2.0 あたりの Slackware に含まれるようになりました。これらのパッチの改良版は ls の GNU 標準配布に組み込まれたので、現在では新しい Linux ディストリビュー ション全てにカラー対応の ls が入っているはずです。 <p> <!-- This revision is an update on a major rewrite from the initial release, including information on xterms and kernel patching. --> この改訂版は、初版を全面的に改稿したもので、xterm の情報や、カーネルへ のパッチの情報も入れてあります。 <p> <!-- The information in this document has been confirmed on Redhat 4.1, and was originally compiled with the 2.0.2 release of Slackware, and the 1.1.54 kernel. The kernel patch information was retrieved on slackware 2.2.0 with the 1.2.13 kernel, and tcsh as the default shell, and later confirmed with a 2.0.27 kernel. If you use any other configuration, or unix version, I would appreciate a note stating your operating system and version, and whether colour support is available as standard. --> この文書に書かれている情報は RedHat 4.1 で動作確認しました。また、これ は元々、Slackware のリリース 2.0.2 とカーネル 1.1.54 でコンパイルして いました。カーネルパッチの情報は Slackware 2.2.0 とカーネル 1.2.13、 デフォルトのシェルが tcsh という組合せについて調べた情報であり、後で カーネル 2.0.27 でも確認しました。読者の皆さんが他の設定や他のバージョ ンの UNIX を使っておられる場合には、お使いのオペレーティングシステムと そのバージョン、標準でカラー表示がサポートされているかどうかを知らせて いただけると幸いです。 <!-- <sect>Quickstart for the impatient --> <sect>せっかちな人のための、すぐに使える方法 <p> <!-- If you have a <em>new</em> distribution of Linux, do these modifications to these files in your home directory. They take effect after next login. --> 読者の皆さんが<em>最近の</em> Linux ディストリビューションを使っておら れるなら、ホームディレクトリにある以下のファイルに対して以下に示す変更 を加えましょう。この変更は次回のログイン時から有効になります。 <tscreen><verb> ~/.bashrc: alias ls="ls ‐‐color" ~/.cshrc: alias ls 'ls ‐‐color' </verb></tscreen> <!-- That's it! --> これだけです! <!-- You may also want to do an ``<tt>eval `dircolors $HOME/.colourrc`</tt>'', to get your own colours. This file is created with ``<tt>dircolors -p >$HOME/.colourrc</tt>'' and is well commented for further editing. --> 自分専用の色の設定を行うために、``<tt>eval `dircolors $HOME/.colourrc`</tt>'' も行おうと思われるかもしれません。このファイルは ``<tt>dircolors -p >$HOME/.colourrc</tt>'' で生成され、後で編集できるように十分なコメントが付いています。 <!-- <sect>Do I have it at all? --> <sect>必要なものを自分は持っているの? <p> <!-- First of all you need to know if you have a version of <tt>ls</tt> which knows how to colourize properly. Try this command in a Linux text console (although an xterm will do): --> まず最初に、正しく色を付けることができる <tt>ls</tt> を持っているか調 べる必要があります。Linux のテキストコンソールでこのコマンドを試してく ださい(xterm でも大丈夫ですが): <tscreen><verb> % ls ‐‐color </verb></tscreen> <!-- (the <tt>%</tt> is a shell prompt): --> (<tt>%</tt> はシェルのプロンプトです): <!-- If you get an error message indicating that <tt>ls</tt> does not understand the option, you need to install a new version of the GNU fileutils package. If you do not have an appropriate upgrade package for your distribution, just get the latest version from your GNU mirror and install directly from source. --> <tt>ls</tt> がこのオプションを認識できないというエラーメッセージが出た ならば、新しいバージョンの GNU fileutils パッケージをインストールする 必要があります。お使いのディストリビューション用の適当なアップグレード 用パッケージが無い場合には、GNU のミラーサイトから最新バージョンを入手 し、ソースから直接インストールしましょう。 <!-- If you do <em>not</em> get an error message, you have a <tt>ls</tt> which understands the command. Unfortunately, some of the earlier versions included previously with Slackware (and possible others) were buggy. The <tt>ls</tt> included with Redhat 4.1 is version 3.13 which is okay. --> エラーメッセージが<em>出なければ</em>、このコマンドを認識する <tt>ls</tt> を持っているということです。しかし残念なことに、以前 Slackware(他のディストリビューションでも可能性があります)に入っていた 古いバージョンには、動作がおかしいものもあります。RedHat 4.1 に入って いる <tt>ls</tt> は問題の無いバージョン 3.13 です。 <tscreen><verb> % ls ‐‐version ls - GNU fileutils-3.13 </verb></tscreen> <!-- If you ran the ``<tt>ls ‐‐ color</tt>'' command on a Linux textbased console, the output should have been colourized according to the defaults on the system, and you can now decide whether there is anything you want to change. --> Linux のテキストコンソールで ``<tt>ls ‐‐ color</tt>'' コ マンドを実行したら、システムのデフォルト値にしたがって色が付いたはずで す。ここで変更したいものがあるかどうかを決めることができます。 <p> <!-- If you ran it in an xterm, you may or you may not have seen any colour changes. As with <tt>ls</tt> itself, the original xterm-program did not have any support of colour for the programs running inside of it, but recent versions do. If your xterm doesn't support colours, you should get a new version as described at the end of this document. In the meantime just switch to textmode and continue from there. --> このコマンドを xterm で実行した場合、色は変わったかもしれませんし、変 わっていないかもしれません。<tt>ls</tt> の場合と同様に、オリジナルの xterm プログラムは内部で動作するプログラムに対するカラー表示をサポート していなかったのですが、最近のバージョンの xterm はこれをサポートして います。お使いの xterm がカラー表示をサポートしていなければ、この文書 の最後で説明に従って新しいバージョンを入手しなければなりません。この場 合はとりあえずテキストモードに切り替えて、先に進んでください。 <!-- <sect>Which colours is there to choose from? --> <sect>どんな色が使えるか? <p> <!-- This shell script (thanks to the many who sent me bash versions) shows all standard colour combinations on the current console. If no colours appear, your console does not support ANSI colour selections. --> 以下のスクリプトは(bash 用のスクリプトを送付してくださった多くの方々に 感謝します)、使用中のコンソールが表示できる色を全て表示するためのもの です。色が出ない場合、お使いのコンソールは ANSI のカラー選択機能をサポー トしていません。 <p> <tscreen> <verb> #!/bin/bash # Display ANSI colours. # esc="\033[" echo -n " _ _ _ _ _40 _ _ _ 41_ _ _ _42 _ _ _ 43" echo "_ _ _ 44_ _ _ _45 _ _ _ 46_ _ _ _47 _" for fore in 30 31 32 33 34 35 36 37; do line1="$fore " line2=" " for back in 40 41 42 43 44 45 46 47; do line1="${line1}${esc}${back};${fore}m Normal ${esc}0m" line2="${line2}${esc}${back};${fore};1m Bold ${esc}0m" done echo -e "$line1\n$line2" done </verb> </tscreen> <!-- The foreground colour number is listed to the left, and the background number in the box. If you want bold characters you add a "1" to the parameters, so bright blue on white would be "37;44;1". The whole ANSI selection sequence is then <verb>ESC [ 3 7 ; 4 4 ; 1 m</verb> --> 画面の左側に前景色(文字の色)番号が、枠外上に背景色番号が表示されます。 文字をボールド(高輝度)で表示したい場合、パラメータに "1" を付け加えます。 つまり、白い背景に明るい青の文字を表示する場合、パラメータは "33;47;1m" となります。ANSI 選択全体としては <verb>ESC [ 3 7 ; 4 4 ; 1 m</verb> となります。 <!-- Note: The background currently cannot be bold, so you cannot have yellow (bold brown) as anything but foreground. This is a hardware limitation. --> 注意: 今のところ、背景を高輝度にすることはできません。これにより、 (高輝度の茶色である)黄色は、前景色以外では使えません。これはハードウェ アの制約です。 <!-- <verb>The colours are: 0 - black 4 - blue 3# is foreground 1 - red 5 - magenta 4# is background 2 - green 6 - cyan 3 - yellow 7 - white ;1 is bold </verb> --> <verb>色を以下に示します: 0 - 黒 4 - 青 3# は前景色 1 - 赤 5 - マゼンタ 4# は背景色 2 - 緑 6 - シアン 3 - 黄 7 - 白 ;1 はボールド </verb> <!-- <sect>How to configure colours with ls --> <sect>ls のカラー設定法 <p> <!-- If you wish to modify the standard colour set built into <tt>ls</tt>, you need your personal copy in your home directory, which you get with <tscreen><verb> cd ; dircolors -p > .coloursrc </verb></tscreen> --> <tt>ls</tt> に標準で組み込まれている色の設定を修正したければ、ホームディ レクトリに個人用の設定をコピーする必要があります。これは <tscreen><verb> cd ; dircolors -p > .coloursrc </verb></tscreen> で行うことができます。 <!-- After modifying this well-commented file you need to have it read into the environment string LS_COLORS, which is usually done with --> コメントが詳しく付いているこのファイルを変更した後は、環境変数 LS_COLORS にこの内容を読み込む必要があります。これは次のコマンドで行い ます: <tscreen><verb> eval `dircolors .colourrc` </verb></tscreen> <!-- You need to put this line in your .bashrc/.cshrc/.tcshrc (depending on your shell), to have it done at each login. See the dircolors(1) manual page for details. --> ログインする度にこのコマンドを実行させるためには、この行を .bashrc/.cshrc/.tcshrc (お使いのシェルによって変わります)に追加する必 要があります。詳しくはオンラインマニュアルの dircolors(1) をご覧くださ い。 <p> <!-- --> <!-- <sect>How to change the text-mode default from white-on-black<p> --> <sect>テキストモードのデフォルト表示を白黒以外に設定するには<p> <!-- You will need to tell the terminal driver code that you want another default. There exists no standard way of doing this, but in case of Linux you have the setterm program.<p> --> 使いたい新たなデフォルト値を端末ドライバに教えてやる必要があります。こ れを行うための標準の方法は存在しないのですが、Linux ならばプログラム setterm があります。<p> <!-- "setterm" uses the information in the terminal database to set the attributes. Selections are done like --> "setterm" は、端末データベースにある情報を使い、端末の属性を設定します。 選択は以下のように行います: <tscreen><verb> setterm -foreground black -background white -store </verb></tscreen> <!-- where the "-store" besides the actual change makes it the default for the current console as well. This requires that the current terminal (TERM environment variable) is described "well enough" in the termcap database. If setterm for some reason does not work, here are some alternatives: --> ここでは、実際の変更に加えて "-store" を指定することにより、これを現在 のコンソールのデフォルト値としても設定しています。 この機能を利用するためには、現在の端末(環境変数 TERM)が termcap データ ベースにきちんと記述されている必要があります。setterm が何らかの理由で 動作しない場合には、別の方法もあります: <sect1>xterm<p> <!-- One of these xterms should be available and at least one of them support colour. --> 以下の xterm のいずれかが利用できるはずで、少なくともこれらのいずれか はカラー表示をサポートしています。 <tscreen><verb> xterm -fg white -bg blue4 color_xterm -fg white -bg blue4 color-xterm -fg white -bg blue4 nxterm -fg white -bg blue4 </verb></tscreen> <!-- where 'color_xterm' supports the colour version of 'ls'. This particular choice resembles the colours used on an SGI. --> このうち、'color_xterm' はカラー対応の 'ls' をサポートしています。ここ で示した例では、SGI みたいなカラー設定にしています。 <!-- <sect1>Virtual console.<p> --> <sect1>仮想コンソール<p> <!-- You may modify the kernel once and for all, as well as providing a run-time default for the virtual consoles with an escape sequence. I recommend the kernel patch if you have compiled your own kernel. --> カーネルをキッパリ書き換えることもできますし、同時にエスケープシーケン スを使って仮想コンソールの実行時デフォルト値を与えることもできます。カー ネルをコンパイルしたことがある方には、カーネルパッチをお勧めします。 <p> <!-- The kernel source file is <tt>/usr/src/linux/drivers/char/console.c</tt> around line 1940, where you should modify --> カーネルソースのファイルは /usr/src/linux/drivers/char/console.c です。 このファイルの 1940 行目あたりに、 <tscreen><verb> def_color = 0x07; /* white */ ulcolor = 0x0f; /* bold white */ halfcolor = 0x08; /* grey */ </verb></tscreen> <!-- as appropriate. I use white on blue with --> とあるので、これを適当に変更するわけです。私は、 <tscreen><verb> def_color = 0x17; /* white */ ulcolor = 0x1f; /* bold white */ halfcolor = 0x18; /* grey */ </verb></tscreen> として、青地に白文字の設定にしてあります。 <!-- The numbers are the attribute codes used by the video card in hexadecimal: the most significant digit (the "1" in the example colours above) is the background; the least significant the foreground. 0 = black, 1 = blue, 2 = green, 3 = cyan, 4 = red, 5 = purple, 6 = brown/yellow, 7 = white. Add 8 to get "bright" colours. Note that, in most cases, a bright background == blinking characters, dull background. (From <url url="mailto:sjlam1@mda023.cc.monash.edu.au" name="sjlam1@mda023.cc.monash.edu.au">).<p> --> 上記の数字(16進数)はビデオカードが使う属性コードです。上のケタの数字 (私の設定では 1)は背景色、下のケタの数字は前景色(文字色)です。0=黒, 1= 青, 2=緑, 3=シアン, 4=赤, 5=紫, 6=茶色/黄色, 7=白となっています。「明 るい」色を使う場合は数字に 8 を加えてください。たいていの場合は 高輝度 の背景 == 文字が見にくい なので、良くない背景ということです (<url url="mailto:sjlam1@mda023.cc.monash.edu.au" name="sjlam1@mda023.cc.monash.edu.au"> さんより)。ご注意を。<p> <!-- You may also supply a new run-time default for a virtual console, on a per-display basis with the non-standard ANSI sequence (found by browsing the kernel sources) --> 標準でないエスケープシーケンス(カーネルソースを眺めていて発見しました) を使って、ディスプレイごとに仮想コンソールに対する実行時のデフォルト値 を設定することができます。このシーケンスは <tscreen><verb> ESC [ 8 ] </verb></tscreen> <!-- which sets the default to the current fore- and background colours. Then the Reset Attributes string (ESC [ m) selects these colours instead of white on black.<p> --> であり、現在の前景色と背景色のデフォルト値を設定します。それから属性再 設定文字列(ESC [ m)を使って白・黒以外の色を選択します。 <!-- You will need to actually echo this string to the console each time you reboot. Depending on what you use your Linux box for, several places may be appropriate: --> 実際にはマシンを再起動する度に、このシーケンスをコンソールに echo する 必要があります。Linux マシンをどのように使っているかにより、いろいろと やり方があります: <sect2>/etc/issue<p> <p> <!-- This is where "Welcome to Linux xx.yy" is displayed under Slackware, and that is a good choice for stand-alone equipment (and probably be a pestilence for users logging in with telnet). This file is created at boottime (Slackware in /etc/rc.d/rc.S; Redhat in /etc/rc.d/rc.local), and you should modify lines looking somewhat like --> Slackware が "Welcome to Linux xx.yy" の表示を行う部分です。スタンドア ローンのマシンなら、ここで echo をするのも良いでしょう(telnet で login するユーザーには有害かもしれませんが)。このファイルはブート時に 自動的に生成される(Slackware なら /etc/rc.d/rc.S で、RedHat なら /etc/rc.d/rc.local)ファイルで、以下のような部分を <tscreen><verb> echo ""> /etc/issue echo Welcome to Linux `/bin/uname -a | /bin/cut -d\ -f3`. >> /etc/issue </verb></tscreen> <!-- to --> から <tscreen><verb> ESCAPE="<replace with a single escape character here>" echo "${ESCAPE}[H${ESCAPE}[37;44m${ESCAPE}[8]${ESCAPE}[2J"> /etc/issue echo Welcome to Linux `/bin/uname -a | /bin/cut -d\ -f3`. >> /etc/issue </verb></tscreen> に変更しましょう。 <!-- This code will home the cursor, set the colour (here white on blue), save this selection and clean the rest of the screen. The modification takes effect after the next reboot. Remember to insert the _literal_ escape character in the file with C-q in emacs or control-v in vi, as apparently the sh used for executing this script does not understand the <tt>/033</tt> syntax. --> この命令は、カーソルをホームポジションに置き、カラー(青地に白文字)を設 定してセーブし、スクリーンの残りの部分をクリアします。この変更は、再起 動後に有効になります。このスクリプトを実行する sh は、\033 という書き 方を処理できないようなので、実際のエスケープキャラクターを挿入するのを 忘れないようにしてください。emacs なら C-q、vi なら C-v で入力できます。 <!-- <sect2>/etc/profile or .profile<p> --> <sect2>/etc/profile または .profile<p> <tscreen><verb> if [ "$TERM" = "console" ]; then echo "\033[37;44m\033[8]" # # or use setterm. setterm -foreground white -background blue -store fi </verb></tscreen> <!-- <sect2>/etc/login or .login<p> --> <sect2>/etc/login または .login<p> <tscreen><verb> if ( "$TERM" == "console" ) then echo "\033[37;44m\033[8]" # or use setterm. setterm -foreground white -background blue -store endif </verb></tscreen> <!-- <sect1>Remote login<p> --> <sect1>リモートログイン<p> <!-- You should be able to use the setterm program as shown above. Again, this requires that the remote machine knows enough about your terminal, and that the terminal emulator providing the login supports colour. In my experience the best vt100 emulation currently available for other platforms are: --> 先に示したように setterm が使えるはずです。繰り返しますが、あなたが使 っている端末のことを、ログイン先のマシンが十分に知っている必要がありま す。また、ログインに使う端末エミュレータも、カラー機能をサポートしてい ることが必要です。筆者の経験を基に、Linux 以外のプラットフォームで現在 のところもっともうまく VT100 エミュレーションができているものを示しま す: <itemize> <!-- <item>MS-DOS: MS-Kermit (free, not a Microsoft product) --> <item>MS-DOS: MS-Kermit (フリー、Microsoft 製品ではありません) <!-- <item>Windows 95/NT: Kermit/95 (shareware) --> <item>Windows 95/NT: Kermit/95 (シェアウェア) <!-- <item>OS/2: Kermit/95 (shareware). Note though that the standard telnet understands colours and can be customized locally. --> <item>OS/2: Kermit/95 (シェアウェア)。ただし、標準の telnet は色を使うことができ、ローカルでカスタマイズできることに注意してください。 </itemize> <!-- See <url url="http://www.columbia.edu/kermit/"> for details about Kermit.<p> --> Kermit の詳しい情報については、<url url="http://www.columbia.edu/kermit/"> をご覧ください。<p> <!-- <sect>Software<p> --> <sect>ソフトウェア<p> <!-- All the information described here is assuming a GNU/Linux installation. If you have something else (like e.g. a Sun running X or so) you can get and compile the actual software yourself. --> ここに記した情報は、すべて GNU/Linux をインストールしたマシンを想定して います。別のもの(そうですねぇ、例えば X が走ってる Sun とか)を使ってい る場合でも、自分でそれなりのソフトウェアを入手し、コンパイルすればよい でしょう。 <!-- The colour version of 'xterm' is based on the standard xterm source with a patch available from any X11R6 site. The xterm distributed with R6.3 is rumoured to have native colour support, but is untested by me. --> カラー版の 'xterm' は標準的な xterm のソースにパッチを当てたものです。 パッチはそこらの X11R6 サイトから取れます。R6.3 に入っている xterm は ネイティブでカラー表示をサポートしていると言われていますが、筆者はテス トしていません。 <tscreen> <verb> ftp://ftp.denet.dk/pub/X11/contrib/utilities/color-xterm-R6pl5-patch.gz </verb> </tscreen> <!-- See the documentation if you use an older version of X. <em>Note:</em> I haven't tried this myself! --> 古いバージョンの X を使っている方は、付属の文書を参照してください。 <em>注意:</em>私はテストしてないもん。 <p> <!-- 'ls' is in the GNU fileutils package available from prep.mit.edu or one of the several mirrors. Get at least version 3.13. --> `ls' は、GNU の fileutils パッケージに入っています。このパッケージは、 prep.mit.edu や、そのミラーサイトから取れます。少なくとも 3.13 を入手 してください。 <tscreen> <verb> ftp://ftp.denet.dk/pub/gnu/fileutils-3.XX.tar.gz </verb> </tscreen> <p> <!-- I have myself successfully compiled color-ls on Solaris, SunOS and Irix. --> 筆者は Solaris, SunOS, Irix でカラー版 ls のコンパイルに成功しています。 <p> <!-- I would appreciate feedback on this text. My e-mail address is <url url="mailto:ravn@dit.ou.dk" name="ravn@dit.ou.dk"> --> この文書に対するご指摘、感想、その他を大歓迎します。読んだ方はぜひぜひ メール下さい。鴨澤眞夫、 <url url="mailto:JCD00743@niftyserve.or.jp" name="JCD00743@niftyserve.or.jp"> と、佐藤亮一、 <url url="mailto:GFG02131@niftyserve.or.jp" name="GFG02131@niftyserve.or.jp"> がメールアドレスです。<p> 英語版文書へのフィードバックを歓迎します。e-mailアドレスは <url url="mailto:ravn@dit.ou.dk" name="ravn@dit.ou.dk">です。<p> ‐‐<p> <!-- Thorbjørn Ravn Andersen --> Thorbjørn Ravn Andersen, 佐藤亮一、鴨澤眞夫<p> (原文の更新に合わせ、藤原輝嘉 <url url="mailto:fujiwara@linux.or.jp" name="fujiwara@linux.or.jp"> が日本語訳を更新しました。) </article>