One of my favorite Bash tips involves creating aliases for marking and returning to directories as described here: http://www.huyng.com/archives/quick-bash-tip-directory-bookmarks/492/.
In Bash, it looks like this:
alias m1='alias g1="cd `pwd`"'
Is it possible to create a similar function in powershell?
This project works very well: http://www.powershellmagazine.com/2016/05/06/powershell-location-bookmark-for-easy-and-faster-navigation/
Install:
Install-Module -Name PSBookmark
Use:
#This will save $PWD as scripts
save scripts
#This will save C:\Documents as docs
save docs C:\Documents
#You don't have to type the alias name.
#Instead, you can just tab complete. This function uses dynamic parameters.
goto docs
You can add the following to the $profile
:
$marks = @{};
$marksPath = Join-Path (split-path -parent $profile) .bookmarks
if(test-path $marksPath){
import-csv $marksPath | %{$marks[$_.key]=$_.value}
}
function m($number){
$marks["$number"] = (pwd).path
}
function g($number){
cd $marks["$number"]
}
function mdump{
$marks.getenumerator() | export-csv $marksPath -notype
}
function lma{
$marks
}
I didn't like the way of defining an alias for each like m1
, m2
and so on. Instead you will be doing m 1
and g 1
etc.
You can also add the line
Register-EngineEvent PowerShell.Exiting –Action { mdump } | out-null
so that it will do mdump
when you exit the Powershell session. Unfortunately, doesn't work if you close the console window, but when you type exit.
PS: Also have a look at CDPATH: CDPATH functionality in Powershell?
Long-time ago I created some module with such functionality. Now I think it quite stable to add a link to the Powershell gallery and the GitHub project :)
Install module function:
Install-Module -Name Bookmarks
Basic usage:
$pwd | ba -Name "BookmarkName" #add
bo BookmarkName #open
br BookmarkName #remove
bl #list
Entire functions list is listed on the GitHub project page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With