DEV Community

Cover image for Ripgrep Cheatsheet For Neovim Users
gokayburuc.dev
gokayburuc.dev

Posted on

Ripgrep Cheatsheet For Neovim Users

Prompt List

  • [x] How to catch date using ripgrep
  • [x] How to catch email using ripgrep
  • [x] How to catch time using ripgrep
  • [x] How to catch markdown titles using ripgrep
  • [x] How to catch hex color using ripgrep
  • [x] How to catch html tags using ripgrep
  • [ ] How to catch comments using ripgrep
  • [x] How to catch adresses using ripgrep
  • [ ] How to catch JSON geolocations using ripgrep
  • [x] How to catch Markdown titles using ripgrep
  • [x] How to catch Markdown tasks using ripgrep
  • [x] How to catch URL Links using ripgrep
  • [x] How to catch DNS Numbers using ripgrep
  • [x] How to catch SHA25 Hash Tokens using ripgrep
  • [ ] How to catch phone number using ripgrep

ripgrep Cheatsheet

Introduction

If you are using Neovim and ripgrep to navigate between files, this Cheatsheet will be a guide for you. Ripgrep is a very fast grep tool because it is written on Rust-regex.

When you use rg as the default command in your Telescope configurations on Neovim, you will see a huge speed difference in searches between your projects.

For more information, you can check the following links:

Catching Dates Using ripgrep

YYYY-MM-DD

(e.g., 2024-08-06):

rg '\b\d{4}-\d{2}-\d{2}\b'
Enter fullscreen mode Exit fullscreen mode

DD/MM/YYYY or DD-MM-YYYY

(e.g., 06/08/2024 or 06-08-2024)

rg '\b\d{2}[/-]\d{2}[/-]\d{4}\b'
Enter fullscreen mode Exit fullscreen mode

MM/DD/YYYY or MM-DD-YYYY

(e.g., 08/06/2024 or 08-06-2024):

rg '\b\d{2}[/-]\d{2}[/-]\d{4}\b'
Enter fullscreen mode Exit fullscreen mode

Month DD, YYYY

(e.g., August 6, 2024)

rg '\b\w+\s+\d{1,2},\s+\d{4}\b'
Enter fullscreen mode Exit fullscreen mode

Catching HTML Tags Using ripgrep

Basic HTML Tag Matching

Catching HTML tags

rg '<[^>]+>'

Enter fullscreen mode Exit fullscreen mode

Advanced Matching (With Attributes)

Catching Tags with attributes

rg '<a\b[^>]*>'
Enter fullscreen mode Exit fullscreen mode

Catching Markdown Titles Using ripgrep

Markdown titles from level 1 to 6

h1, h2, h3, h4,h5, h6 titles

rg '^#{1,6} '

Enter fullscreen mode Exit fullscreen mode

Markdown titles second level

h2 titles

rg '^## '

Enter fullscreen mode Exit fullscreen mode

Catching Markdown Tasks with ripgrep

Catching All Tasks

Catching markdown Tasks using ripgrep

rg '^\s*[-*]\s*\[[ xX]\]'
Enter fullscreen mode Exit fullscreen mode

Catching Incomplete Tasks

Catching incomplete markdown tasks using ripgrep

rg '^\s*[-*]\s*\[ \]' TODO.md

Enter fullscreen mode Exit fullscreen mode

Catching Completed Tasks

Catching completed markdown tasks using ripgrep

rg '^\s*[-*]\s*\[[xX]\]' TODO.md
Enter fullscreen mode Exit fullscreen mode

URL Matching using ripgrep

Basic URL Matching

Basic URL matching

rg 'https?://[^\s/$.?#].[^\s]*'
Enter fullscreen mode Exit fullscreen mode

FTP URL Matching using ripgrep

FTP link Catching

rg '(https?|ftp)://[^\s/$.?#].[^\s]*'

Enter fullscreen mode Exit fullscreen mode

Matching HTTPS Links using ripgrep

HTTPS catching with ripgrep

rg 'https://[^\s/$.?#].[^\s]*'

Enter fullscreen mode Exit fullscreen mode

