*This article was originally posted on July 23rd, 2019 at: https://nickjanetakis.com/blog/find-and-replace-text-in-1-or-more-files-using-vim-fzf-and-ripgrep
Vim is a great editor and over the months I've configured a number of custom mappings and plugins to help me find and replace text in a number of different ways.
In this video, we'll go over how to set things up so you can:
- Find and jump to a word in a line
- Find and cycle through word matches in the current file
- Find and cycle through visual section matches in the current file
- Find and cycle through regular expression matches in the current file
- Find any word, visual selection or regexp match in the current directory
- Replace a word, visual selection or regexp match in the current visual selection
- Replace a word, visual selection or regexp match in the current file
- Perform a multiple cursors style replace, but without using multiple cursors
- Replace a word, visual selection or regexp match in the current directory
Basically we'll cover close to every possible find / replace use case you'll encounter while doing some programming work or writing. Everything will be accessible with easy to remember mappings or short commands and searching will be lightning fast.
Demo Video Showing Everything
Timestamped Table of Contents
- 0:55 -- Cloning an example project to work with
- 1:43 -- Finding or jumping to a word on a single line
- 3:25 -- Finding a term in the current file
- 4:57 -- Automatically clearing search highlights on cursor move
- 6:37 -- Manually replacing text in the current file
- 7:33 -- Using the star mapping to search a word under your cursor
- 7:54 -- Replacing the last searched word using a custom mapping
- 10:45 -- Using the visual star plugin to search for any selected term
- 11:59 -- Replacing any selected term using a custom mapping
- 13:31 -- Replacing text inside of a visual selection
- 14:48 -- A multiple cursors alternative in Vim
- 18:28 -- Custom key mapping to quickly replace near by text
- 19:53 -- Finding terms in multiple files based on where you opened Vim
- 21:33 -- Opening search results in vsplits, splits or tabs
- 22:42 -- Opening multiple search results at once
- 23:22 -- Seeing real time search results when finding text in multiple files
- 24:02 -- Filtering multiple file search results by file type
- 24:48 -- Finding files, viewing git commits and more with the fzf.vim plugin
- 26:50 -- Installing and configuring fzf
- 28:26 -- Installing and configuring fzf.vim
- 30:26 -- Installing and configuring ripgrep to work with fzf and fzf.vim
- 32:41 -- Finding and replacing text in multiple files with vim-grepper
- 35:42 -- Finding and replacing a phrase or regexp in multiple files
- 39:15 -- Installing and configuring the vim-grepper plugin
- 40:48 -- Running vim-grepper manually for more complex find / replace
Reference Links
- https://github.com/nickjj/dotfiles
- https://github.com/haya14busa/is.vim
- https://github.com/nelstrom/vim-visual-star-search
- https://github.com/junegunn/fzf
- https://github.com/junegunn/fzf.vim
- https://github.com/BurntSushi/ripgrep
- https://github.com/mhinz/vim-grepper
What do you do to solve finding and replacing text? Let me know below!