CL
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: ripgrep:現代化搜尋利器(rg) tags: [Tool, CLI, Linux, MacOS, Search, Rust] --- # ripgrep:現代化搜尋利器(rg) [文章更新時間: 2026-01-23 13:34] ![ripgrep](https://hackmd.io/_uploads/SkNnphV8bl.jpg) ## 📖 簡介 **ripgrep** 是一款由 Rust 編寫的命令行搜尋工具。它的功能與經典的 `grep` 類似,但在速度、預設行為和易用性上有了質的飛躍。它能遞迴地在目錄中搜尋正規表示式(Regular Expression)匹配的內容,並聰明地跳過你不想搜尋的檔案。 --- ## 🌟 為什麼選擇 ripgrep?(優點) * **極致的速度**:在多數基準測試中,ripgrep 是目前最快的搜尋工具,超越了 `grep`、`ack` 與 `ag` (The Silver Searcher)。 * **聰明的預設行為**: * 自動尊重你的 `.gitignore` 檔案。 * 自動跳過隱藏檔案與資料夾(如 `.git`)。 * 自動跳過二進位檔案。 * **強大的 Unicode 支援**:基於 Rust 的正規表示式引擎,原生支援 Unicode。 * **彩色輸出**:預設提供易於閱讀的彩色標記,區分檔名、行號與匹配內容。 * **跨平台**:在 macOS、Linux 與 Windows 上都能完美運行。 --- ## 🛠️ 安裝步驟 你可以根據你的作業系統選擇對應的安裝方式: ### macOS 使用 Homebrew 安裝是最簡單的方式: ```bash brew install ripgrep ``` ### Linux * **Ubuntu/Debian**: ```bash sudo apt install ripgrep ``` * **Arch Linux**: ```bash sudo pacman -S ripgrep ``` * **Fedora**: ```bash sudo dnf install ripgrep ``` ### Rust 開發者 如果你有安裝 Rust 環境,也可以透過 Cargo 安裝: ```bash cargo install ripgrep ``` --- ## ⌨️ 基礎操作步驟 安裝完成後,指令名稱為 `rg`。 ### 1. 基本搜尋 搜尋目前目錄下所有檔案中的關鍵字: ```bash rg "search_term" ``` ### 2. 指定目錄搜尋 ```bash rg "search_term" ./src ``` ### 3. 常用參數組合 | 參數 | 說明 | | --- | --- | | `-i` | 忽略大小寫 (ignore case) | | `-w` | 匹配整個單字 (word boundary) | | `-v` | 反向匹配 (顯示不包含該字串的行) | | `-e` | 正則(regex)匹配 | | `-r` | 替換文字 | | `-l` | 僅列出檔案名稱,不列出具體行內容 | | `-n` | 顯示行號 (預設已開啟) | | `-t` | 僅查詢指定檔案類型 | | `-p` | 開啟顏色, 行數 | | `-h` | 使用說明 | --- ## 💡 使用情境介紹 ### 情境 A:在大規模專案中尋找 Function 定義 當你接手一個超大型專案,想知道 `handleUserLogin` 在哪裡被定義,直接輸入: ```bash rg -w "handleUserLogin" ``` 因為 `rg` 會自動過濾掉 `node_modules` 或 `.git` 目錄,搜尋會瞬間完成。 ### 情境 B:搜尋特定的檔案類型 如果你只想在 Python 檔案中搜尋某個關鍵字: ```bash rg "import pandas" -t py ``` ### 情境 C:結合其他工具 (Pipeline) 你可以將搜尋結果傳遞給其他工具處理。例如,尋找所有包含 "TODO" 的檔案並統計數量: ```bash rg "TODO" --count ``` --- ## 🚀 進階用法 ### 1. 顯示前後文 (Context) 這在 Debug 時非常有用,可以看到匹配行周圍的程式碼: * `-A 3`: 顯示匹配行後 (After) 3 行。 * `-B 3`: 顯示匹配行前 (Before) 3 行。 * `-C 3`: 顯示前後 (Context) 各 3 行。 ```bash rg "error_handler" -C 3 ``` ### 2. 取代功能 (Replace) 雖然 `rg` 主要用於搜尋,但它支援 `--replace` 參數來預覽取代後的結果(通常配合管道使用): ```bash rg "old_name" --replace(-r) "new_name" ``` ### 3. 使用配置檔案 如果你不喜歡每次都輸入某些參數(例如你想包含隱藏檔案),你可以建立一個配置文件。 1. 在家目錄建立 `.ripgreprc`。 2. 在環境變數中加入 `export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"`。 3. 在檔案中寫入: ```text --smart-case --max-columns=150 ``` ### 4. 正則表達式 `rg` 支援 PCRE2(需加 `-P` 參數),這讓你處理複雜的搜尋模式: ```bash rg -P "\d{3}-\d{3}-\d{4}" # 搜尋電話號碼格式 ``` ### 5. 超級模糊搜尋:ripgrep + fzf (模糊搜尋整合) 如果你覺得 rg 的輸出太長,可以結合 fzf 實現互動式搜尋,這會讓你一邊輸入關鍵字,一邊即時過濾搜尋結果。 用下面的方式設定 alias 後可以使用 `rgf` 啟動互動式查詢 Bash ``` # 在 .bashrc 或 .zshrc 加入此別名 alias rgf='rg --line-number --column --no-heading --color=always --smart-case . | fzf --ansi' ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully