All Projects → tillig → ps-bash-completions

tillig / ps-bash-completions

Licence: other
Bridge to enable bash completions to be run from within PowerShell.

Programming Languages

shell
77523 projects
powershell
5483 projects

Projects that are alternatives of or similar to ps-bash-completions

Argcomplete
Python and tab completion, better together.
Stars: ✭ 942 (+1444.26%)
Mutual labels:  bash-completion
Bash It
A community Bash framework.
Stars: ✭ 12,671 (+20672.13%)
Mutual labels:  bash-completion
Sorlov.PowerShell
This is the public version of Sorlov.PowerShell Self-Hosted Executable Extensions for Powershell
Stars: ✭ 22 (-63.93%)
Mutual labels:  powershell-module
Dargs
Enhance any command with dynamic arguments
Stars: ✭ 107 (+75.41%)
Mutual labels:  bash-completion
Completion Ruby
Command-line completion for Ruby-related commands under Bash: rake, bundle, gem, rails, ruby, jruby
Stars: ✭ 129 (+111.48%)
Mutual labels:  bash-completion
Emacs Bash Completion
Add programmable bash completion to Emacs shell-mode
Stars: ✭ 217 (+255.74%)
Mutual labels:  bash-completion
Complete
bash completion written in go + bash completion for go command
Stars: ✭ 761 (+1147.54%)
Mutual labels:  bash-completion
HostsFileManagement
Hosts file management on Windows systems using PowerShell classes
Stars: ✭ 25 (-59.02%)
Mutual labels:  powershell-module
Tmux Bash Completion
Tmux bash completion
Stars: ✭ 153 (+150.82%)
Mutual labels:  bash-completion
PowerFGT
PowerShell module to manage Fortinet (FortiGate) Firewall
Stars: ✭ 80 (+31.15%)
Mutual labels:  powershell-module
Bash Completion Tutorial
Code of the bash completion tutorial
Stars: ✭ 110 (+80.33%)
Mutual labels:  bash-completion
Magento2 Bash Completion
Magento2 Bash Completion
Stars: ✭ 129 (+111.48%)
Mutual labels:  bash-completion
OutSystems.SetupTools
Powershell module to install and manage the OutSystems platform
Stars: ✭ 20 (-67.21%)
Mutual labels:  powershell-module
Vboxmanage Bash Completion
VBoxManage bash completion
Stars: ✭ 73 (+19.67%)
Mutual labels:  bash-completion
PowerVCF
PowerVCF: A PowerShell Module for VMware Cloud Foundation
Stars: ✭ 25 (-59.02%)
Mutual labels:  powershell-module
Dockerize
Substitua instruções de serviços por operações de contêineres
Stars: ✭ 21 (-65.57%)
Mutual labels:  bash-completion
Yarn Completion
Bash completion for Yarn
Stars: ✭ 210 (+244.26%)
Mutual labels:  bash-completion
bash.env
Bash.env is a cascading Bash environment system for those who work on different hardware and OS environments. Similar to oh-my-zsh but for Bash, and special sauce for those who work 'ssh' on remote machines.
Stars: ✭ 50 (-18.03%)
Mutual labels:  bash-completion
PSPasswordExpiryNotifications
Following PowerShell Module provides different approach to scheduling password notifications for expiring Active Directory based accounts. While most of the scripts require knowledge on HTML... this one is just one config file and a bit of tingling around with texts. Whether this is good or bad it's up to you to decide. I do plan to add an optio…
Stars: ✭ 38 (-37.7%)
Mutual labels:  powershell-module
PSRule.Rules.CAF
A suite of rules to validate Azure resources against the Cloud Adoption Framework (CAF) using PSRule.
Stars: ✭ 54 (-11.48%)
Mutual labels:  powershell-module

PowerShell Bridge for Bash Completions

Bridge to enable bash completions to be run from within PowerShell.

Commands like kubectl allow you to export command completion logic for use in bash. As these same commands get ported to Windows and/or used within PowerShell, porting the dynamic completion logic becomes challenging for the project maintainers. This project is an attempt to make a bridge so things "just work."

Installation

Prerequisites for Windows

Make sure you have bash.exe in your path or that you have Git for Windows installed. If bash.exe isn't in the path, the version shipping with Git for Windows will be used.

Prerequisites for MacOS

You need a newer bash than the one shipped with MacOS. If you haven't ever upgraded bash, likely you've still got 3.2 from 2007. Use Homebrew to get a newer version. brew install bash

From PowerShell Gallery

Install-Module -Name "PSBashCompletions"

