Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind a regular expression to a key combination in emacs?

Tags:

regex

emacs

lisp

For context, I am something of an emacs newbie. I haven't used it for very long, but have been using it more and more (I like it a lot). Also I'm comfortable with lisp, but not super familiar with elisp.

What I need to do is bind a regular expression to a keyboard combination because I use this particular regex so often.

What I've been doing:

M-C-s ^.*Table\(\(.*\n\)*?GO\)

Note, I used newline above, but I've found that for isearch-forward-regexp, you really need to replace the \n in the regular expression with the result of C-q Q-j. This inserts a literal newline (without ending the command) enabling me to put a newline into the expression and match across lines.

How can I bind this to a key combination?

I vaguely understand that I need to create an elisp function which executes isearch-forward-regexp with the expression, but I'm fuzzy on the details. I've searched google and found most documentation to be a tad confusing.

How can I bind a regular expression to a key combination in emacs?


Mike Stone had the best answer so far -- not exactly what I was looking for but it worked for what I needed

Edit - this sort of worked, but after storing the macro, when I went back to use it later, I couldn't use it with C-x e. (i.e., if I reboot emacs and then type M-x macro-name, and then C-x e, I get a message in the minibuffer like 'no last kbd macro' or something similar)


@Mike Stone - Thanks for the information. I tried creating a macro like so:

C-x( M-C-s ^.*Table\(\(.*C-q C-J\)*?GO\) C-x)

This created my macro, but when I executed my macro I didn't get the same highlighting that I ordinarily get when I use isearch-forward-regexp. Instead it just jumped to the end of the next match of the expression. So that doesn't really work for what I need. Any ideas?

Edit: It looks like I can use macros to do what I want, I just have to think outside the box of isearch-forward-regexp. I'll try what you suggested.

like image 916
Justin Standard Avatar asked Aug 13 '08 18:08

Justin Standard


People also ask

How to bind commands in Emacs?

Keys can be bound to commands either interactively or in your . emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET .

What is the M key in Emacs?

M- means "meta key", (“Escape” in the lab, on other computers sometimes “Alt”). For meta commands with the Escape key, press the Escape key and then release it, then press the other key. Thus M-f stands for the keyboard sequence "press and release the Escape key", " press f".


1 Answers

You can use macros, just do C-x ( then do everything for the macro, then C-x ) to end the macro, then C-x e will execute the last defined macro. Then, you can name it using M-x name-last-kbd-macro which lets you assign a name to it, which you can then invoke with M-x TESTIT, then store the definition using M-x insert-kbd-macro which will put the macro into your current buffer, and then you can store it in your .emacs file.

Example:

C-x( abc *return* C-x)

Will define a macro to type "abc" and press return.

C-xeee

Executes the above macro immediately, 3 times (first e executes it, then following 2 e's will execute it twice more).

M-x name-last-kbd-macro testit

Names the macro to "testit"

M-x testit

Executes the just named macro (prints "abc" then return).

M-x insert-kbd-macro

Puts the following in your current buffer:

(fset 'testit
   [?a ?b ?c return])

Which can then be saved in your .emacs file to use the named macro over and over again after restarting emacs.

like image 132
Mike Stone Avatar answered Nov 15 '22 09:11

Mike Stone