I've seen a lot of articles describing how to replace text with the cfdo
command and others that describe how to find files with FZF
, but I couldn't find one that «puts it all together», so I decided to make a short instruction.
Prerequisites
First you need to install fzf via vim-plug:
call plug#begin()
" if you have fzf installed via brew
if isdirectory('/usr/local/opt/fzf')
Plug '/usr/local/opt/fzf'
else
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
endif
Plug 'junegunn/fzf.vim', {
\ 'on': [
\ 'Ag',
\ 'Rg',
\ 'FZF',
\ 'Files',
\ 'Buffers',
\ 'Commits',
\ 'BCommits',
\ 'Tags',
\ 'BTags',
\ 'History',
\ 'Lines',
\ 'BLines',
\ 'Marks'
\ ] }
call plug#end()
and then ripgrep
Configuring Meta key
(⌥) in macOS
terminal
Some shortcuts use Meta
key. To make it work properly in macOS
terminal check the «Use option as meta key»:
Or «Esc+» option in iterm2
:
Finding and replacing text
- To search text within project files simply type
:Rg <text>
. This will open up a FZF buffer with a list of files. - Then with a Tab key you can select/deselect files that you want to pass to
quickfix
window. To select all files tap Alt/⌥-a and to deselect Alt/⌥-d. - Once your selection is ready hit Enter to pass selected files to
quickfix
buffer. - Now you can replace text in all files within
quickfix
buffer with:cfdo %s/<text to search>/<text to replace with>/g | update
.
The cfdo
command simply executes command on every file in the list, and the update
command after the pipe saves the results to the disk.
I've used here some of the snippets from my vim setup. You can find it here:
Vim Settings
An article describing key features of this config.
Prerequisites
In order to get all features you might want to install following packages:
Installation
On unix and windows(with bash which can be installed with git):
curl -L https://raw.github.com/gko/vimio/main/install.sh | bash
macOS
In macOS terminal.app don't forget to check the «Use option as meta key»:
And «Esc+» option in iterm2:
Shortcuts
Some of shortcuts(Leader key is comma):
- Ctrl + s saves current file
-
Leader + s in both
select
andnormal
mode initiates search and replace - Alt + Up/Down moves line or selection above or below current line(see upside-down for more info)
- Alt + Left/Right moves character or selection to left or to the right
- Leader + n toggles NERDTree
- Leader + m shows current file in NERDTree
- when in select mode ', ", ( wraps selection accordingly
- y…
Top comments (2)
Thanks!
Very clean and easy solution
Thanks!!! Nice solution.