7

如何在dotnet,git,winget等命令行中使用自动完成功能

 3 years ago
source link: https://geekplayers.com/auto-complete-command-line.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何在dotnet,git,winget等命令行中使用自动完成功能_极客玩家大白

转载自 Scott Hanselman.

多年前,.NET core增加了 Powershell或bash中的.NET Core CLI的命令行Tab自动完成 的功能,但很少有人花时间进行设置。

我喜欢设置 并使我的Prompt/命令行/ shell /终端等的体验尽可能有用(和Pretty)。在Windows上,你有很多命令行Shell 可供选择!

请记住,这些是SHELLs,而不是终端或其他控制台。你可以在 Windows Terminal中运行所有这些。

  • 经典cmd.exe命令提示符(伪DOS!) - 仍然很有用,Clink 可以使其更加bash-y,而无需进行全面的bash操作。
  • 来自Malcolm Smith的 Yori
  • Starship - 稍后会有更多介绍,这有点独特
  • Windows PowerShell - 经典,因为…
  • PowerShell 7已经发布并且可以在任何地方运行,包括ARM机器!

我倾向于将PowerShell 7(以前称为PowerShell Core)用作主提示符,因为它是跨OS的提示符。我可以在Windows和Linux上使用相同的提示,相同的功能,所有内容。

极客玩家大白-WindowsTerminal

但是命令行自动补全功能带给我最大的乐趣!

  • git ch <TAB> -> git checkout st <TAB> -> git checkout暂存
  • dotnet bu <TAB> -> dotnet构建
  • dotnet –list-s <TAB> -> dotnet –list-sdks
  • <TAB>中的winget -> winget安装 -> winget安装WinDi <TAB> -> winget安装WinDirStat

一旦你成功地完成了标签操作,就可以轻松获得荣耀。使用PowerShell及其表亲,可以通过Register-ArgumentCompleter使其成为可能。这是dotnet CLI的外观。

# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
        dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
           [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}

看起来很多,但是唯一重要的是,当它看到命令“dotnet”和一些部分文本并且用户按下TAB时,它将调用cursorPosition和wordToComplete并传递给“dotnet complete”。

注意:如果你了解这是如何工作的,则可以轻松地为一直使用的实用程序制作自己的Argument Completer!你可以为使用你的实用程序的工作人员制作它们!

你从未真正看到过对“dotnet complete”的调用。你只是看到自己输入dotnet bui <TAB>并获得一系列选项即可浏览!

这是后台发生的事情:

> dotnet complete --position 3 bui 
build 
build-server 
msbuild

你可以将它们添加到你的$profile中。通常,我在命令行上运行’notepad $profile’,它将在正确的位置自动创建正确的文件。

这是一个超级强大的模式!你可以使用 PoshGit在PowerShell中的Git中 以及在WinGet中获得自动完成功能

你已添加到PowerShell配置文件的一些更晦涩的自动完成功能是什么?

dotnet.jpg
一个有故事的程序员

(转载本站文章请注明作者和出处 极客玩家大白

点击了解 :.NET技术人的网站

Show Disqus Comments

0 comments

Be the first person to leave a comment!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK