2

I want to highlight somehow symbol under cursor, is there a way to customize font for that current symbol? Ideally I want to add underline with some color to it, or maybe change bg color of text, is there a package for that or specific default font?

4
  • All instances of that symbol? If your cursor is at var f|oo = bar; var baz = foo;, do you want both instances of foo to be highlighted? – omajid Nov 3 '16 at 16:54
  • yes, it would be nice. – sandric Nov 3 '16 at 17:00
  • Have you seen github.com/nschum/highlight-symbol.el ? – omajid Nov 3 '16 at 18:18
  • Have you tried M-s h . – Jules Nov 3 '16 at 18:33
1

Use library Highlight (code: highlight.el).

Commands hlt-highlight-symbol and hlt-unhighlight-symbol highlight and unhighlight the symbol at point or under the mouse pointer.

(They are bound to C-x X h s and C-x X u s, by default. All commands of the library are bound in keymap hlt-map, which is on prefix key C-x X by default.)

Normally, these commands provide a highlighting background color by default (changed each time you invoke them). But when called from Lisp they accept a FACE argument and use that face.

So you can easily define your own command that uses an underline of a given color, for which you are prompted:

(defun my-highlight-symbol-with-face (symbol color all-buffers-p)
  "Highlight occurrences of SYMBOL using an underline of COLOR.
The symbol at point is used by default, or the symbol under the mouse
pointer if the command is invoked using the mouse.

Highlight over the region if active, or the buffer otherwise.
With a prefix arg, use all buffers that are visible or iconified.
\(This first unhighlights occurrences, to prevent stacking up multiple
highlighting on the same occurrences.)"
  (interactive
   (save-excursion
     (when (listp last-nonmenu-event) (mouse-set-point last-nonmenu-event))
     (let ((symb  (symbol-at-point))
           colr)
       (unless symb (error "No symbol %s" (if (listp last-nonmenu-event)
                                              "under mouse pointer"
                                            "at point")))
       (setq colr  (read-color "Color: "))
       (list symb colr current-prefix-arg))))
  (let ((face  `(:underline (:color ,color))))
    (hlt-highlight-symbol symbol all-buffers-p face)))

(You could also specify an underlining style in the code, or prompt for it. By default, the style is a straight line.)

Just use hlt-unhighlight-symbol to unhighlight this highlighting.

1

Try this:

(defvar hi-last-symbol nil)

(defun hilight-current-symbol (&optional arg)
  (interactive)
  (let ((sym (thing-at-point 'symbol))) 
    (cond
     ;; sym + last and not equal
     ((and
       sym          
       hi-last-symbol       
       (not (string= hi-last-symbol sym)))
      ;; unhilite the last one
      (hi-lock-unface-buffer (format "\\_<%s\\_>" (regexp-quote hi-last-symbol)))
      ;; highlight this one
      (hi-lock-face-buffer (format "\\_<%s\\_>" (regexp-quote sym)))
      (setq hi-last-symbol sym))
     ;; sym + no last
     ((and
       sym              ;we have symbol
       (null hi-last-symbol))             
      ;; highlight this one
      (hi-lock-face-buffer (format "\\_<%s\\_>" (regexp-quote sym)))
      (setq hi-last-symbol sym))
     ;; no sym and last
     ((and
       (null sym)           
       (not (null hi-last-symbol)))           
      (hi-lock-unface-buffer (format "\\_<%s\\_>" (regexp-quote hi-last-symbol))) 
      (setq hi-last-symbol sym)))))

(add-hook 'post-command-hook 'hilight-current-symbol)
1

The highlight-symbol package is a simple way to do exactly this. It provides highlight-symbol which will toggle the highlighting of the symbol under the point and highlight-symbol-mode which will continuously update the highlighting as the point moves.

The default is to change the background color. To underline instead use

(set-face-attribute 'highlight-symbol-face nil
                    :background nil
                    :underline t)

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.