WindowsのPowerShellによく使うディレクトリのブックマークモジュールを追加
WindowsのCUI、ヘビーユーザでもないので特に探しもせずPowerShellを使ってる。あまり不満はないけど1つだけ、いくら補完があっても毎回cd C:\Users\hoge\Desktop\Dropbox\project\
とか書くのがキツいので、cdb project
とかでブックマークできるモジュールを入れた。元ネタはココ
こんな感じで使える
Set-Bookmark <name> <path> Get-Bookmark # to list all Get-Bookmark <name> Remove-Bookmark <name> Clear-Bookmarks Invoke-Bookmark <name>
Set-Bookmark
でブックマーク保存して、Invoke-Bookmark
で移動する。
Set-Alias cdb Invoke-Bookmark
とか設定すると、cdb project
などで可能。
やり方ざっくり
$profile
で自分のプロフィールパスを確認$profile/Microsoft.PowerShell_profile.ps1
がなければ作成$profile/Modules/bookmarks/
を作成(途中のディレクトリがなければ作成)$profile/Modules/bookmarks/bookmarks.psm1
を作成し、以下のスクリプトを記述
# bookmarks.psm1 # Exports: Set-Bookmark, Get-Bookmark, Remove-Bookmark, Clear-Bookmarks, Invoke-Bookmark # holds hash of bookmarked locations $_bookmarks = @{} function Get-Bookmark() { Write-Output ($_bookmarks.GetEnumerator() | sort Name) } function Remove-Bookmark($key) { <# .SYNOPSIS Removes the bookmark with the given key. #> if ($_bookmarks.keys -contains $key) { $_bookmarks.remove($key) } } function Clear-Bookmarks() { <# .SYNOPSIS Clears all bookmarks. #> $_bookmarks.Clear() } function Set-Bookmark($key, $location) { <# .SYNOPSIS Bookmarks the given location or the current location (Get-Location). #> # bookmark the current location if a specific path wasn't specified if ($location -eq $null) { $location = (Get-Location).Path } # make sure we haven't already bookmarked this location (no need to clutter things) if ($_bookmarks.values -contains $location) { Write-Warning ("Already bookmarked as: " + ($_bookmarks.keys | where { $_bookmarks[$_] -eq $location })) return } # if no specific key was specified then auto-set the key to the next bookmark number if ($key -eq $null) { $existingNumbers = ($_bookmarks.keys | Sort-Object -Descending | where { $_ -is [int] }) if ($existingNumbers.length -gt 0) { $key = $existingNumbers[0] + 1 } else { $key = 1 } } $_bookmarks[$key] = $location } function Invoke-Bookmark($key) { <# .SYNOPSIS Goes to the location specified by the given bookmark. #> if ([string]::IsNullOrEmpty($key)) { Get-Bookmarks return } if ($_bookmarks.keys -contains $key) { Push-Location $_bookmarks[$key] } else { Write-Warning "No bookmark set for the key: $key" } } Export-ModuleMember Get-Bookmark, Remove-Bookmark, Clear-Bookmarks, Set-Bookmark, Invoke-Bookmark
$profile/Microsoft.PowerShell_profile.ps1
以下のスクリプトを記述(ショートカットは自由に書き換えてOK)
# Microsoft.PowerShell_profile.ps1 Import-Module bookmarks.psm1 Set-Alias cdb Invoke-Bookmark Set-Bookmark project c:\path\to\my\project
スクリプトの実行権限がある人はPowerShellを再起動して完了。
再起動時に実行権限がないと警告される場合は、
Set-ExecutionPolicy RemoteSigned
する。
http://windows-podcast.com/sundayprogrammer/archives/95
Set-ExecutionPolicy
でレジストリ編集権限がないと言われる人は、
管理者権限でPowerShellを再起動(右クリック→管理者として起動)して再度行う。