How to associate a regex with a keyboard shortcut in emacs?

In context, I'm some of the newcomers to emacs. I have not used it for very long, but used it more and more (I really like it). I am also comfortable with lisp, but not super familiar with elisp.

What I need to do is bind the regular expression to the key combination, because I often use this particular regular expression.

What I did:

MCs ^.*Table\(\(.*\n\)*?GO\) 

Note. I used the new line above, but I found that for isearch-forward-regexp you really need to replace the \n in the regular expression with the result of Cq Qj . This inserts a literal new line (without completing the command), allowing me to put a new line in an expression and match strings.

How can I bind this to a key combination?

I vaguely understand that I need to create an elisp function that executes the isearch-forward-regexp expression with the expression, but I'm not clear on the details. I searched google and found most of the documentation a bit confusing.

How to associate a regex with a keyboard shortcut in emacs?


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

Edit - this worked, but after saving the macro, when I returned to use it later, I could not use it with Cx e . (i.e. if I restart emacs and then type Mx macro-name and then Cx e , I get a message in the minibuffer like "no last kbd macro" or something like that)


@ Mike Stone - Thanks for the info. I tried to create a macro as follows:

 Cx( MCs ^.*Table\(\(.*Cq CJ\)*?GO\) Cx) 

This created my macro, but when I executed my macro, I did not get the same selection that I usually get when I use isearch-forward-regexp . Instead, he simply bounced to the end of the next match of expression. So it really does not work for what I need. Any ideas?

Edit: it looks like I can use macros to do what I want, I just need to think outside the isearch-forward-regexp box. I will try what you suggested.

+6
regex emacs lisp
source share
4 answers

You can use macros, just do Cx ( , then do everything for the macro, then Cx) to finish the macro, then Cx e will execute the last macro specified. You can then name it with Mx name-last-kbd-macro , which allows you to assign a name to it, which you can then call with Mx TESTIT , then save the definition with Mx insert-kbd-macro , which will put the macro in your current a buffer, and then you can store it in your .emacs file.

Example:

 Cx( abc *return* Cx) 

Define the macro to enter "abc" and press return.

 C-xeee 

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

 Mx name-last-kbd-macro testit 

Macro names for "testit"

 Mx testit 

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

 Mx insert-kbd-macro 

In your current buffer, adds the following:

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

You can then save it in your .emacs file to use the named macro again and again after restarting emacs.

+5
source share

I started by solving your problem literally,

 (defun search-maker (s) `(lambda () (interactive) (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 key sequence from (kbd ...) starts a new empty search. To actually search for your string, you press the last key again as many times as you need. So C-. ttt C-. ttt or <f6> <f6> <f6> . The solution is basically a hack, but I will leave it here if you want to experiment with it.

Perhaps the closest thing to what you need,

 (defmacro define-isearch-yank (key string) `(define-key isearch-mode-map ,key (lambda () (interactive) (isearch-yank-string ,string)))) ;` (define-isearch-yank (kbd "C-. t") "^.*Table\\(\\(.*\\n\\)*?GO\\)") (define-isearch-yank (kbd "<f6>") "HELLO WORLD") 

Key combos now only work in isearch mode. You usually start a search and then press a key combination to insert a predefined string.

+2
source share

@Justin:

When executing a macro, this is slightly different ... incremental search will happen once, and you will have to run the macro again if you want to search again. You can do more powerful and complex things, such as searching for a keyword, skipping to the beginning of a line, marking, skipping to the end of a line, Mw (for copying), then going to another buffer, then Cy (pasting), then go back to another buffer and complete your macro. Then each time you run the macro, you will copy the line to the next buffer.

The really cool thing about emacs macros will stop when it sees a call ... what happens when you can't match incremental searches (by the way). So, with the above macro, you can make Cu 1000 Cx e, which will execute the macro 1000 times ... but since you searched, it will only copy 1000 lines, OR NOT AFTER SEARCH! This means that if there are 100 matches, it will execute the macro only 100 times.

EDIT: Check out C-hf highlight-lines-matching-regexp which will show the help of a command that selects everything that matches the regex ... I don't know how to deselect though ... in any case, you can use a saved macro to highlight all regex matches, and then another macro to find the next ...?

FURTHER EDIT: Mx unhighlight-regexp will deselect, you need to enter the last regular expression (but by default it is used for the regular expression that you used to highlight)

+1
source share

In general, to define custom key bindings in Emacs, you should write

 (define-key global-map (kbd "Cc Cf") 'function-name) 

define-key , unsurprisingly, the function of defining a new key. global-map - global keyboard layout, as opposed to separate maps for each mode. (kbd "Cc Cf") returns a string representing the sequence of keys Cc Cf There are other ways to do this, including entering a line directly, but this is usually the most straightforward, since it takes a normal written representation. 'function-name - a character denoting the name of the function.

Now, if your function is not already defined, you must define it before using it. To do this, write

 (defun function-name (args) (interactive) stuff ...) 

defun defines a function - use Ch f defun for more details. (interactive) there is actually no function call; it tells the compiler that this is normal for a function called by the user with the Mx function-name and with the keys.

Now, for interactive search, in particular, it is difficult; the isearch module is not really configured for what you are trying to do. But you can use this to do something like this.

+1
source share

All Articles