kelleyma49 / PSFzf

A PowerShell wrapper around the fuzzy finder fzf
MIT License
787 stars 34 forks source link

[Feature request] Set the current location as the parent of the user's selection file in fzf #72

Closed pluma9 closed 2 years ago

pluma9 commented 3 years ago

I would like to have the ability to fuzzy search a file and have the current working location set to the directory that contains the selected file.

For example, I have a directory structure like this:

├───backend
│   data.json │   main.cs

└───frontend
│   data.json │   main.js └───... There are times when I want to go to the backend directory to work on a few backend related files and I don't remember the name of the directory, but know the directory contains a file called main.cs. It will be helpful if there's a new command or a new parameter added to one of the existing commands that allows me to fuzzy search the main.cs file and then set the current working directory to backend.

I think this is a common use case that other people may also need.

mattcargile commented 2 years ago

You could make a function.

function Set-FuzzyParentDirectory { 
Set-Location ( Split-Path ( ( cmd.exe /S /C dir /B /S ) | Invoke-Fzf ) -Parent )
}

Then you can combine with a Get-PSReadlineKeyHandler.

pluma9 commented 2 years ago

Thanks for your idea. I've ended up with a slightly different but equivalent function:

function Set-FuzzyParentDirectory {
  Set-Location -LiteralPath (Split-Path (fzf) -Parent)
}
mattcargile commented 2 years ago

Glad you got it working! There is also a new feature for ALT + C where you can have similar behavior as this function. You can replace the function from Set-Location to your iteration. I may incorporate it into my workflow.

https://github.com/kelleyma49/PSFzf/issues/88#issuecomment-1025042065