惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

Ben Frain

So, you want a React modal that uses the <dialog> element and transitions in AND out? Scroll indicators on tables with background colours using animation-timeline Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! Center items in a container, and make then left aligned when they overflow A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Managing multiple versions of node, without NVM or additional tools Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Yes! You can use position: sticky and overflow together Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations? Using CSS @property inside shadowRoot (web components) workaround Dynamically create a ref for items when iterating over them in lit.dev templates Selecting and pausing running animations in Lit Web Components New Web APIs — a popover on top of a dialog element can’t be interacted with? Review: ZSA Voyager, split, mechanical keyboard Russel Brand, narcissism, and a sadly common pattern… When it comes to text editors, I feel like Goldilocks Simple settings for writing and converting markdown with Sublime Text Review: The ZSA Platform tenting kit for the Moonlander keyboard Logitech MX Master 3/3s scroll wheel fix Building a line graph with CSS clip-mask Review: Dell 6K 32″ Monitor U3224KBA I broke my keyboard! Swapping the key switches in the Kinesis Advantage360 Pro HUGE macOS Productivity boost: Set-up simple, keyboard only, instant App switching and arrangement Adding to $PATH for a central location for Neovim/NPM tools Neovim Power Tips: Volume 2 Review: MoErgo Glove80, split, wireless, columnar ergonomic keyboard with RGB Review: Kinesis Advantage 360 Pro — split ergo mechanical keyboard Review: Dactyl Manuform – an ergonomic, custom built mechanical keyboard How to animate along an SVG path at the same time the path animates? Getting the context of Web Components (lit)
Neovim – how to do project-wide find and replace?
Ben Frain · 2024-04-13 · via Ben Frain

There are plenty of occasions when using an LSP to ‘rename’ a symbol isn’t possible. Perhaps you are amending JSON files, or some other basic data format like csv or txt. In those instances there will be times when you want to do a find and replace across multiple files. How can we do that in Neovim?

I’ll give you the short version of how I do it, and then I’ll explain what is going on in more detail…

In short

  1. Use telescope to find (grep) the things
  2. Send the things to the Quickfix list
  3. Use cfdo command to change the string to what you want and update all the files: cfdo %s/stringOne/stringTwo/g | update | bd

Simple enough when you are used to it but I found that workflow strange having come to Vim/Neovim from conventional text editors.

But assuming you want to know how we just did what we did…

The explanation

Let’s break down what we are doing here.

  1. Use Telescope to find the things you want. In Vim-land, ‘grep’ and ‘find’ are kind of interchangeable terms. Certainly for our purposes here, it’s the same thing. The term grep is just short-hand for the “global regular expression print”; you know, “find” 😉 It is also worth saying here you can use whatever finding tool you want, fzf, or just the grep command itself. As long as that tool gives you a list of things you can send to the Quickfix list.
  2. So, you have a list of all the instances of the thing you were looking for. Now what? Well, built into Neovim is the ‘Quickfix’ list and the ‘Location’ list. Quickfix can deal with locations across multiple files, Location list is for the current window. Either way, these are just lists of things you may want to do something with. They might be a list of errors, but in this case we are using it to send a list of the lines where we want to change the text we searched for.
  3. Telescope is able to send things directly to the Quickfix list, and I am using that with a mapping of CTRL+Q (which might be the default) to do that.
  4. So now, the list of things is in this Quickfix list, and you can see them like a small buffer in a tray at the bottom of your interface. You can go up and down like you would in any normal file and go back and forth through the locations with :cnext, and :cprev. But what we want to do is just cycle through them all replacing one string with another.
  5. To do string replacement across all the content of any other buffer, we would use %s/stringOne/stringTwo/g where g means global so it replaces every instance on a line (and append c if you wanted to confirm each one). However, we want to do this in one go across all our instances and files.

To do that we use cfdo which is short for “Change File Do”.

So we “change file do”, tell it the change we want with our %s/this/that/g command and then pipe that into the update command, to save the file (otherwise you would have a load of changed buffers you then need to manually write/save), and finally pipe into bd to delete the buffer and keep the resources low.

Summary

I think the way you do find and replace across a project in a modern editor like Sublime Text, Zed, or even VS Code, is much simpler to reason about, but this is an effective way to do the same thing in Vim. There are alternative approaches to this, even when you can’t use an LSP, like using fd and sed outside of Neovim but this is the way I do it.