<p>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.</p>

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

<p>What I've been doing:  </p>

<pre><code>M-C-s ^.*Table\(\(.*\n\)*?GO\)
</code></pre>

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

<p>How can I bind this to a key combination?  </p>

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

<p><em>How can I bind a regular expression to a key combination in emacs?</em></p>

<hr>

<p><strong>Mike Stone had the best answer so far -- not <em>exactly</em> what I was looking for but it worked for what I needed</strong>  </p>

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

<hr>

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

<pre><code>C-x( M-C-s ^.*Table\(\(.*C-q C-J\)*?GO\) C-x)
</code></pre>

<p>This created my macro, but when I executed my macro I didn't get the same highlighting that I ordinarily get when I use <code>isearch-forward-regexp</code>.  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?</p>

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

[{'answer_id': 10161, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 4, 'selected': True, 'text': '<p>You can use macros, just do <kbd>C-x (</kbd> then do everything for the macro, then <kbd>C-x )</kbd> to end the macro, then <kbd>C-x e</kbd> will execute the last defined macro.  Then, you can name it using <kbd>M-x name-last-kbd-macro</kbd> which lets you assign a name to it, which you can then invoke with <kbd>M-x TESTIT</kbd>, then store the definition using <kbd>M-x insert-kbd-macro</kbd> which will put the macro into your current buffer, and then you can store it in your <code>.emacs</code> file.</p>\n\n<p>Example:</p>\n\n<pre><code>C-x( abc *return* C-x)\n</code></pre>\n\n<p>Will define a macro to type "abc" and press return.</p>\n\n<pre><code>C-xeee\n</code></pre>\n\n<p>Executes the above macro immediately, 3 times (first e executes it, then following 2 e\'s will execute it twice more).</p>\n\n<pre><code>M-x name-last-kbd-macro testit\n</code></pre>\n\n<p>Names the macro to "testit"</p>\n\n<pre><code>M-x testit\n</code></pre>\n\n<p>Executes the just named macro (prints "abc" then return).</p>\n\n<pre><code>M-x insert-kbd-macro\n</code></pre>\n\n<p>Puts the following in your current buffer:</p>\n\n<pre><code>(fset \'testit\n   [?a ?b ?c return])\n</code></pre>\n\n<p>Which can then be saved in your <code>.emacs</code> file to use the named macro over and over again after restarting emacs.</p>\n'}, {'answer_id': 10202, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': "<p>@Justin:</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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...?</p>\n\n<p>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)</p>\n"}, {'answer_id': 24920, 'author': 'Natalie Weizenbaum', 'author_id': 2518, 'author_profile': 'https://Stackoverflow.com/users/2518', 'pm_score': 1, 'selected': False, 'text': '<p>In general, to define a custom keybinding in Emacs, you\'d write</p>\n\n<pre><code>(define-key global-map (kbd "C-c C-f") \'function-name)\n</code></pre>\n\n<p><code>define-key</code> is, unsurprisingly, the function to define a new key. <code>global-map</code> is the global keymap, as opposed to individual maps for each mode. <code>(kbd "C-c C-f")</code> returns a string representing the key sequence <code>C-c C-f</code>. 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. <code>\'function-name</code> is a symbol that\'s the name of the function.</p>\n\n<p>Now, unless your function is already defined, you\'ll want to define it before you use this. To do that, write</p>\n\n<pre><code>(defun function-name (args)\n  (interactive)\n  stuff\n  ...)\n</code></pre>\n\n<p><code>defun</code> defines a function - use <code>C-h f defun</code> for more specific information. The <code>(interactive)</code> 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 <code>M-x function-name</code> and via keybindings.</p>\n\n<p>Now, for interactive searching in particular, this is tricky; the <code>isearch</code> 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.</p>\n'}, {'answer_id': 60207, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve started with solving your problem literally,</p>\n\n<pre><code>(defun search-maker (s)\n  `(lambda ()\n     (interactive)\n     (let ((regexp-search-ring (cons ,s regexp-search-ring)) ;add regexp to history\n           (isearch-mode-map (copy-keymap isearch-mode-map)))\n       (define-key isearch-mode-map (vector last-command-event) \'isearch-repeat-forward) ;make last key repeat\n       (isearch-forward-regexp)))) ;`\n\n(global-set-key (kbd "C-. t") (search-maker "^.*Table\\\\(\\\\(.*\\\\n\\\\)*?GO\\\\)"))\n(global-set-key (kbd "&lt;f6&gt;") (search-maker "HELLO WORLD"))\n</code></pre>\n\n<p>The keyboard sequence from <code>(kbd ...)</code> starts a new blank search. To actually search for your string, you press <em>last key</em> again as many times as you need. So <code>C-. t t t</code> or <code>&lt;f6&gt; &lt;f6&gt; &lt;f6&gt;</code>. The solution is basically a hack, but I\'ll leave it here if you want to experiment with it.</p>\n\n<p>The following is probably the closest to what you need,</p>\n\n<pre><code>(defmacro define-isearch-yank (key string)\n  `(define-key isearch-mode-map ,key \n     (lambda ()\n       (interactive) \n       (isearch-yank-string ,string)))) ;`\n\n(define-isearch-yank (kbd "C-. t") "^.*Table\\\\(\\\\(.*\\\\n\\\\)*?GO\\\\)")\n(define-isearch-yank (kbd "&lt;f6&gt;") "HELLO WORLD")\n</code></pre>\n\n<p>The key combos now only work in isearch mode. You start the search normally, and then press key combos to insert your predefined string.</p>\n'}]
['https://Stackoverflow.com/questions/10149', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/92/']