If I need to remove everything in a parenthesis {...}, I do

  1. Put the cursor on {
  2. Click C-space (set mark)
  3. Click C-M-right (go to the })
  4. Click C-w (remove selection)

How can I automatize these steps? How to do this macro-like action in emacs?

  • I am not familiar with the C-M-=> binding. Looks like it's specific to some major mode? In any case, C-M-f also does what you want in that step 3. – Kaushal Modi Jan 25 '16 at 17:41
  • 1
    @KaushalModi But there's a shorter way: steps 2 and 3 can be replaced by C-M-SPC (mark-sexp). – Gilles 'SO- stop being evil' Jan 25 '16 at 21:02
  • Emacs would write C-M-=> as <C-M-right>. You can see Emacs's way of writing a key sequence by pressing C-h k (i.e. invoking describe-key) and then pressing the keys you are interested in. – Constantine Jan 25 '16 at 21:17
  • Optionally, step 5: C-s { to take you to the next brace. – Daniel Doherty Jan 26 '16 at 12:56

See Keyboard Macros in the Emacs manual.

In summary: press C-x ( to start recording a macro, C-x ) to stop, C-x e to execute.

| improve this answer | |
  • 1
    And keep typing e to invoke the macro multiple times in a row (in newer Emacsen). – asjo Jan 25 '16 at 22:20
  • 1
    @asjo: Yes, and if you want to execute the macro right after recording, you can skip C-x ) and press C-x e both to finish recording and to execute it the first time. – Constantine Jan 25 '16 at 22:25
  • 2
    Alternatively, press F3 to start recording the macro, then F4 to finish recording, and F4 again to execute the macro. – legoscia Jan 26 '16 at 11:41

You can also use multiple-cursors as an alternative to use instead of macros -- especially when you would only run the macro three or four times and each one is viewable on the current page.

They're not quite as powerful as macros and there's some decent performance overhead, but it can be a more intuitive solution.

| improve this answer | |

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.