I would like to know if it is possible to refactor R code in ESS. Using search and replace in Emacs does not seem to be a good alternative to it.
-
what exactly do you want to accomplish? can you provide an example? – David LeBauer Jan 05 '11 at 00:43
-
1Renaming of variables. This can be done easily with the statet plugin of eclipse but not in ess. – ggg Jan 11 '11 at 13:51
-
AFAIK refactoring in Eclipse is far far far ahead of refactoring in Emacs. In general Eclipse understands the language, while Emacs (sort of) understands the syntax (kind of). But for some reason I still use Emacs for everything except Java. – Ken Williams Jan 24 '11 at 15:25
-
@Ken, just out of curiosity, what is the reason the emacs is so sticky with you? – VitoshKa Jan 24 '11 at 16:55
-
Good question - I'm not really sure, just force of habit I guess. And I like the way the 'tab' key works. =) Though in the past couple of days I discovered StatET in Eclipse, which handles Sweave files very nicely. – Ken Williams Jan 26 '11 at 15:01
-
@KenWilliams . Now 08/2015, from my experience with Eclipse and IntelliJ when doing Java/Scala, I really like their code naviation and refactoring features. I still don't see that happening in ESS. Maybe it's just because I haven't seen. Anyone has update on this? On the other hand, due to the nature of Javaish projects (i.e. normally complex codebase), Eclipse/IntelliJ are at home about offering those features. – biocyberman Aug 08 '15 at 22:35
-
1Search and replace is no problem in ESS, I mean some 'real' refactoring like extract variables, function, and add more arguments to existing functions. There is currently [Emacs-refactor](https://github.com/chrisbarrett/emacs-refactor) but does not support `R` yet – biocyberman Aug 08 '15 at 22:38
2 Answers
ESS itself does not provide any facilities for refactoring. Instead you can use emacs functionality.
Here is an example of how to replace all words starting with "xxx" and ending with "yyy" with "zzzzzz" in all your open R files in your project directory.
C-x d (enter dired)
% m r$ (mark all files ending in R or r)
Q (enter
dired-to-query-replace-regexp
)type xxx.*zzz and zzzzzz when asked for replacement patterns.
accept, skip with y,n
Here is a complete tutorial of how to search-replace in multiple files.
and also how to save all modified buffers with ibuffer
.

- 21,988
- 13
- 81
- 109

- 8,387
- 3
- 35
- 59
-
1Refactoring in a good IDE is much more sophisticated than find-and-replace of text. For example, you can rename a method in class `Foo` from `value` to `getValue`, and it will find all instances where `Foo.value` is called (handling subclasses of `Foo` correctly), renaming them to `Foo.getValue`, without touching `value` strings in unrelated places. The IDE needs to fully understand the code for this to be possible. – Ken Williams Aug 10 '15 at 21:59
In Emacs you can do a search replace with M-%
and then !
will replace every instance in the current buffer.
If that isn't want you mean, can you explain (edit your Q or comment here) what Statet does and what exactly you'd like to do in Emacs + ESS?

- 170,508
- 25
- 396
- 453