psfzf

repository·master·Indexed 22 days ago

https://github.com/kelleyma49/psfzf

A PowerShell module that provides a fuzzy-finding interface by wrapping the fzf command-line tool. It integrates with PSReadline to enhance command history, path selection, and tab completion. Key cmdlets include Invoke-Fzf for general fuzzy finding, Invoke-FuzzyEdit for file editing, Invoke-FuzzyHistory for command execution, and specialized integrations for Git status, Scoop, ripgrep, ZLocation, and fasd/fasdr.

Tokens
7.2K
Snippets
31
Records
42
Agent score
75%

What's inside psfzf

  1. Set PSReadline keyboard chords

    master

    You can customize the keyboard shortcuts used by PSReadline to trigger PSFzf features using the following parameters:

    • -PSReadlineChordProvider: Sets the chord to trigger file and directory selection (e.g., 'Ctrl+t').
    • -PSReadlineChordReverseHistory: Sets the chord to trigger history selection (e.g., 'Ctrl+r').

    Example: Setting custom chords for provider and reverse history:

    Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
    Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
  2. Use Invoke-FuzzyEdit to edit selected files

    master

    The Invoke-FuzzyEdit cmdlet launches fzf to allow you to pick one or multiple files. Once files are selected, they are opened in your default editor.

    Editor Selection Logic:

    • Uses the editor defined in the $env:EDITOR environment variable.
    • On Windows: If $env:EDITOR is not defined, it defaults to Visual Studio Code.
    • On non-Windows systems: If $env:EDITOR is not defined, it defaults to vim.
    • VS Code Integration: If running inside a Visual Studio Code integrated terminal, the command automatically uses the --reuse-window option to ensure files open in the currently active window.
    Invoke-FuzzyEdit
  3. Basic Usage of Invoke-Fzf

    master

    The core function Invoke-Fzf can be used within a pipeline to filter items and perform actions on the selection.

    Change to a selected directory:

    Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location

    Edit a selected file:

    Get-ChildItem . -Recurse -Attributes !Directory | Invoke-Fzf | % { notepad $_ }

    Pipeline usage: Invoke-Fzf works with input from a pipeline and can be used in the middle of a pipeline or as part of an expression.

    Get-ChildItem . -Recurse | ? { $_.PSIsContainer } | Invoke-Fzf | Set-Location
  4. Enable PSReadline integration for fuzzy history search

    master

    To improve the history search experience, you can integrate fzf directly into PSReadline. This allows you to trigger the fuzzy history search using standard keyboard chords (like Ctrl+r) instead of calling the cmdlet manually.

    Use the Set-PsFzfOption cmdlet with the -PSReadlineChordReverseHistory parameter to enable this integration.

    Set-PsFzfOption -PSReadlineChordReverseHistory
  5. Set current location using Set-LocationFuzzyEverything

    master

    The Set-LocationFuzzyEverything cmdlet allows you to interactively select a directory from the Everything database using fzf and immediately change your current PowerShell location to that directory.

    Note: This function is only available if the PSEverything module is installed and accessible in your PowerShell session.

    Set-LocationFuzzyEverything
  6. Use Invoke-FuzzyZLocation to select a directory

    master

    The Invoke-FuzzyZLocation cmdlet allows you to interactively select a directory from your ZLocation history database using fzf. Once a directory is selected, the cmdlet automatically sets your current location (working directory) to that selection.

    Note: This function is only available if ZLocation is installed and accessible in your PowerShell session.

    # Launch fzf with ZLocation history and set the location upon selection
    Invoke-FuzzyZLocation
  7. Use Invoke-FuzzyScoop to manage Scoop applications

    master

    The Invoke-FuzzyScoop cmdlet provides a fuzzy-search interface (via fzf) to select one or more applications from your local Scoop buckets. Once applications are selected, it executes a specified Scoop subcommand on them.

    Prerequisite: Scoop must be available in your system's PATH for this function to work.

    # Default behavior: Select applications to install
    Invoke-FuzzyScoop
    
    # Select applications to uninstall
    Invoke-FuzzyScoop -subcommand uninstall
  8. Use Invoke-Fzf to start the fuzzy finder

    master

    Invoke-Fzf starts an interactive fuzzy finder based on input provided via the pipeline or the -Input parameter. It returns the objects selected by the user.

    Common Usage Pattern

    You can wrap Invoke-Fzf in other commands to act on the selection. For example, to change your current directory to a directory selected via the fuzzy finder:

    Set-Location (Invoke-Fzf)
  9. Use PSFzf convenience functions for common tasks

    master

    PSFzf provides several specialized functions (and their short aliases) to perform specific workflows quickly:

    • File Editing: Invoke-FuzzyEdit (fe) - Select a file to edit.
    • Frecent Files: Invoke-FuzzyFasd (ff) - Select a frecent file using Fasdr.
    • Directory Navigation: Invoke-FuzzyZLocation (fz) - Select a recent directory using ZLocation.
    • Git Integration: Invoke-FuzzyGitStatus (fgs) - Select files from git status.
    • History Search: Invoke-FuzzyHistory (fh) - Invoke previous history.
    • Process Management: Invoke-FuzzyKillProcess (fkill) - Kill processes.
    • Location Management: Invoke-FuzzySetLocation (fd) - Set the current location.
    • Scoop Integration: Invoke-FuzzyScoop (fs) - Select Scoop applications.
    • Everything Integration: Set-LocationFuzzyEverything (cde) - Select a file or folder from Everything.
  10. Enable and Configure Tab Expansion

    master

    PSFzf can replace standard tab completion with a fuzzy interface.

    1. Enable Tab Expansion:
      Set-PsFzfOption -TabExpansion
    2. Replace Tab Key Handler:
      Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }

    Supported Specialized Commands: After typing the trigger (default is **) and pressing Tab, you can use:

    • git: Requires posh-git (v1.0.0 Beta 4+).
    • Get-Service, Start-Service, Stop-Service: Select between installed services.
    • Get-Process, Start-Process: Select between running processes.

    Customizing the Preview Window: The preview window can be toggled with Ctrl+/. Use Set-PsFzfOption -TabCompletionPreviewWindow to set the initial state and the cycle order using the format initial_state|change_preview_window_options (e.g., right|down|hidden).

    # Enable tab expansion
    Set-PsFzfOption -TabExpansion
    
    # Start with visible preview on the right
    Set-PsFzfOption -TabCompletionPreviewWindow 'right|down|hidden'
  11. Use Invoke-FuzzyFasd to select files from fasd/fasdr

    master

    The Invoke-FuzzyFasd cmdlet allows you to interactively select a file or directory from your fasd (non-Windows) or fasdr (Windows) database using fzf. Once a selection is made, the cmdlet automatically sets your current working directory to the location of the selected item.

    Prerequisite: This function is only created if fasdr is discoverable by PowerShell.

    Invoke-FuzzyFasd