目前算是都會碰到 big5/utf8 的時代,我原本有在 ~/.screenrc 底下,使用下面的技術,以便在 big5 的 terminal 裡看 utf8,或在 utf8 的 terminal 裡看 big5:

如果 terminal 使用 big5:

# C-a b --(switch to)--> view big5 data
bind b encoding big5 big5
# C-a u --(switch to)--> view utf8 data
bind u encoding utf8 big5 # convert utf8 to big5

如果 terminal 使用 utf8:

# Same as the encoding command except that the default  setting  for  new
# windows is changed. Initial setting is the encoding taken from the ter-
# minal.
defencoding utf8
# C-a b --(switch to)--> view big5 data
bind b encoding big5 utf8 # convert big5 to utf8
# C-a u --(switch to)--> view utf8 data
bind u encoding utf8 utf8

在這樣的設定下,不管我是用 utf8 terminal 還是 big5 terminal,當我要看 big5 的資料時,我隨時可以按 Ctrl-A 再按 B,切到 big5 模式;反之,當我要看 utf8 的資料時,就按 Ctrl-A 再按 U,切到 utf8 模式。

因為我有使用 Subversion 管理我所有的設定檔,這樣就可以在多台機器裡,共享同一份設定,享用相同的開發環境,並在初到新機時,可以瞬間完成設定,變成我用起來最舒服的模樣。然而,這卻造成了一個問題:在有些機器裡,我已經改成全 utf8 環境,但還有些機器,還在使用 big5 環境。因此,面對不同的環境,必須適用上述兩種 screenrc 設定的其中一種。

然而,僅僅為了這一點點的不同,而去維護兩份幾乎完全一樣的 screenrc 設定檔,很顯然地是不恰當地,會有嚴重的維護問題存在[1]

第一個想法是,將相同的部份放在一個檔裡,不同的部份,則依照環境,選擇不同的設定法,附加到 ~/.screenrc 裡。不過後來我採用的是另外一種解法:先將 screenrc 寫成如下:

# if (using utf8) {
#<enc="utf8">   # Same as the encoding command except that the default  setting  for  new
#<enc="utf8">   # windows is changed. Initial setting is the encoding taken from the ter-
#<enc="utf8">   # minal.
#<enc="utf8">   defencoding utf8
#<enc="utf8">   # C-a b --(switch to)--> view big5 data
#<enc="utf8">   bind b encoding big5 utf8 # convert big5 to utf8
#<enc="utf8">   # C-a u --(switch to)--> view utf8 data
#<enc="utf8">   bind u encoding utf8 utf8
# } else {
#<enc="big5">   # C-a b --(switch to)--> view big5 data
#<enc="big5">   bind b encoding big5 big5
#<enc="big5">   # C-a u --(switch to)--> view utf8 data
#<enc="big5">   bind u encoding utf8 big5 # convert utf8 to big5
# }

奧妙就在 #<enc="utf8">#<enc="big5"> 的字樣。我們只要在安裝時,依據環境,將 #<enc="utf8">#<enc="big5"> 刪掉,就可以將對應的設定露出,發揮效用。假設 $ZHTW_CHARSET 依照環境,可能是 UTF-8Big5[2],安裝的程式可以這麼寫:

rm -f ~/.screenrc;
if [ "x$ZHTW_CHARSET" = 'xUTF-8' ]; then
    cat "dot.screenrc" | sed -e 's/^#<enc="utf8">//' > ~/.screenrc;
elif [ "x$ZHTW_CHARSET" = 'xBig5' ]; then
    cat "dot.screenrc" | sed -e 's/^#<enc="big5">//' > ~/.screenrc;
else
    cp -f "dot.screenrc" ~/.screenrc;
fi;

我們使用 sed,將 #<enc="utf8">#<enc="big5"> 刪除。條件判斷裡的 "x$ZHTW_CHARSET" = 'xUTF-8',之所以多加了一個 x,是為了防止若 $ZHTW_CHARSET 不存在或是空字串的情況。

假設我們現在使用的是 utf8 的環境,經過安裝後,~/.screenrc 就會長這個樣子:

# if (using utf8) {
   # Same as the encoding command except that the default  setting  for  new
   # windows is changed. Initial setting is the encoding taken from the ter-
   # minal.
   defencoding utf8
   # C-a b --(switch to)--> view big5 data
   bind b encoding big5 utf8 # convert big5 to utf8
   # C-a u --(switch to)--> view utf8 data
   bind u encoding utf8 utf8
# } else {
#<enc="big5">   # C-a b --(switch to)--> view big5 data
#<enc="big5">   bind b encoding big5 big5
#<enc="big5">   # C-a u --(switch to)--> view utf8 data
#<enc="big5">   bind u encoding utf8 big5 # convert utf8 to big5
# }

大功告成,搞定收工。額外的好處是,光看安裝後的 ~/.screenrc,不靠安裝程式,也可以手動切換成 big5 模式。這是附加不同設定法,或是維護兩個版本,所無法達到的效果。


  1. 我的 ~/.tcshrc 也有兩個版本:UNIX 與 Win32 版,也有同樣的問題。
  2. 我在 cshrc 裡用這個變數,組成 LC_ALL 的值。