I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of 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 "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.
Related
I want to be able to use M-v hotkey in the emacs search mode to paste text. I know I can add the binding to the isearch-mode-map but when I try to bind yank as a method, it yanks the text in the current buffer, not the search input. How can I find which command is invoked when C-y is pressed in the search mode?
Use isearch-yank-kill instead of yank. Try (lookup-key isearch-mode-map (kbd "C-y")). I use minibuffer-inactive-mode-map, minibuffer-local-map, minibuffer-local-completion-map. You can get exhaustive list of maps by C-hv-mapTAB. Function (current-local-map) can help. See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Controlling-Active-Maps.html
Upd.: Name of current local keymap, definition of function keymap-symbol, see https://stackoverflow.com/a/14490054/1937596
If you use
(setq enable-recursive-minibuffers t)
you can, while in minibuffer, call (eval-expression) by hotkey and execute (current-local-map) or (keymap-symbol (current-local-map))
Typing C-sC-hkC-y will tell you:
C-y runs the command isearch-yank-kill.
More generally, type C-hk whilst isearching, followed by the key sequence you want to know about. Analogous to C-hk when you're not searching, of course.
Typing C-hb whilst isearching displays all of the isearch bindings, which is likewise analogous to the output for C-hb when you're not searching.
The other isearch help bindings are C-hm to show you the mode help, and C-hC-h which gives you a menu to all of the above.
Given such text
Hello, World
I do incremental search for "world"
C-s World
The text is highlihted
Hello, World
Now I would like to type new text "All" instead of the highlighted and get:
Hello, All
How can I do this? (Notice, I am NOT looking for "query-replace", or alike). Ideally it have to be some thing like this:
C-s World BS All
Well you can't use backspace because that key is required to modify your query.
I have this in my init:
(defun le::isearch-kill-result ()
(interactive)
(if (use-region-p)
(call-interactively 'kill-region)
(kill-region (point) isearch-other-end))
(isearch-exit))
(define-key isearch-mode-map [(control w)] 'le::isearch-kill-result)
My point is never at anything useful when I search, so I don't mind overriding
C-w, you may want to pick another key.
AFAIK, there is no official way to do exactly what you want to. The closest way will be "C-s World M-% All" if isearch-query-replace does not belong to the query-replace relatives by your definition.
(isearch-query-replace &optional DELIMITED REGEXP-FLAG)
Start query-replace with string to replace from last search string.
This was bugging me as well, and so here is what I eventually came up with.
Quick Setup
First you'll need Isearch+.
If you are in Emacs 24 or later (or otherwise have ELMA/package.el) you can get Isearch+ with
M-x package-install <RET> isearch+ <RET>
Then add this line to your .emacs:
(global-set-key (kbd "C-.") 'isearchp-set-region-around-search-target)
Then with your cursor at the end, C-x C-e to make this change in the current session.
(This uses C-., you can change this to whatever keys you want instead.)
How To Use
C-s World C-. C-w All
How it works
It is selecting (in proper Emacs parlance, it sets the active region) the highlighted text, then C-w is killing that text (putting it in the kill ring). The string World can now be pasted later if you want with C-y.
If the word is long
There are some other goodies in isearch+. For example, If "World" was a longer word, like "Worcestershire", and you didn't want to type it all, you could try this:
C-s W C-( C-. C-w All
With isearch+, C-( will yank the rest of the word in the current "search target" into your search, so in this case it'd add orchestershire to the current i-search for you.
If having this piece of code in an emacs buffer:
(if (> x 5
true
false))
When I try to edit it in order to fix the parenthesis, something very annoying is happening! When I try to add a closing parenthesis to the if condition, emacs is making the cursor jump to the closing parenthesis after 'false' instead of adding a new parenthesis after 5.
Is this part of some mode, maybe clojure-mode? Do you know how may I fix this? What is this useful for?
It sounds like you're using paredit. Did you install it like recommended on the project page?
As to what it's good for? It's good for editing lists. But you have to buy
into whe whole system, or you'll end up really confused. See the wiki
page.
Do you have this section in your ~/.emacs.el? Just remove it.
;; (require 'paredit) if you didn't install via package.el
(defun turn-on-paredit () (paredit-mode 1))
(add-hook 'clojure-mode-hook 'turn-on-paredit)
Yes, paredit is "different". It will always make sure your parenthesis balance. See http://www.emacswiki.org/emacs/PareditCheatsheet .
For your code, place the cursor beneath the first closing parenthesis and press C-left. Repeat the exercise and it will have moved to where you want it.
Cut&paste (kill & yank in emacs lingo) also allow you to manually screw with the balanced parenthesis, so until you get used to paredit it may be easier to use. Good luck!
how to verify that paredit is the offender
You can type C-h k ) while in your Lisp buffer to see what ) is bound to. If it is bound to paredit-close-round then yes paredit is the offender.
how to disable paredit when you don't know what's triggering it
Try auramo's answer in another thread
or if that doesn't work, try this:
(eval-after-load 'paredit
'(defalias 'paredit-mode 'ignore))
If you are curious about what is triggering paredit-mode in your Emacs, use M-x debug-on-entry RET paredit-mode RET
learning to live with paredit
But still I must encourage you to continue using paredit. Let's keep using paredit and let's see solutions for problems you posed. You asked "Do you know how may I fix this?" I'll just assume you are asking how to fix that if form. Marius Kjeldahl gave you solution which uses paredit-forward-barf-sexp, now in general, if you have some Lisp code where you see that some parens are in wrong places and you want to fix that, you can simply temporarily disable paredit-mode in that buffer (by typing M-x paredit-mode) and then fix your code and then enable paredit-mode again (by typing M-x paredit-mode again). Another thing to consider is that Emacs has undo, so if you arrive at (if (> x 5 true false)) through some action, you can undo that action and start over. Undo is bound to C-z if you use CUA mode.
Still you might find bindings of C-left, C-right to be weird, so you might want to use the following setup:
(eval-after-load 'paredit
'(progn
;; paredit-forward-barf-sexp is usually bound to <C-left>, C-}.
;; here we unbind it from <C-left>
;; so that one can continue to use <C-left> for movement.
(define-key paredit-mode-map (kbd "<C-left>") nil)
;; paredit-forward-slurp-sexp is usually bound to <C-right>, C-).
;; here we unbind it from <C-right>
;; so that one can continue to use <C-right> for movement.
(define-key paredit-mode-map (kbd "<C-right>") nil)
;; paredit-backward-kill-word is bound to M-DEL but not to <C-backspace>.
;; here we bind it to <C-backspace> as well
;; because most people prefer <C-backspace> to M-DEL.
(define-key paredit-mode-map (kbd "<C-backspace>") 'paredit-backward-kill-word)))
You asked "What is this useful for?" by which you may be asking two things:
Why is paredit-close-round useful?
Why does paredit have to bind paredit-close-round to ) when it could have bound it to better keys?
Best way to think of paredit-close-round is to think of it as a counterpart to C-M-u. Move point to | in the following code and try press C-M-u several times to see what happens, and then move point to | again and try press C-M-- C-M-u (i.e. type -u while Control and Alt are on) several times to see what happens.
(when t
(when t
(blah)
(blah))
(when t
(blah | blah)
(blah))
(when t
(blah)
(blah)))
C-M-u is useful for selecting expressions; in order to select an enclosing form or forms, you press C-M-u several times and then C-M-SPC several times. C-M-- C-M-u is useful for evaluating an enclosing form; you press C-M-- C-M-u several times and then C-x C-e to eval the enclosing form.
paredit-close-round basically does what C-M-- C-M-u does.
Why is it an OK thing that paredit binds ) to a command that does something other than simply inserting a close paren? Because you are not supposed to insert a closing parenthesis by yourself. Whenever you insert an open paren, a close paren is also inserted automatically. Whenever you want to change (blah) (blah) to ((blah) (blah)), you simply select the two blah forms and press (.
Cannot get to bind Enter to newline-and-indent in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter], (kbd "enter"), (read-kbd-macro "enter"), (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp
function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If use-hard-newlines' is non-nil, the newline is marked with the
text-propertyhard'.
With ARG, insert that many newlines.
Call auto-fill-function' if the current column number is greater
than the value offill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global
or local binding that gets in the way. trying to remap C-j
also doesnt work.
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode, so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that.
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent.
If you open a new file, activate ruby-mode and try the previous
command you will see why it doesn't work. Because ruby-mode doesn't
have newline-and-indent but rather
reindent-then-newline-and-indent. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode is not the only mode to do so, like paredit-mode which
uses paredit-newline.
Is there a way to list all the yanked text in Emacs? You can do it on Textmate with SPLAT+V.
Edit: I meant recently killed items, items that can be yanked.
The list of kills (i.e., the list of things you can yank) is called kill ring and stored in the variable kill-ring, so you can view it (in a not very nice way) with C-h v kill-ring RET.
The kill ring also appears in the menu, under “Edit / Paste from kill menu”. If you use a text mode Emacs or have turned the menu bar off, you can access the menu with M-x tmm-menubar (bound to M-`): type M-` e p followed by the first letter of the item you want to paste (if it's a letter and it's unique, otherwise whatever character is indicated). If you don't want to paste anything, type M-` e p C-g; the kills remain in the *Completions* buffer. The kill texts are displayed truncated to yank-menu-length characters.
To my knowledge, emacs doesn't support that feature out of the box.
If you're using a Debian or Ubuntu Linux distribution, you can install the emacs-goodies-el package, which contains a browse-kill-ring feature (bound to M-y by default).
Alternatively, you can use the browse-kill-ring ELisp package available here.
See also here for a nice article about this problem and other alternate solutions.
EmacsWiki has a satisfying list of solutions. A portable and intuitive solution uses the built-in popup.el to display a vertical list to choose from:
(global-set-key (kbd "C-c y") '(lambda ()
(interactive)
(popup-menu 'yank-menu)))
In Icicles you can see all of your kill-ring, and yank any entries in it using completion. By default, C-y is bound in Icicle mode to icicle-yank-maybe-completing.
That's the same as yank, unless you give it a negative prefix arg (e.g., C--). In that case, it lets you complete against the kill-ring. Completion can be prefix, apropos (substring, regexp), or fuzzy.
http://www.emacswiki.org/emacs/Icicles_-_Multi-Commands
councel-yank-pop wors well for me
especially with the binding suggested in
http://pragmaticemacs.com/emacs/counsel-yank-pop-with-a-tweak/
(use-package counsel
:bind
(("M-y" . counsel-yank-pop)
:map ivy-minibuffer-map
("M-y" . ivy-next-line)))
if you use helm, you may call the helm-show-kill-ring function.