shtaxxx日記

コンピュータアーキテクチャについて研究している研究者の日記や技術紹介

screenでタイトルを,コマンド実行中はコマンド名に,でなければ現在のディレクトリ名に自動で設定する

以前にscreenのhardstatusに表示されるウィンドウタイトルを現在実行中のコマンドに設定する方法の記事を書いたが,今回はその更新版.


screenの各ウィンドウのタイトルを,コマンド実行中はそのコマンド名に自動で設定し,そうでなければ現在の作業ディレクトリ名に自動で設定する.

bashtcshの2つのシェルについてその方法は以下の通り.
zshの場合にはおそらく,tcshの場合と同じようにprecmdを設定すれば,同様のことができるはず(未確認).

設定するとこんな感じで,screenのtitleが自動で設定される.
ウィンドウ1ではemacsを,2ではtopをそれぞれ実行中.3では現在workディレクトリにいて,コマンドは実行していない.

bashの場合,PS1とPROMPT_COMMAND変数を設定する.

## .bashrc
if [ $TERM == 'screen' ]; then
    export PS1='\u@\h:\W\$ '
    export PROMPT_COMMAND='echo -ne "\033k\033\0134\033k$(basename $(pwd))\033\\"'
else
    export PS1='\u@\h:\W\$ '
fi

tcshの場合,promptとprecmd変数を設定する.
echo_styleをbothに設定しないとMacなどのBSD系の環境で正しく動作しないため注意.

## .cshrc
if ( $TERM == 'screen' ) then
    set echo_style=both
    set prompt="%n@%m:%c1$ "
    alias precmd 'echo -n "\033k\033\0134\033k`basename ${cwd}`\033\\"'
else
    set prompt="%n@%m:%c1$ "
endif

ちなみに.screenrcはこんな感じ.

## .screenrc
startup_message off

## current command name as window name
shelltitle "$ |bash"

#hardstatus alwayslastline "%-w%{= ck}%n %t%{-}%+w %=%H [%Y-%m-%d %02c:%s]"
hardstatus alwayslastline "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %Y %m/%d %C:%s%a"
hardstatus off

escape ^z^z

termcap * 'G0:S0=\E(%.:E0=\E(B:TY=iso2022'
termcapinfo xterm* ti@:te@
autodetach on
defflow off
crlf off
vbell off
vbell_msg "Bell"

defutf8 on
defkanji utf-8
encoding utf-8 utf-8
defencoding utf-8

bind o focus
bind k kill
bind s split

screen 0
scrollback 4000
screen 1
scrollback 4000