Note by Apollia on Nov. 8, 2023: Please join my Patreon if you'd like to support me and my work!

My main personal website is now Apollia.org. I'm still not sure what to do with Astroblahhh.Com, so it's mostly staying as-is for now.





Emacs Lisp Blog Post:

Emacs Lisp code: Right-Click to Highlight and Select Name - Sort of Like Notepad++
12/2/2015

Post Below
12/2/2015 - Emacs Lisp code: Right-Click to Highlight and Select Name - Sort of Like Notepad++ (Emacs Lisp)

    Hide/Show:


   ▲ Top  ▼ Bottom  △ TOC
Emacs Lisp code: Right-Click to Highlight and Select Name - Sort of Like Notepad++
Wednesday, December 2nd, 2015
06:47:22 GMT

Emacs Lisp

The text (and code) editor Notepad++ has a nice feature where all you have to do to temporarily highlight a name (such as a variable name, function name, command name, or other words) everywhere is double-click on it.

Emacs doesn't do that out of the box, and I didn't find a way to make Emacs behave exactly like Notepad++.

But, I like the below behavior more in some ways anyway.


I'm using GNU Emacs 24.5.1. The below requires three add-ons:


Here's all the code I had to add to my .emacs settings file:

Sorry, I don't know how to get rid of the "Wrong type argument: syntax-table-p, nil" error message. But it seems to work fine despite that error message.

And the above code does interfere with whatever right-clicking is supposed to do by default. But the original default behavior of right-click was rather dangerous and useless-seeming - unexpectedly selecting blocks of text and often deleting any text that was already selected - so I'm glad to be rid of that, whatever that was.


After adding the above code and add-ons to your Emacs, it should be possible to right-click on a name to highlight that name everywhere persistently.

The highlighting persists forever until you either right-click on the name again, or run the (highlight-symbol) function in some other way, such as clicking on the name and pressing F3 - the key I've bound to (highlight-symbol).

Right-clicking not only highlights the name, but also selects (or "marks", in Emacs jargon) some, most, or all of the name under your cursor (or "point", in Emacs jargon).


If you right-clicked on a dash or underscore in the name, the entire (or almost entire) name gets selected. Excluding any dollar signs at the beginning, to make it easier to select the names of PHP variables without their $ prefix. (But if you ever actually want to also select the dollar sign, a normal left double-click on a PHP variable name, or an underscore inside it, should accomplish that.)

If you right-clicked anything other than a dash or underscore, the only thing that gets selected is the word you clicked. So, you can easily select individual words in a function or variable name with dashes or underscores in it - something I never figured out how to do in Notepad++.

However, in Emacs, I don't know how to make it possible to so easily persistently highlight (as opposed to select) just individual words, because highlight-symbol.el pays no attention to what text you have or haven't selected - it just highlights the entire name it detects where your cursor (point) is.

But, doing a search while using the code from this previous blog post - Emacs Lisp code: Easily Search for Text You've Selected - will let you easily and case-insensitively put a temporary highlight on any arbitrary currently-selected text.

   ▲ Top  ▼ Bottom  △ TOC


    Hide/Show:





Note by Apollia on Nov. 8, 2023: Please join my Patreon if you'd like to support me and my work!

My main personal website is now Apollia.org. I'm still not sure what to do with Astroblahhh.Com, so it's mostly staying as-is for now.