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.
Related
So in notepad++ I can select text, hit C-f, then, if I need to look for occurrence of selection in all opened files, i hit M-o and get nice clickable list with navigating to occurrence option. Or if I need list only for current file I point mouse to “Find all in current document” button do a click and get same nice clickable list only for currently active file. So is it possible to do exact thing in emac?
You can implement that functionality with the following lisp function:
(defun occur-selection ()
(interactive)
(when (region-active-p)
(let (deactivate-mark)
(occur (regexp-quote (buffer-substring (region-beginning) (region-end)))))))
If you put that code in your ~/.emacs file together with the follwing line:
(global-set-key [(meta o)] 'occur-selection)
you should be able to select some text, hit M-o and get a list of all occurrences of the selected text displayed in a separate buffer.
User M-g n and M-g p do cycle through the matching lines in the original buffer.
Note, however, that multiple occurrences in a single line are not distinguished.
By default, Emacs has M-x occur which work similar but slightly differently. It allows you to specify a regular expression, all matches of which in the current buffer will be displayed and hyperlinked.
If your focus is more on navigation than on highlighting all matches of a search term, there might be an external alternative that could help you.
Emacs' original philosophy is not built around user interface metaphors such as clicking with a mouse, it comes from a keyboard only background. If you're interested in this approach, you might want to have a look at the Avy package for Emacs. It lets you quickly jump to one of multiple occurrences of a word.
Check out the excellent Emacs Rocks episode "Jumping Around" to see a precursor of Avy (called ace-jump-mode) in action: http://emacsrocks.com/e10.html
You can do the same thing with the helm package.
Emacs will search the word the cursor/"point" is on (you
don't need to highlight it).
To make helm search in all open files/buffers, use:
M-x helm-multi-swoop-all
To make helm search only in file/buffer you're currently in, use:
M-x helm-swoop
Press the ENTER key to drop into the selected file at the
selected line.
To bind these functions to the same key-comboes, you'd need
this in your .emacs:
(global-set-key (kbd "M-o") 'helm-multi-swoop-all)
(global-set-key (kbd "C-f") 'helm-swoop)
NB
Helm is hosted in the MELPA repository.
HTH,
Michael
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.
As I'm using M-x ispell to check LaTeX code
I use SPC to skip a lot of entries that should not be corrected.
But then I sometimes skip an actual misspelled word.
What's the key to go back with ispell to previous word?
I checked the source code of ispell.el, and unfortunately, there seems to be no key-binding for this (the keys are actually hardcoded in the function ispell-command-loop). As a quick hack, if you don't mind your buffer-local mark-ring to be cluttered, you could do something like this:
(defadvice ispell-command-loop (after leave-breadcrumbs activate)
"Leave a trail in the mark-ring when waiting for user input"
(push-mark (point) t))
Then you can always go back to previous errors with C-u C-SPC. Alternatively, you can create your own mark-ring for this function.
looking for an equivalent cut and paste strategy that would replicate vim's 'cut til'. I'm sure this is googleable if I actually knew what it was called in vim, but heres what i'm looking for:
if i have a block of text like so:
foo bar (baz)
and I was at the beginning of the line and i wanted to cut until the first paren, in visual mode, I'd do:
ct (
I think there is probably a way to look back and i think you can pass more specific regular expressions. But anyway, looking for some emacs equivalents to doing this kind of text replacement. Thanks.
Here are three ways:
Just type M-dM-d to delete two words. This will leave the final space, so you'll have to delete it yourself and then add it back if you paste the two words back elsewhere.
M-z is zap-to-char, which deletes text from the cursor up to and including a character you specify. In this case you'd have to do something like M-2M-zSPC to zap up to and including the second space character.
Type C-SPC to set the mark, then go into incremental search with C-s, type a space to jump to the first space, then C-s to search forward for the next space, RET to terminate the search, and finally C-w to kill the text you selected.
Personally I'd generally go with #1.
as ataylor said zap-to-char is the way to go, The following modification to the zap-to-char is what exactly you want
(defun zap-up-to-char (arg char)
"Like standard zap-to-char, but stops just before the given character."
(interactive "p\ncZap up to char: ")
(kill-region (point)
(progn
(search-forward (char-to-string char) nil nil arg)
(forward-char (if (>= arg 0) -1 1))
(point))))
(define-key global-map [(meta ?z)] 'zap-up-to-char) ; Rebind M-z to our version
BTW don't forget that it has the ability to go backward with a negative prefix
That sounds like zap-to-char in emacs, bound to M-z by default. Note that zap-to-char will cut all the characters up to and including the one you've selected.
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.