8

Right now, I have my Emacs frame vertically split, and am looking at two different buffers in the two windows.

I'd like to go to a word in one buffer, hit some keys, and have the ocurrences of that word (if any) highlighted in the other buffer, scrolling to it if necessary.

Is there a way to do that?

Screenshot to explain

In the screenshot above, with my cursor just before "bar" in the left buffer, I'd like to be able to <do something> and quickly see where "bar" is in the right buffer.


Note: This is related to How do I find text across multiple open buffers? in the sense that, if I could do an isearch across the "other" buffer, it would do what I want. But none of the answers there are exactly what I want:

  • The occur based solutions open a new window that shows matching lines, while I'd like to keep my two windows still displayed -- I'm actually going to be looking in the other buffer for context around the match, so showing only the matching line is not very helpful.
  • multi-isearch-buffers requires me to specify the list of buffers every time I want to do this search (a lot of times), instead of automatically picking it up as the two displayed buffers. More importantly, when I start from one window and perform the isearch, when it finds the match in the other buffer it displays that buffer in this window, so that both windows display the same (other) buffer. Then C-g puts everything back, and I can't see the word highlighted in the other buffer any more.
  • icicle-search has so many features that one of them may be able to do this, but I wasn't able to find anything after a cursory look at the documentation.
  • Right now, what I do is C-s C-w C-s (to do the isearch in this buffer), switch to the other buffer, do C-s C-s (to repeat the same search there), then switch back. That's seven keystrokes, which I could record as a macro I guess. Let me do that. Still, am interested to know if there are other solutions. – ShreevatsaR Nov 6 '14 at 20:20
  • FWIW: C-s C-w C-x o C-s C-s doesn't sound so bad to me. (You don't need a C-s right after the C-w.) – Drew Nov 6 '14 at 21:30
  • @Drew: Thanks, the idea of using a macro only occurred after I had posted the question; now a macro that does C-s C-w C-x o C-s C-s C-x o indeed seems fine. – ShreevatsaR Nov 6 '14 at 21:46
5

You can use a package called highlight-global that's available from github but not on Melpa (as of writing this).

The highlight-frame-toggle function defined in this pacakge will highlight the word at point or the selected region everywhere in the frame (in all the buffers). This, though, would not do the autoscroll to the highlighted word that you need. It picks a different highlighting color as you highlight more words without clearing the earlier hightlights.

Here is a screenshot of what it looks like:

enter image description here Click for better view of the screenshot

0

helm has interfaces that can accomplish this goal, I've recorded a Gifcast to demonstrate the helm-swoop package:

After starting the helm-swoop search I can either narrow down to less candidates by entering further space-separated tokens or hit C-p and C-n to switch to the next one and scroll in the target buffer at the same time.

  • As I said in the question, the occur-based solutions are not what I want, because they list matching lines (and I have to further go there and interact with the list) instead of keeping the other buffer intact and highlighting the text in it. – ShreevatsaR Nov 6 '14 at 20:27
  • Hmm, the other buffer is kept intact and can show you context, but if the highlights are what you're after, helm-swoop might be more your thing. – wasamasa Nov 6 '14 at 20:34
  • OK, edited it to demonstrate helm-swoop. – wasamasa Nov 6 '14 at 20:41
  • I wish I could make myself clearer. During the entire process, I don't want the other window to ever display anything other what it's currently displaying. All I want is to hit a keystroke and see in the other window, as it is, the (first, say) occurrence of the current word in the other buffer. (In my screenshot, when my cursor is at "bar" in the left buffer, I want to hit a key and be able to see where "bar" occurs in the right buffer, with the right still displayed the same.) Equivalent to hitting C-s C-w C-x o C-s C-s C-x o (which is what I'm using now, and seems fine actually). – ShreevatsaR Nov 6 '14 at 21:41
  • If helm could highlight all the occurrences in the other buffer (while still scrolling so that the first one is visible on screen), then that would make it better than the macro bound to the sequence of those 8 keystrokes. :-) – ShreevatsaR Nov 6 '14 at 21:48
0

How about using highlight-regexp to highlight the current symbol (or word, region, etc) in the other window?

Here's a couple functions that could work. The first one will find the symbol at point, then jump to the other window and highlight it. The second one jumps to the other window and removes the highlights.

 (require 'thingatpt)

 (defun highlight-other-window ()
   (interactive)
   (let ((match (thing-at-point 'symbol t)))
     (when match
       (save-excursion
         (other-window 1)
         (highlight-regexp (regexp-quote match))
         (other-window 1)))))

 (defun unhighlight-other-window ()
   (interactive)
   (save-excursion
     (other-window 1)
     (unhighlight-regexp t) 
     (other-window 1)))

In both cases this jumps back so the current window remains active at the end. Not sure if that's what you want, otherwise leave off the final other-window call.

These functions could also be improved by checking for and matching the active region; dealing with the case where there is no other window visible; etc.

  • The highlight-frame-toggle command mentioned in another answer seems to be a more polished version of this, so I guess you don't need to roll your own. – glucas Nov 7 '14 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.