Gab*_*les 5

最近我不得不再次开始在 Windows 上进行开发,并且很难弄清楚这一点。

所以,这就是我的想法:

在 Windows 10 专业版和 Windows 11 专业版中测试。

首先,安装Windows 版 Git

然后,打开附带的 Git Bash 终端。除非另有说明,您应该在 Git Bash 中运行以下所有命令。

超级简短、快速的答案:

以管理员身份打开 Git Bash,然后运行:

# install ripgrep
choco install ripgrep
# verify it is now installed
rg --version
Run Code Online (Sandbox Code Playgroud)

其他非管理选项以及更多详细信息如下:

[更通用,并且不需要管理员权限] 选项 1:如何在 Windows 上手动安装 ripgrep(或任何可执行文件)

此手动过程在 Linux或Windows上几乎相同(除了仅在 Windows 上需要 Git for Windows),并且可用于任何可执行文件或脚本。

转至此处的 ripgrep 版本页面,并从最新版本中找到所需可执行文件的 URL。对于 64 位 Windows,请使用 GNU 编译版本 ( ripgrep-13.0.0-x86_64-pc-windows-gnu.zip)MSVC 编译版本 ( ripgrep-13.0.0-x86_64-pc-windows-msvc.zip)。我测试了两者,它们都运行良好。请注意,GNU 编译的rg.exe文件较大,约为 38.2 MB,而 MSVC 编译的文件rg.exe约为 4.42 MB。我不知道为什么会有如此巨大的差异,但我猜测这是因为 MSVSC 编译的版本更多地依赖于系统中已有的 Windows 动态库。

在下面的说明中,我使用了ripgrep-13.0.0-x86_64-pc-windows-msvc.zip. 如果您使用不同的文件,请相应地调整说明。

# download the latest 64-bit Windows release file of your choosing (GNU or
# MSVC)
curl -LO https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep-13.0.0-x86_64-pc-windows-msvc.zip
# unzip it
unzip ripgrep-13.0.0-x86_64-pc-windows-msvc.zip

# create a ~/bin dir to store it
mkdir -p ~/bin 

# copy rg.exe into ~/bin
cd ripgrep-13.0.0-x86_64-pc-windows-msvc
cp -i rg.exe ~/bin/
Run Code Online (Sandbox Code Playgroud)

现在,创建并编辑您的~/.bashrc文件:

# Create `~/.bashrc` if it doesn't exist, or just update the access and
# modification time of the file if it does.
touch ~/.bashrc
# Open the file in your editor of choice. Examples:
notepad ~/.bashrc  # in Notepad
nano ~/.bashrc     # in Nano
subl ~/.bashrc     # in Sublime Text
code ~/.bashrc     # in Microsoft Visual Studio Code (MS VSCode)
Run Code Online (Sandbox Code Playgroud)

将其添加到您刚刚打开的文件的底部~/.bashrc(这是从 Ubuntu 的默认~/.profile文件借用的,我已将其放在网上

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
Run Code Online (Sandbox Code Playgroud)

最后,关闭并重新打开所有 Git Bash 终端,或者在所有打开的终端中运行以下命令:

# re-source your ~/.bashrc file to update your PATH
. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

现在测试一下rg(ripgrep) 是否有效:

# check the version number
rg --version
Run Code Online (Sandbox Code Playgroud)

我的运行和输出是:

$ rg --version
ripgrep 13.0.0 (rev af6b6c543b)
-SIMD -AVX (compiled)
+SIMD +AVX (runtime)
Run Code Online (Sandbox Code Playgroud)

[更简单,但需要管理员权限] 选项 2:如何通过包管理器(例如choco在 Windows 中)安装 ripgrep(或其他程序)

快速总结:

按您的Windows键 --> 输入“Git Bash” --> 右键单击​​您的 Git Bash 快捷方式 --> 单击“以管理员身份运行”。在以管理员身份运行的 Git Bash 窗口中,运行以下命令:

# Install ripgrep in Windows.
# See: https://github.com/BurntSushi/ripgrep#installation. Apparently my
# computer already has `choco` installed on it. 
choco install ripgrep
# - then follow the on-screen instructions, typing `y` for "yes", 
#   or `a` for "yes to all", when needed

# verify that ripgrep is installed; I see:
#       
#       ripgrep 13.0.0 (rev af6b6c543b)
#       -SIMD -AVX (compiled)
#       +SIMD +AVX (runtime)
#       
rg --version
Run Code Online (Sandbox Code Playgroud)

当您这样做时,您也可以安装fzfbat因为我的rgf2.sh脚本(请参阅:此处和 顶部的安装说明rgf.sh)需要这两个:

choco install fzf  # install fuzzy-finder
choco install bat  # install colored `cat` ("cat with wings")
Run Code Online (Sandbox Code Playgroud)

完成后关闭 Git Bash 管理窗口,然后返回使用非管理 Git Bash 窗口。

细节

我非常习惯在Linux Ubuntu 中使用aptsnap安装程序。

事实证明,Windows 中也有 3 种流行的包管理器:

  1. 巧克力味choco install ripgrep
    1. 很受欢迎。
    2. 它有一些付费版本,但也有针对个人和公司/组织的免费(如自由)和开源且免费的版本。请参阅: https: //chocolatey.org/pricing
      1. 查看我的目录C:\ProgramData\chocolatey\LICENSE.txt,我发现他们使用的开源许可证是Apache License, Version 2.0,我认为这是一个非常非限制性(慷慨)的许可证。
  2. 勺子scoop install ripgrep
  3. Winget(另见此处):winget install BurntSushi.ripgrep.MSVC
    1. 这是 Microsoft 支持的官方应用程序。

在 Git Bash 中,检查您是否已安装这些工具。我已经拥有chocowinget安装了。我不确定为什么或如何安装它们,但也许它们随 Windows 一起提供,或者随 Windows 版 Git 一起提供。查看您的系统上是否安装了它们:

choco --version   # I see `1.3.0`
scoop --version   # I see: `bash: scoop: command not found`
winget --version  # I see: `v1.5.1572`
Run Code Online (Sandbox Code Playgroud)

让我们使用Chocolatey来安装ripgrep,因为我读过它可能是最受欢迎的并且有最多的程序。

  1. 安装请参阅: https: //chocolatey.org/install

    仅当您尚未choco安装时才在 Power Shell 中运行此命令:

    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后,使用它:

    以管理员身份打开 Git Bash,然后运行:

    choco install ripgrep
    rg --version  # check the version to see if it installed correctly
    
    Run Code Online (Sandbox Code Playgroud)
  3. 完成后关闭 Git Bash 管理窗口,然后返回使用非管理 Git Bash 窗口。

参考

  1. 我首先在此处此处发现了 3 个主要的 Windows 包管理器:Chocolatey、Scoop 和 Winget 。
  2. 我自己关于如何使用的答案curl(上面-L我的curl用法部分是必需的,因为 GitHub 对于该版本的下载链接有一个 HTML 302 重定向ripgrep