Matching URLs with Query Parameters and Fragments

Matching URLs with Query Parameters and Fragments

rg 'https?://[^\s/$.?#].[^\s]*[^\s]*'

Enter fullscreen mode Exit fullscreen mode

Matching URLs in Markdown Links

Matching URLs in Markdown files.

rg '\[.*\]\(https?://[^\s/$.?#].[^\s]*\)'
Enter fullscreen mode Exit fullscreen mode

Alternative :

rg '\[.*?\]\(https?://[^\)]+\)'
Enter fullscreen mode Exit fullscreen mode

Matching HEX Codes Using ripgrep

3 Digit Hex Codes

e.g. #F00 - Red ( equivalent to #FF0000 )

rg '#[0-9a-fA-F]{3}\b'

Enter fullscreen mode Exit fullscreen mode

6 Digit Hex Codes



#FF0000 - Red
#00FF00 - Green
#0000FF - Blue

Enter fullscreen mode Exit fullscreen mode
rg '#[0-9a-fA-F]{6}\b'
Enter fullscreen mode Exit fullscreen mode

8 Digit Hex Codes



** Colors with Alpha Channel **

#FF0000FF - Red with 100% opacity
#00FF00FF - Green with 100% opacity
#0000FFFF - Blue with 100% opacity
#FFFF00FF - Yellow with 100% opacity
#FF00FFFF - Magenta with 100% opacity
#00FFFFFF - Cyan with 100% opacity
** Colors with Transparency **

#FF000080 - Red with 50% opacity
#00FF0080 - Green with 50% opacity
#0000FF80 - Blue with 50% opacity
#FFFF0080 - Yellow with 50% opacity
#FF00FF80 - Magenta with 50% opacity
#00FFFF80 - Cyan with 50% opacity
** Shades of Gray with Alpha Channel **

#000000FF - Black with 100% opacity
#333333FF - Dark Gray with 100% opacity
#666666FF - Medium Gray with 100% opacity
#999999FF - Light Gray with 100% opacity
#FFFFFF00 - White with 0% opacity (fully transparent)
** Earth Tones with Alpha Channel **

#964B00FF - Brown with 100% opacity
#228B22FF - Forest Green with 100% opacity
#663300FF - Sienna with 100% opacity
#A0522DFF - Terra Cotta with 100% opacity
#808000FF - Olive Green with 100% opacity


Enter fullscreen mode Exit fullscreen mode
rg '#[0-9a-fA-F]{6}\b'
Enter fullscreen mode Exit fullscreen mode

All Format Hex codes

All formats of hex codes (3,6,8 digits )

rg '#[0-9a-fA-F]{3,8}\b'

Enter fullscreen mode Exit fullscreen mode

Matching DNS Numbers using ripgrep

Matching IPv4 Adressses

** Private IP Addresses **

  • 192.168.1.1 - Matches a private IP address (used for local networks)
  • 10.0.0.1 - Matches a private IP address (used for local networks)
  • 172.16.1.1 - Matches a private IP address (used for local networks)

