9

I'm using POSH git for powershell and can do for example

git checkout mini<tab>

and I get

git checkout minidisk

I'd like to create a alias gco for git checkout to do

gco mini<tab>

to get

gco minidisk

Is it possible to forward the tab completion to the POSH git handler?

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

3 Answers3

7

There is no such thing as "git handler". The only thing that posh-git shell is doing is replacing default TabExpansion function with own implementation.

You need to modify their implementation to get behavior you want.

If you want to modify it, just run this command within posh-git shell:

notepad (Get-Command TabExpansion).ScriptBlock.File

You can replace notepad with your editor of choice.

EDIT

There are few ways you can do it in this particular case. With all the complexity in this implementation though I would not invest too much time, I would just try to convince tab function that you actually used 'git checkout':

function TabExpansion($line, $lastWord) {
   $line = $line -replace '^gco ', 'git checkout '
   # rest of the function as it is...

BTW: there is no way to create alias like that in PowerShell: aliases in PowerShell can replace command, not command + arguments (for latter you will need to define function).

BartekB
  • 8,492
  • 32
  • 33
  • Ok. That's interesting but it's not clear exactly how I would edit that file to add a single tab expansion for __gco__ to behave like __git commit__ – bradgonesurfing May 12 '14 at 11:56
  • Modified answer with one possible (and probably most straight-forward) method of achieving this goal. – BartekB May 12 '14 at 14:40
  • I found a better non intrusive way to do this using the PowerTab extension. http://stackoverflow.com/a/23623543/158285 – bradgonesurfing May 13 '14 at 05:40
3

There is a simple way to add your own tab expansion hooks via the PowerTab powershell plugin. Once you have PowerTab installed. ( You can do this via chocolately using an admin privilege shell) then create the following in your powershell profile

# Load posh-git example profile
. 'C:\tools\poshgit\dahlbyk-posh-git-c481e5b\profile.example.ps1'


# Create a function for registering alias's that support tab expansion
function Register-TabExpansion-Alias([string]$alias, [string]$expansion) {

    Invoke-Expression "function global:$alias { $expansion `$args }"

    Register-TabExpansion -Name $alias -Type Command {
        param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) 

        $Argument = $Context.Argument
        if ( $Argument -notlike '^\$' ){
            $TabExpansionHasoutput.Value = $true 
            TabExpansion "$expansion $Argument"
        }
    }.GetNewClosure()
}

Register-TabExpansion-Alias "gco" "git checkout"
Register-TabExpansion-Alias "grb" "git rebase"
Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
-1

Another option is to use git aliases e.g.

git config --global alias.co checkout

Now you can use:

git co develop

Not quite a short as gco but other gitters are likely to recognize git co over gco.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369