Manual Install

  1. Put the PSBashCompletions folder in your PowerShell module folder (e.g., C:\Users\username\Documents\WindowsPowerShell\Modules or /Users/username/.local/share/powershell/Modules.
  2. Import-Module PSBashCompletions

Usage

  1. Locate the completion script for the bash command. You may have to export this like:

    ((kubectl completion bash) -join "`n") | Set-Content -Encoding ASCII -NoNewline -Path kubectl_completions.sh

    Make sure the completion file is ASCII. Some exports (like kubectl) come out as UTF-16 with CRLF line endings. Windows bash may see this as a binary file that can't be interpreted which results in no completions happening. Using join and Set-Content ensures the completions script will load in bash.

  2. Run the Register-BashArgumentCompleter cmdlet to register the command you're expanding and the location of the completions.

    Example:

    Register-BashArgumentCompleter "kubectl" C:\completions\kubectl_completions.sh
  3. If you use PowerShell aliases, register the completer for your aliases as well.

    Example:

    Set-Alias kc kubectl
    Register-BashArgumentCompleter "kc" C:\completions\kubectl_completions.sh

How It Works

The idea is to register a PowerShell argument completer that will manually invoke the bash completion mechanism and return the output that bash would have provided. Basically, that means:

  • A bridge script (bash_completion_bridge.sh) is used to load up the exported bash completions and manually invoke the completion functions.
  • From PowerShell, locate bash, locate the bridge script, and register a completer that ties those together.
  • When PowerShell invokes the completer, the completer arguments are taken and passed to the bash bridge.
  • The bash bridge executes the actual completion and returns the results, which are passed back through to PowerShell.

It won't be quite as fast as if it was all running native but it means you can use provided bash completions instead of having to re-implement in PowerShell.

Demo Expansions

In the Demo folder there are some expansions to try:

  • Register-KubectlCompleter.ps1 - Expansions for kubectl
  • Register-GitCompleter.ps1 - Expansions for git
  • Register-EchoTestCompleter.ps1 - Simple echo script that shows completion parameters in bash; use echotest as the command to complete and whatever else after it to simulate command lines.

Troubleshooting

There are lots of moving pieces, so if things aren't working there isn't "one answer" as to why.

First, run Register-BashArgumentCompleter with the -Verbose flag. When you do that, you'll get a verbose line that shows the actual bash.exe command that will be used to generate completions. You can copy that and run it yourself to see what happens.

The command will look something like this in Windows:

&"C:\Program Files\Git\bin\bash.exe" "/c/Users/username/Documents/WindowsPowerShell/Modules/PSBashCompletions/1.2.2/bash_completion_bridge.sh" "/c/completions/kubectl_completions.sh" "<url-encoded-command-line>"

On MacOS, it'll look like this:

&"/usr/local/bin/bash" "/Users/username/.local/share/powershell/Modules/PSBashCompletions/1.2.2/bash_completion_bridge.sh" "/Users/username/.config/powershell/bash-completion/kubectl_completions.sh" "<url-encoded-command-line>"

The last parameter is what you can play with - it's a URL-encoded version of the whole command line being completed (with %20 as space, not +).

Say you're testing kubectl completions and want to see what would happen if you hit TAB after kubectl c. You'd run:

&"C:\Program Files\Git\bin\bash.exe" "/c/Users/username/Documents/WindowsPowerShell/Modules/PSBashCompletions/1.0.0/bash_completion_bridge.sh" "/c/completions/kubectl_completions.sh" "kubectl%20c"

If it works, you'd see a list like:

certificate
cluster-info
completion
config
convert
cordon
cp
create

If it generates an error, that's the error to troubleshoot.

Common things that can go wrong:

  • Bash isn't found or the path to bash is wrong.
  • You have an old bash version and need to upgrade.
  • Your completion script isn't found or the path is wrong.
  • Your completion script isn't ASCII with \n line endings. UTF-16 doesn't get read as text by Windows bash; UTF-8 with a byte-order mark also sometimes causes problems. Even Windows \r\n line endings may cause trouble with parsing sometimes. Keeping it ASCII and \n is the safest way to ensure compatibility.
  • You have something in your bash profile that's interfering with the completions.
  • You're trying to use a completion that isn't compatible with the command on your OS. This happens with git completions - for example, on Windows you need to use the completion script that comes with Git for Windows, not the Linux version.
  • The completions rely on other commands or functions that aren't available/loaded. If the completion script isn't self-contained, things won't work. For example, the kubectl completions actually call kubectl to get resource names in some completions. If bash can't find kubectl, the completion won't work.

Add to Your Profile

After installing the module you can set the completions to be part of your profile. One way to do that:

  • Create a folder for completions in your PowerShell profile folder, like: C:\Users\username\OneDrive\Documents\WindowsPowerShell\bash-completion
  • Save all of your completions in there (e.g., kubectl_completions.sh).
  • Add a script block to your Microsoft.PowerShell_profile.ps1 that looks for bash (or Git for Windows) and conditionally registers completions based on that. (This will avoid errors if you sync your PowerShell profile to machines that might not have bash.)
$enableBashCompletions = ($Null -ne (Get-Command bash -ErrorAction Ignore)) -or ($Null -ne (Get-Command git -ErrorAction Ignore))

if ($enableBashCompletions) {
  Import-Module PSBashCompletions
  $completionPath = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($profile), "bash-completion")
  Register-BashArgumentCompleter kubectl "$completionPath/kubectl_completions.sh"
  Register-BashArgumentCompleter git "$completionPath/git_completions.sh"
}

Known Issues

Flags Don't Autocomplete

PowerShell doesn't appear to pass flags to custom argument completers. So, say you tried to do this:

kubectl apply -<TAB>

Note the TAB after the dash -. In bash you'd get completions for the flag like -f or --filename. PowerShell doesn't seem to call a custom argument completer for flags. (If you know how to make that work let me know!)

Recursive Command Calls Don't Work

Some completions (like kubectl) actually call themselves to generate completions. For example, if you do:

kubectl get pod -n <TAB>

...then kubectl will actually be called to go get the list of your namespaces to try to autocomplete the remote values for you. In cases like this, you may see that PowerShell will instead do something weird like duplicate the flag...

kubectl get pod -n -n

...or it may do nothing at all. If you run the troubleshooting command line, you may see an error message, possibly something cryptic like compopt: not currently executing completion function.

This happens because we're not actually in bash doing the completion, we're manually invoking the completion and the fake-out isn't deep enough. I don't know how to fix that, since kubectl or whatever might not actually be installed in bash for you - it may be a Windows/PowerShell thing. Even if it was, getting all the levels working is beyond my ken. (If you know how to make that work let me know!)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].