** Public IP Addresses **

  • 8.8.8.8 - Matches a public IP address (Google's public DNS server)
  • 216.58.194.174 - Matches a public IP address (Google's public IP address)
  • 157.240.195.35 - Matches a public IP address (Facebook's public IP address)

** IP Addresses with Subnet Masks **

  • 192.168.1.1/24 - Matches a private IP address with a subnet mask of 255.255.255.0
  • 10.0.0.1/16 - Matches a private IP address with a subnet mask of 255.255.0.0
  • 172.16.1.1/20 - Matches a private IP address with a subnet mask of 255.255.240.0

** IP Addresses with Default Gateways **

  • 192.168.1.1/24 gw 192.168.1.254 - Matches a private IP address with a subnet mask of 255.255.255.0 and a default gateway of 192.168.1.254
  • 10.0.0.1/16 gw 10.0.0.1 - Matches a private IP address with a subnet mask of 255.255.0.0 and a default gateway of 10.0.0.1
  • 172.16.1.1/20 gw 172.16.1.1 - Matches a private IP address with a subnet mask of 255.255.240.0 and a default gateway of 172.16.1.1
rg '\b((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b'
Enter fullscreen mode Exit fullscreen mode

Matching IPv6 Adressses

** Global Unicast Addresses **

  • 2001:0db8:85a3:0000:0000:8a2e:0370:7334 - A global unicast address
  • 2001:0db8:0000:0000:0000:0000:0000:0001 - A global unicast address
  • 2001:4860:4860:0000:0000:0000:0000:8888 - A global unicast address (Google's public IPv6 DNS server)

** Link-Local Addresses **

  • fe80:0000:0000:0000:0202:b3ff:fe1e:8329 - A link-local address
  • fe80:0000:0000:0000:0000:0000:0000:0001 - A link-local address
  • fe80::0202:b3ff:fe1e:8329 - A link-local address (abbreviated form)

** Unique Local Addresses **

  • fc00:0000:0000:0000:0000:0000:0000:0001 - A unique local address
  • fd00:0000:0000:0000:0202:b3ff:fe1e:8329 - A unique local address
  • fc00::1 - A unique local address (abbreviated form)

** Multicast Addresses **

  • ff02:0000:0000:0000:0000:0000:0000:0001 - A multicast address
  • ff15:0000:0000:0000:0000:0000:0000:0001 - A multicast address
  • ff02::1 - A multicast address (abbreviated form)

** Special Addresses **

  • ::1 - The IPv6 loopback address
  • :: - The IPv6 unspecified address
  • ff00:0000:0000:0000:0000:0000:0000:0000 - The IPv6 multicast address range
rg '\b(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):([0-9a-fA-F]{1,4}:){1,6}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}:){1,5}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,5}|:):([0-9a-fA-F]{1,4}:){1,4}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,4}|:):([0-9a-fA-F]{1,4}:){1,3}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,3}|:):([0-9a-fA-F]{1,4}:){1,2}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,2}|:):[0-9a-fA-F]{1,4}([0-9a-fA-F]{1,4}:){1}|::([0-9a-fA-F]{1,4}:){1,7}|::([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})\b'
Enter fullscreen mode Exit fullscreen mode

Matching Email Using ripgrep

Matching Email Adresses using ripgrep

rg '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'

Enter fullscreen mode Exit fullscreen mode

Matching Time using ripgrep

Catching times in the format HH:MM:SS

Catching times in the format HH:MM:SS

rg '\b[0-9]{1,2}:[0-9]{2}\b'
Enter fullscreen mode Exit fullscreen mode

Catching times in the format HH:MM AM/PM

Catching times in the format HH:MM AM/PM

rg -oE '\b[0-9]{1,2}:[0-9]{2} (AM|PM)\b' your_file.txt

Enter fullscreen mode Exit fullscreen mode

Matching Adressses using ripgrep

Matching Adressses using ripgrep

rg '\b[0-9]+ [A-Za-z]+ [A-Za-z]+, [A-Za-z]+, [A-Za-z]+ [0-9]{5}\b'

Enter fullscreen mode Exit fullscreen mode

Catching JSON geolocations

Catching JSON geolocations

{"lat": 37.7749, "lng": -122.4194} format JSON geolocations

rg  '\{ *"lat": *[0-9.-]+, *"lng": *[0-9.-]+ *\}'
Enter fullscreen mode Exit fullscreen mode

Catching JSON geolocations with additional properties

lat, lng, alt, accuracy propercies in JSON geolocations

rg '\{ *"lat": *[0-9.-]+, *"lng": *[0-9.-]+, *"alt": *[0-9.-]+, *"accuracy": *[0-9.-]+ *\}'

Enter fullscreen mode Exit fullscreen mode

SHA256 Matching Using ripgrep

Input: hello world
SHA-256 Hash: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

rg '[0-9a-fA-F]{64}'

Enter fullscreen mode Exit fullscreen mode

Matching Phone Number using ripgrep

Catching phone numbers in the format (123) 456-7890

Catching phone numbers in the format (123) 456-7890

rg '\(\d{3}\) \d{3}-\d{4}'

Enter fullscreen mode Exit fullscreen mode

Top comments (0)