Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory Bookmarks in Powershell?

Tags:

powershell

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?

like image 777
tltjr Avatar asked Nov 02 '11 17:11

tltjr


3 Answers

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
like image 182
Simon Bergot Avatar answered Oct 23 '22 02:10

Simon Bergot


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?

like image 24
manojlds Avatar answered Oct 23 '22 03:10

manojlds


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

like image 4
Stadub Dima Avatar answered Oct 23 '22 04:10

Stadub Dima