How to highlight all occurrences of a word in an Emacs buffer?

后端 未结 13 1019
执念已碎
执念已碎 2020-12-12 11:56

Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as

相关标签:
13条回答
  • 2020-12-12 12:35

    This package available in Melpa works, you can customize the highlight style as well.

    https://github.com/ignacy/idle-highlight-in-visible-buffers-mode

    0 讨论(0)
  • 2020-12-12 12:36

    Nobody mentioned symbol-overlay mode. It's basically a better rewrite of highlight-symbol-mode. "Better" as in, lacks bugs of original highlight-symbol (such as, temporary highlight getting stuck, or the temporary highlight disappearing for moving inside the highlighted word; or not being able to highlight symbols like *), better integrated, and maintained. See "Advantages" paragraph of its README.

    You can install it as usual, with M-xpackage-install (make sure to update package list beforehand with package-list-packages). For reference, at the bottom I attached code I use to enable the mode and disable a few of the more advanced features which you may or may not want.

    Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?

    Once you enable overlay-symbol, occurrences on the screen will be shown for every word that you put cursor upon after a timeout (timeout by default is 0.5s, can be configured with symbol-overlay-idle-time variable). If a word don't get highlighted, this means there's just one match on the screen (the one you put cursor upon), hence there's no need to highlight it.

    It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.

    To highlight the word under cursor permanently there's a function symbol-overlay-put. To unhighlight call it once again.

    In my config example it's bound to Logo+` key.


    (require 'symbol-overlay)
    (defun enable-symbol-overlay-mode ()
      (unless (or (minibufferp)
                  (derived-mode-p 'magit-mode)
                  (derived-mode-p 'xref--xref-buffer-mode))
        (symbol-overlay-mode t)))
    (define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
      symbol-overlay-mode                                ;; name of the minor mode
      enable-symbol-overlay-mode)
    (global-symbol-overlay-mode)                         ;; enable it
    (global-set-key (kbd "s-`") 'symbol-overlay-put)
    (setq symbol-overlay-ignore-functions nil)           ;; don't ignore keywords in various languages
    (setq symbol-overlay-map (make-sparse-keymap))       ;; disable special cmds on overlays
    
    0 讨论(0)
  • 2020-12-12 12:36

    This maybe won't highlight but will search for a word without you needing to type it...

    when you've reached the word you wanted to search, C-S, then read the full word with C-W then you can C-S and it will search for it. In my Emacs it also highlights all instances in the document.

    0 讨论(0)
  • 2020-12-12 12:38

    This may not be as nice as what you were hoping but if you put

    (global-hi-lock-mode 1)
    

    in your .emacs file then you can type C-x w h REGEX <RET> <RET> to highlight all occurances of REGEX, and C-x w r REGEX <RET> to unhighlight them again. Again, not as elegant as you'd probably like, but it'll work.

    0 讨论(0)
  • 2020-12-12 12:39

    Check Interactive Highlighting

    Should be:

    C-x w h word <RET> <RET>

    0 讨论(0)
  • 2020-12-12 12:40

    Try http://www.emacswiki.org/emacs/msearch.el All occurences of the text selected with the cursor are highlighted. You have to drag over the string which you want to highlight. That enables you to easily change the selection without changing the highlight.

    If you want to preserve the highlighting of a string you can freeze it.

    You can enslave a buffer to another buffer. Text selected in the master buffer will also be highlighted in the slave buffer. That is useful for comparing buffers. It is also useful for taking notes in one buffer while you investigate the text in another one. You can have a collection of keywords in the notes buffer. Drag over such a keyword and its occurences in the investigated text will be highlighted.

    I am using this stuff for years now. I added the freezing quite recently. So, maybe something is broken. If this is the case leave me a note on http://www.emacswiki.org/emacs/msearch or here.

    0 讨论(0)
提交回复
热议问题