コマンドプロンプトを設定するために.bashrcをカスタマイズする方法は?

コマンドプロンプトを設定するために.bashrcをカスタマイズする方法は?

表示される色とテキストを変更するためにBashプロンプトを変更する簡単な方法はありますか?でこれが完了したと聞きましたが、.bashrc修正する簡単で良い方法が見つかりませんでした。 Bashで色を表現する方法は?

ベストアンサー1

私はこれを私で定義しました.bashrc

export e="\e["

function cls          { echo -n "${e}H${e}J${e}0m"; }
function rcls         { echo -n "${e}H${e}J${e}S${e}H${e}J${e}0m${e}u${e}J"; } # not quite yet !
# rcls only works when no funny codes have been issued in between, i.e. the buffer is clean
# below the current screen. And then there can be issues with the first line of the buffer.

function bright       { echo -n "${e}1m"; }
function normal       { echo -n "${e}0m"; }
function colour       { echo -n "${e}$1;$2;$3m"; }
function black        { echo -n "${e}30m"; }
function bg_black     { echo -n "${e}40m"; }
function red          { echo -n "${e}31m"; }
function bg_red       { echo -n "${e}41m"; }
function green        { echo -n "${e}32m"; }
function bg_green     { echo -n "${e}42m"; }
function yellow       { echo -n "${e}33m"; }
function bg_yellow    { echo -n "${e}43m"; }
function blue         { echo -n "${e}34m"; }
function bg_blue      { echo -n "${e}44m"; }
function magenta      { echo -n "${e}35m"; }
function bg_magenta   { echo -n "${e}45m"; }
function cyan         { echo -n "${e}36m"; }
function bg_cyan      { echo -n "${e}46m"; }
function white        { echo -n "${e}37m"; }
function bg_white     { echo -n "${e}47m"; }
function c_up         { echo -n "${e}$1A"; }
function c_down       { echo -n "${e}$1B"; }    # within
function c_right      { echo -n "${e}$1C"; }
function c_left       { echo -n "${e}$1D"; }    # screen
function c_pos        { echo -n "${e}$1;$2f"; } # [Hf]
function c_line       { echo -n "${e}$1d"; }
function screentop    { echo -n "${e}H"; }      # [Hdf]
function linetop      { echo -n "${e}G"; }
function buffertop    { echo -n "${e}u"; }      # depends on other control characters ?
function tab          { echo -n "${e}I"; }
function backtab      { echo -n "${e}Z"; }
function scrolldown   { echo -n "${e}$1T"; }    # within screen
function scrolldown_B { echo -n "${e}$1L"; }    # scroll down below cursor
function scrollup_B   { echo -n "${e}$1M"; }    # scroll up below cursor
function clear        { echo -n "${e}J"; }      # delete to end of screen
function cleanbuffer  { echo -n "${e}S"; }      # copies first screen to bottom and clears
                                                # everything else above the cursor.
#function xxx { echo -n "${e}xxx"; }

export -f bright normal colour
export -f black bg_black red bg_red green bg_green yellow bg_yellow
export -f blue bg_blue magenta bg_magenta cyan bg_cyan white bg_white
export -f c_up c_down c_right c_left c_pos c_line
export -f screentop linetop buffertop tab backtab
export -f scrolldown scrolldown_B scrollup_B clear cleanbuffer

その後、次のように使用できます。

PS1_init="\n$(bright)$(black)$(hostname):\w\n$(bg_blue)"
PS1_end="$(normal)$(bright)\n\! -> $(normal)$(clear)"
export PS1="$PS1_init"'$(date)'"$PS1_end"

これはあなたに役立ちます。

変数の代わりに関数に変更した理由は怠惰です。ただタイピングをやめたいです。もちろん、効率変数を探してみると良いでしょう。

最終的に、これは特定の端末にのみ適しています。したがって、ヘルプが必要な場合は、bashや他のシェル文書ではなく端末文書を参照してください。

おすすめ記事