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-regex, you really need to replace the \n in your regex with C-q Q-j, which lets me match my expression 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-regex with my 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-xe. (ie, if I reboot emacs and then type M-x macro-name, and then C-xe, 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-regex. I'll try what you suggested.

+3  A: 

You can use macros, just do C-x( then do everything for the macro, then C-x) to end the macro, then C-xe 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, 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.


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

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


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.

Mike Stone
+1  A: 

Here is a really good reference card for emacs, it helped me a LOT when I was starting out.

Mike Stone


When executing a macro, it's a little different... incremental searches will just happen once, and you will have to execute the macro again if you want to search again. You can do more powerful and complex things though, such as search for a keyword, jump to the beginning of the line, mark, go to end of the line, M-w (to copy), then jump to another buffer, then C-y (paste), then jump back to the other buffer and end your macro. Then, each time you execute the macro you will be copying a line to the next buffer.

The really cool thing about emacs macros is it will stop when it sees the bell... which happens when you fail to match an incremental search (among other things). So the above macro, you can do C-u 1000 C-x e which will execute the macro 1000 times... but since you did a search, it will only copy 1000 lines, OR UNTIL THE SEARCH FAILS! Which means if there are 100 matches, it will only execute the macro 100 times.

EDIT: Check out C-hf highlight-lines-matching-regexp which will show the help of a command that highlights everything matching a regex... I don't know how to undo the highlighting though... anyways you could use a stored macro to highlight all matching the regex, and then another macro to find the next one...?

FURTHER EDIT: M-x unhighlight-regexp will undo the highlighting, you have to enter the last regex though (but it defaults to the regex you used to highlight)

Mike Stone
+1  A: 

In general, to define a custom keybinding in Emacs, you'd write

(define-key global-map (kbd "C-c C-f") 'function-name)

define-key is, unsurprisingly, the function to define a new key. global-map is the global keymap, as opposed to individual maps for each mode. (kbd "C-c C-f") returns a string representing the key sequence C-c C-f. There are other ways of doing this, including inputting the string directly, but this is usually the most straightforward since it takes the normal written representation. 'function-name is a symbol that's the name of the function.

Now, unless your function is already defined, you'll want to define it before you use this. To do that, write

(defun function-name (args)

defun defines a function - use C-h f defun for more specific information. The (interactive) there isn't really a function call; it tells the compiler that it's okay for the function to be called by the user using M-x function-name and via keybindings.

Now, for interactive searching in particular, this is tricky; the isearch module doesn't really seem to be set up for what you're trying to do. But you can use this to do something similar.

+1  A: 

I've started with solving your problem literally,

(defun search-maker (s)
  `(lambda ()
     (let ((regexp-search-ring (cons ,s regexp-search-ring)) ;add regexp to history
           (isearch-mode-map (copy-keymap isearch-mode-map)))
       (define-key isearch-mode-map (vector last-command-event) 'isearch-repeat-forward) ;make last key repeat
       (isearch-forward-regexp)))) ;`

(global-set-key (kbd "C-. t") (search-maker "^.*Table\\(\\(.*\\n\\)*?GO\\)"))
(global-set-key (kbd "<f6>") (search-maker "HELLO WORLD"))

The keyboard sequence from (kbd ...) starts a new blank search. To actually search for your string, you press last key again as many times as you need. So C-. t t t or <f6> <f6> <f6>. The solution is basically a hack, but I'll leave it here if you want to experiment with it.

The following is probably the closest to what you need,

(defmacro define-isearch-yank (key string)
  `(define-key isearch-mode-map ,key 
     (lambda ()
       (isearch-yank-string ,string)))) ;`

(define-isearch-yank (kbd "C-. t") "^.*Table\\(\\(.*\\n\\)*?GO\\)")
(define-isearch-yank (kbd "<f6>") "HELLO WORLD")

The key combos now only work in isearch mode. You start the search normally, and then press key combos to insert your predefined string.