Neovim Multiline Search and Replace with grug-far.nvim | ast-grep and waaaaaay more
Have you ever needed to replace really complex strings in Neovim? Probably sometimes you need to replace entire paragraphs that include multiple lines
Neovim Multiline Search and Replace with grug-far.nvim | ast-grep and waaaaaay more
Contents
Table of contents
- YouTube video
- Pre-requisites
- If you like my content, and want to support me
- Discord server
- Follow me on social media
- All links in the video description
- How do you manage your passwords?
- Multiline search and replace
- Open links in new tab
- Add a line at the end of every file
- Replace only certain items
- Replace only certain files in a sub path
- Search only current file
- ast-grep
- Exclude files
- I don’t remember a flag or command
- Get the ripgrep command for a search
- Start your 14 day FREE trial
YouTube video
Pre-requisites
Do you want to test this but not spend time configuring it?
- My entire
neobean
setup is in my github repo, so you can grab it from there - I have a video in which I show you how to download and setup my
neobean
config, but also other neovim distributions, so I highly recommend you check it out:
- If you don’t want to watch the video above, and you already have your own neovim config, and want to quickly get started and follow along in this tutorial
- Run the
git clone
commands below to clone my dotfiles in your .config directory and we will run my config below
1
2
mkdir -p ~/.config
git clone git@github.com:linkarzu/dotfiles-latest ~/.config/linkarzu/dotfiles-latest
- Open the newly downloaded
neobean
config with:
1
NVIM_APPNAME=linkarzu/dotfiles-latest/neovim/neobean nvim
- You can create an alias in your
.bashrc
or.zshrc
file to run my config
1
alias neobean='NVIM_APPNAME=linkarzu/dotfiles-latest/neovim/neobean nvim'
- Then to run this config, just run
neobean
- If you don’t even have neovim yet, of course you will need to install it first, so if you’re just getting started, I have a video for you:
If you like my content, and want to support me
- I create and edit my videos in an M1 mac mini, and it’s starting to stay behind in the editing side of things, tends to slow me down a bit, I’d like to upgrade the machine I use for all my videos to a
mac mini
with these specs:- Apple M4 Pro chip with 14‑core CPU, 20‑core GPU, 16-core Neural Engine
- 24GB unified memory
- 1TB SSD storage
- 10 Gigabit Ethernet
- If you want to help me reach my goal, you can donate here
Discord server
- My discord server is now open to the public, feel free to join and hang out with others
- join the discord server in this link
Follow me on social media
- Twitter (or “X”)
- TikTok
- GitHub
- Threads
- OnlyFans 🍆
- YouTube (subscribe MF, subscribe)
- Ko-Fi
All links in the video description
- The following links will be in the YouTube video description:
- Each one of the videos shown
- A link to this blogpost
How do you manage your passwords?
- I’ve tried many different password managers in the past, I’ve switched from
LastPass
toDashlane
and finally ended up in1password
- You want to find out why? More info in my article:
Multiline search and replace
- I used to do this by hand, fuck that man
Open links in new tab
- I needed to add
{:target="\_blank"}
at the end of each of my links - Demo in video
1
2
\[(.*?)\]\((http[s]?:\/\/.*?)\)(?:[^{]|$)
[$1]($2){:target="_blank"}
Add a line at the end of every file
RIPGREP
(\n|.)*
return match .. "\n<!-- very end of the file -->"
--multiline
\x (lua interpreter)
Replace only certain items
- Sometimes your search includes multiple results, but you need to leave some of them out
- that’s when you use
\s
( Sync all )Sync All
does not work with multiline replacementast-grep
only supports\r
( Replace )
Replace only certain files in a sub path
- Use this in the
Files Filter:
filter - The
Paths:
filter only accepts fixed paths - The
Files Filter
uses gitignore syntax
1
2
3
_posts/**/*
or
_posts/*.md
Search only current file
- Look at the cookbook
- You can (and should) create a keymap for the ones you need
1
:lua require('grug-far').open({ prefills = { paths = vim.fn.expand("%") } })
ast-grep
- ast-grep understand your codes, that’s all I fucking now
- Their page
- “It is like syntax-aware grep/sed! You can write code patterns to locate and modify code, based on AST, in thousands of files, interactively.”
- Old way of me achieving this
1
2
colors\["(linkarzu_color\d+)"\]
"$1"
- But with Astgrep
1
2
3
colors[$A]
$A
\e (switch engine to astgrep)
- Make sure you have ast-grep installed
- Installation instructions
1
brew install ast-grep
- Another example provided by maintainer
- By mistake added
/
and now I need to replace it with.
inside arequire
1
2
3
4
5
require($A)
return 'require(' .. vars.A:gsub('/', '.') .. ')'
*sample.lua
\x (lua interpreter)
\e (switch engine to astgrep)
Exclude files
- I wanted to exclude 2 files from the replace, so added each file in a separate line under
Files Filter:
- !wezterm.lua
- !eldritch.lua
- But remember that it may be easier to treat the search results like a buffer, delete what you don’t want, and then replace
I don’t remember a flag or command
- Just type –help in the
Flags
field, you’ll find all the docs and search - Also for help type
g?
Get the ripgrep command for a search
\p
Start your 14 day FREE trial
This post is licensed under CC BY 4.0 by the author.