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?


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.


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.

  • the 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:54
  • 2
    It works for me in Python. It should be bound to the command mark-sexp (hit C-h c C-M-SPC to check). Your cursor needs to be on the d in def. – nanny Feb 19 '15 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.