Skip to content

Instantly share code, notes, and snippets.

@maciakl
Last active October 1, 2024 05:53
Show Gist options
  • Select an option

  • Save maciakl/b7f65bf40a1a78c06e6b0b058d76234f to your computer and use it in GitHub Desktop.

Select an option

Save maciakl/b7f65bf40a1a78c06e6b0b058d76234f to your computer and use it in GitHub Desktop.
My CLi bookmarking function for PowerShell. It allows you to store a current path as a bookmark and then quickly go back to it later.

Bookmarks for the CLI

This powershell function allows you to bookmark a path and then effortlessly return to it at a later time.

Usage

To use j, first browse to a desired location, then run:

j set 1

This will set the current path as bookmark 1.

You can then run:

j 1

This will jump to that bookmark. Alternatively you can run the command without any arguments:

j

This will print out a list and let you choose a bookmark to jump to.

You can overwrite any bokmark by simply setting it again. Note that bookmarks must be numerical.

To delete an existing bookmark run:

j delete 1

This will delete bookmark 1.

Prerequisties

The j script uses Skate for storing your bookmarks and Gum for UI.

To install the prerequisites, run:

winget install skate winget install gum

Installation

Copy and paste the function into your $PROFILE

function j {
if ($args.Length -eq 0) {
$answer = gum spin --show-output -- skate list "@j" | gum choose
$answer = $answer -split "`t"
$key = -join("", $answer[0], "@j")
$path = gum spin --show-output -- skate get $key
if ($path -ne "Key cannot be empty") {
cd $path
}
} else {
# check if first argument is a number
if ($args[0] -match '^\d+$') {
$num = -join("", $args[0], "@j")
$path = gum spin --show-output skate get $num
if ($path -ne "Key not found") {
cd $path
} else {
Write-Host "Bookmark not found"
return
}
} else {
if ($args[0] -eq 'set' -or $args[0] -eq 'delete' -and $args.Length -eq 2) {
# check if second argument is a number
if ($args[1] -match '^\d+$') {
if ($args[0] -eq 'set') {
gum spin -- skate set $key $path
$path = (Get-Location).Path
$key = -join("", $args[1], "@j")
gum spin -- skate set $key $path
} elseif ($args[0] -eq 'delete') {
$in = $args[1]
$key = -join("", $in, "@j")
$path = gum spin --show-output -- skate get $key
if ($path -ne "Key not found") {
$message = "Deleting bookmark $in`: $path"
gum style --foreground 212 $message
gum confirm
if ($?) { gum spin -- skate delete $key }
} else {
Write-Host "Bookmark $in not found"
return
}
}
} else {
Write-Host "Invalid argument: bookmark must be a number"
}
} else {
Write-Host "Invalid argument: try set, delete or a number"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment