Is there a quick way to 'Find and Replace' across a project

The RStudio offers a find and replace option (oddly called replace and find) for an individual file
There is one piece of code which appear in multiple chapters of a bookdown project which I wish to amend

Is there a quick way I can do this?

1 Like

I don't think there's anything built in. Requested here also:

1 Like

@Frank Thanks for that. I forgot I had set up that thread!. I probably have one or two others to add. It would be interesting to know if any RStudio people look at it or if they want suggested enhancements as issues on github. I fell some of mine are fairly trivial (though useful) @mara?

1 Like


I misunderstood your question, I would say that feature requests are probably better! They're easier to track.

Old answer in case anyone's curious…

Is the question do people from RStudio look at the issues on GitHub? If so, the answer is absolutely yes. In fact, I'd say a solid chunk of my day involves looking at issues and feature requests filed on GitHub.

I'm not on the IDE team (I don't know Java, C, or C++…and my JavaScript is pretty shoddy too, which would make it kinda difficult…I mean, in addition to the fact that I have a different job, over in tidyverse land, that I love very much!), but I'm pretty darn sure they look at the issues as well.

Wrote this a few weeks back re. tidyverse packages, but, in both cases, demand is factored in, but doesn't necessarily dictate the order (esp. because there are often things that aren't user-facing that need to be resolved, etc.)

1 Like

If you are on windows I recommend notepad++, its pretty good at find and replace across files.

If you are on Linux, you can do that from the command line with sed. I made a small R package that wrapps sed for that purpose, but please be aware that as of now it uses regular expression matching. There is also no way of undoing your search and replace so I recommend checking in your project into git first (same is true for the notepad ++ solution)


@hoelk Thanks for tips

You can use vcs::gsubr from here. if you just want to recursively grep use vcs::grepr, which also works on remote paths (ie github | bitbuck | svn)

dir.create(file.path(tempdir(), 'gsubr/d'),recursive = TRUE)

path_up <- tempfile(pattern = 'gsubr-', 
fileext = '.txt', 
tmpdir = file.path(tempdir(), 'gsubr'))

path_down <- tempfile(pattern = 'gsubr-', 
fileext = '.txt', 
tmpdir = file.path(tempdir(), 'gsubr/d'))

cat('this is a test',sep='\n',file = path_up)

cat('this is a test in the subdir',sep='\n',file = path_down)

list.files(tempdir(),recursive = TRUE)
#> [1] "gsubr/d/gsubr-e2e73edc688b.txt" "gsubr/gsubr-e2e7642ce2c6.txt"

vcs::gsubr(pattern = '\\bis\\b', 
replacement = 'was', 
path = tempdir(),
recursive = TRUE)
#> /var/folders/4_/xhs9__yd49l4v4j4wdg9f0wr0000gp/T//RtmpTw5R52/gsubr/d/gsubr-e2e73edc688b.txt 
#>                                                    "[line 1] this was a test in the subdir" 
#>   /var/folders/4_/xhs9__yd49l4v4j4wdg9f0wr0000gp/T//RtmpTw5R52/gsubr/gsubr-e2e7642ce2c6.txt 
#>                                                                  "[line 1] this was a test"

vcs::gsubr(pattern = '\\bis\\b', 
replacement = 'was', 
path = tempdir(),
recursive = TRUE,
test = FALSE)

#> [1] "this was a test"
#> [1] "this was a test in the subdir"

unlink(file.path(tempdir(),'gsubr'),recursive = TRUE)

Created on 2018-05-10 by the reprex package (v0.2.0).

1 Like

This is directly not answering your question, but Eclipse + the StatEt addon is a great (one of the best?) IDE for R package maintenance and it includes workspace-wise search and replace.

1 Like

There are many text editors, packages, IDE, etc. which have this functionality. But since this is something that can be so useful and so powerful in any context, if you are keen, I would suggest learning the Unix utility sed. If you are on macOS or linux, you have access to it directly. If you are on Windows, there are various ways to have access to it. It takes a bit of effort to learn the syntax, but it is, in my opinion, way worth the time investment.