Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace on variable name within S-Expression (lexical scope)?

Using emacs with Paredit enabled. How can I search through a LISP (Clojure) S-expression and rename a variable? I'd like to do it within the current S-expression instead of globally.

like image 525
Eric Schoonover Avatar asked Mar 25 '12 05:03

Eric Schoonover


2 Answers

Just use C-M-<space> at the start of the s-expression you'd like to use as your scope, to highlight it, and then do a query-replace as usual - it restricts itself to the active region.

like image 159
amalloy Avatar answered Nov 14 '22 11:11

amalloy


The query-replace with active region is quite good for general search/replace in region. But for refactoring code, I much prefer iedit.

To rename a variable within a defun, go to the symbol to be reanmed, press C-0 C-; and edit away.

To rename a variable within arbitrary region, go to the symbol, press C-;, select the region of interest, and press C-; again.

iedit has many other functions to help code refactoring. Hint: C-'

like image 37
event_jr Avatar answered Nov 14 '22 13:11

event_jr