9

我正在使用 POSH git for powershell 并且可以做例如

git checkout mini<tab>

我得到

git checkout minidisk

我想为git checkout创建一个别名gco

gco mini<tab>

要得到

gco minidisk

是否可以将选项卡完成转发到 POSH git 处理程序?

4

3 回答 3

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).

于 2014-05-12T11:29:45.577 回答
2

有一种简单的方法可以通过PowerTab powershell 插件添加您自己的选项卡扩展钩子。一旦你安装了 PowerTab。(您可以通过巧克力使用管理员权限外壳执行此操作)然后在您的 powershell 配置文件中创建以下内容

# 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"
于 2014-05-13T05:39:45.197 回答
-1

另一种选择是使用 git 别名,例如

git config --global alias.co checkout

现在您可以使用:

git co develop

不是很短,gco但其他 gitter 可能会认出git coover gco

于 2017-02-19T01:26:57.223 回答