PowerShellでコマンド入力中に、ディレクトリ名が長すぎて困る・・・(´・ω・`)
Powershellの青い画面でコマンドを入力していると、カレントディレクトリ名が長すぎてスクロールしなくちゃならないことがあって嫌でした。
BashでいうところのPS1の変更ができたら、カレントディレクトリの次の行で入力したら便利になるのですが・・・。
prompt関数
PS1の代わりに、prompt関数を定義しておくといいらしいです。
なお、この関数が文字列を返さない場合は、PS>がでるそうです。
例1
ログインユーザー名、現在時刻、カレントディレクトリ、その次の行に入力できる場所、とするには、次のようにします。
関数定義:
powershell
function prompt {
Write-Host "$(whoami) $(get-date) " -BackgroundColor Blue -ForegroundColor White -NoNewline;
Write-Host "$(pwd)" -BackgroundColor Yellow -ForegroundColor Blue;
}
出力結果:
vm-windows81\hiroshi 03/20/2016 17:16:37 C:\Users\hiroshi
PS>
例2
上記とあまり変わりませんが、PS>の場所に現在時刻を移動させた場合です。
関数定義:
powershell
function prompt {
Write-Host "$(whoami) " -BackgroundColor Blue -ForegroundColor White -NoNewline;
Write-Host "$(pwd)" -BackgroundColor Yellow -ForegroundColor Blue;
return "$(get-date) > ";
}
出力結果:
vm-windows81\hiroshi C:\Users\hiroshi
03/20/2016 17:44:25 >
この関数を書いた後に保存しておく場所
.bashrcでexportする代わりに、
%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 にこの関数定義を書いとくと、この設定が自動で反映されます。
最後に
bashの環境変数のPS1は、何の略なんですかね。PowerShellの略ではなさそうですが。調べたら、"Prompt statement"の略だそうです。
ややこしいですね。意地悪して、わかりにくく書いたわけではないです。
参考
- about_Prompts -- https://technet.microsoft.com/ja-jp/library/hh847739.aspx
- Windows powershell: changing the command prompt -- http://stackoverflow.com/questions/5725888/windows-powershell-changing-the-command-prompt
- カレントディレクトリをウィンドウタイトルに表示してプロンプトを短くする -- http://qiita.com/kmr_hryk/items/424bb80e14f04e762578
- PowerShellでのプロファイル (.bashrcのようなもの) -- http://qiita.com/bitnz/items/400bb6a0b124b8b3d398
- Bash Shell: Take Control of PS1, PS2, PS3, PS4 and PROMPT_COMMAND -- http://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/