Skip to content

Instantly share code, notes, and snippets.

@st0le
Last active February 11, 2021 06:47
Show Gist options
  • Save st0le/820677e5fb99db5b16c7ae1edd5c05fa to your computer and use it in GitHub Desktop.
Save st0le/820677e5fb99db5b16c7ae1edd5c05fa to your computer and use it in GitHub Desktop.
Lightweight git autocompletion.
# Basic Autocompleter for git
# Covers most usecases
Register-ArgumentCompleter -CommandName "git" -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$cmd = $commandAst.ToString() -split " "
# order is based on "popularity"
$supportedCommands = @("checkout", "add", "commit", "branch", "status", "pull", "push", "merge", "log", "stash")
$comps = @()
if ($cmd.Length -eq 1 -or $supportedCommands -inotcontains $cmd[1]) {
$comps = $supportedCommands
}
else {
$comps = switch ($cmd[1]) {
"checkout" { @("-") + @(git branch --format='%(refname:short)' --sort=-committerdate) }
"add" { @(".") }
"commit" { @("-m") }
"branch" { @("-b", "-D") + @(git branch --format='%(refname:short)' --sort=-committerdate) }
"merge" { @(git branch --format='%(refname:short)' --sort=-committerdate) }
"log" { @("--oneline") }
}
}
$comps | Where-Object { $_ -like "$wordToComplete*" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment