Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I bind a regular expression to a key combination in emacs?
    text
    copied!<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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload