I want to use Ctrl-space at the start of a function, and at the end of a function to select the area of the function and then change the name of a variable var1
to var2
. How to do this?
2 Answers
You can use C-M-SPC
at the start of a function to mark the entire thing. C-M-a
moves to the beginning of a function quickly.
Then, M-%
to run query-replace
on the region. Type var1 RET var2 RET
then y
/n
to choose whether to replace each occurrence or !
to replace them all.
You may also want to use C-h r
to read the Emacs manual.

- 5,756
- 1
- 20
- 38
After you have marked the region, you can run M-%
which is query-replace
.
In your mode line (the status bar at the bottom) it will ask you to type in the name of the variable you want to change. Afterwards press enter and it will ask you what you want to change it to.
At this point it has highlighted the instances of your original variable and the cursor will be at the first one. You can press space
and it will replace that instance, or press del
to skip to the next one.
If you want to replace them all without checking individually you can press !
If you want to do something more complex, you can use C-M-%
which will let you use query-replace
with regular expressions.

- 136
- 2
M-%
works great. I can't get the C-M-SPC to work to select the fn automatically (python context) – Vass Feb 19 '15 at 14:54mark-sexp
(hitC-h c C-M-SPC
to check). Your cursor needs to be on thed
indef
. – nanny Feb 19 '15 at 14:58