Copy Regular Expression Emacs

I have a text file. Can Emacs select text based on a regular expression and put it in a kill-ring, so I can copy it to another location? Something like regex-kill-ring-save?

+6
emacs
source share
3 answers

inspired by the comments already provided (Charles’s answer doesn’t work the way I would like to), I added a new function to the isearch / isearch-regexp mode map, which puts only the match string in the annihilation ring (whereas Charles's sentence kills from the current point to the end matching string):

(defun hack-isearch-kill () "Push current matching string into kill ring." (interactive) (kill-new (buffer-substring (point) isearch-other-end)) (isearch-done)) (define-key isearch-mode-map (kbd "Mw") 'hack-isearch-kill) 

The good thing about the isearch / isearch-regexp approach (which you can enable with Cs and CMs respectively) is that you can see your search string grow and you can copy it with Mw once you are satisfied (and go back to where you used to be with Cu C-Space ).

This works for me with Emacs 23.1. I don't know if this will work in all situations. In any case, I hope you find this useful :)

UPDATE: going through emacswiki, I came across KillISearchMatch , which offers more or less the same (plus a few more tips ...).

Cheers, Daniel

+3
source share

I'm not sure if there is such a function already, but what can you do with the keyboard macro:

  • Start recording the macro kbd: Cx (
  • Find your regex with search-forward-regexp
  • Move to the start of your match (the text you want to kill) using various emacs navigation commands, for example. search or backward-word etc.
  • Note: C-spc
  • Go to the end of your match
  • Kill the text: Cw

You can then name the keyboard macro Mx name-last-kbd-macro so that you can run the macro with a name, not Cx e .

If you want to save the macro for future sessions, you can open .emacs and paste the macro into the buffer using Mx insert-kbd-macro . After that, you can bind the key to the macro in the same way that you bind keys to normal emacs functions, for example. (global-set-key "\Cc m" 'funky-macro-macro) .

Learn more about emacs keyboard macros

+1
source share

Isearch + is already doing this. Optionally sets the area around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to switch this M-= C-SPC .

isearchp-deactivate-region-flag is a variable defined in Isearch+.el .

Its value t

Documentation:

A nonzero value means that the search leads to the deactivation of the region.

See also the isearchp-restrict-to-region-flag parameter. You can enable this option using M-= C-SPC during the search.

You can configure this variable.

0
source share

All Articles