Search selected text and show all occurrence of it in current (all other buffers) in emacs? - emacs

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

Related

Emacs: How to select word under cursor and append to file

I want the following behavior:
Append the word under cursor into a file(~/vocabulary.txt, for example)
Better still to bind a key for it.
Could anyone show me how to do it?
Should I put those code into .emacs ?
Try the following function:
(defun my-write-to-file ()
"Save word at point to file"
(interactive)
(write-region (concat (thing-at-point 'word) "\n") nil "~/vocabulary.txt" 'append))
When called, this function will save the word at point (the word the cursor is on or the word right before the cursor) to ~/vocabulary.txt.
You can bind it to a key (C-c w in this case, but you can change it to whatever you like) like this:
(global-set-key (kbd "C-c w") 'my-write-to-file)
To use, simply put the function and the keybinding assignment in your .emacs.
#Elethan wrote you a command that does just what you ask for, and bound it to a key.
It might also help to mention some general commands that you can use for this kind of thing. M-x append-to-file appends the region contents to a file, and M-x write-region prepends.
The manual is your friend for things like this. See nodes Misc File Ops and Accumulating Text.
Be aware too that for the two commands just mentioned, as the manual says about append-to-file (it should say it about both):
You should use append-to-file only with files that are not being
visited in Emacs. Using it on a file that you are editing in Emacs
would change the file behind Emacs’s back, which can lead to losing some
of your editing.
Accumulating Text also tells you about commands for adding text to a buffer, including the case of adding to a buffer for a file that you are visiting (as opposed to what the above quote warns you about for append-to-file). These include commands append-to-buffer and prepend-to-buffer.

How can I most quickly edit the text highlighted by incremental search?

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.

How do I list all yanks in emacs?

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.

Emacs copy with regex

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.

emacs list-buffers behavior

In GNU emacs, every time I hit Ctrl-x Ctrl-b to see all of my buffers, the window is split to show the buffer list, or if I have my window already split in 2 (for instance, I will have a shell running in the lower window), the buffer list appears in the other window.
My desired behavior is for the buffer list to appear in my active window so that I can select the buffer I want and continue to working in the same window, rather than having to Ctrl-x Ctrl-o to the other buffer, selecting the buffer (with enter) and editing that buffer in the other window... I've googled for it but it doesn't seem to be a common desire? I wonder if anyone has an elispy (or other) solution?
You might want to rebind C-x C-b to invoke buffer-menu rather than list-buffers:
(global-set-key "\C-x\C-b" 'buffer-menu)
Just customize the variable same-window-regexps. display-buffer will display any buffer whose name matches a regexp there in the currently-selected window.
(You will want to add "[*]Buffer List".)
not exactly a solution, but ido-mode provides a different and powerful way to interact with buffers. C-x b will then show a list of all the open buffers, and the one you select will open in the current window.
Strangely, there isn't an answer here about ibuffer.
I would recommend this as a standard change for the majority of Emacs users:
(global-set-key (kbd "C-x C-b") 'ibuffer)
ibuffer is a very advanced replacement for the default buffer listing, and not only features the exact behaviour requested, but provides a wealth of other functionality.
I listed a few ibuffer filtering and grouping basics in
Emacs: help me understand file/buffer management, but be sure to read the documentation for details.
Try to add
(ido-mode 1)
to your .emacs, and enjoy the result :)
If you like the original buffer list (as opposed to the 'buffer-menu solution proposed by others), you can use this:
(global-set-key (kbd "C-x C-b") 'my-list-buffers)
(defun my-list-buffers (&optional files-only)
"Display a list of names of existing buffers.
The list is displayed in a buffer named `*Buffer List*'.
Note that buffers with names starting with spaces are omitted.
Non-null optional arg FILES-ONLY means mention only file buffers.
For more information, see the function `buffer-menu'."
(interactive "P")
(switch-to-buffer (list-buffers-noselect files-only)))
Which is the same function as before, only in the current window.
I highly recommend bs.el from http://www.geekware.de/software/emacs/ Install it and:
(require 'bs)
(add-hook 'bs-mode-hook 'turn-on-font-lock)
(global-set-key "\C-x\C-b" 'bs-show)
It manages buffers and window configuration in the right way, so everything requires minimum number of keystrokes.
Not sure where I got this but:
;;; Faster buffer switching
(global-set-key [(C tab)] 'buffer-menu)
This makes Ctrl-Tab display all buffers in the current window. You can then navigate to a buffer and hit Enter to visit it.
Another not-what-you-asked-for solution: don't select the desired buffer with the mouse, rather finish typing its name (use tab-completion to reduce keystrokes and increase accuracy), then hit return. The buffer list will disappear, and the new file will be open in the previously active window.