Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to make Esc clear search highlight? #2719

Closed
zeux opened this issue Aug 22, 2015 · 24 comments
Closed

Is it possible to make Esc clear search highlight? #2719

zeux opened this issue Aug 22, 2015 · 24 comments

Comments

@zeux
Copy link

zeux commented Aug 22, 2015

My understanding is that the official way to clear is 'SPC s c'. Which is verbose.

In all Vim-like editors (including Vim, ViEmu, Sublime Text + Vintageous and XCode + XVim) that I use or used in the past, it's either the default or you can set up Esc in normal mode to clear search highlight.

In Vim you can do it using this map (http://www.viemu.com/blog/2009/06/16/a-vim-and-viemu-mapping-you-really-cant-miss-never-type-noh-again/):

:nnoremap <esc> :noh<return><esc>

By now it's impossible for me to switch to anything else. Not to mention that dismissing highlight is frequent and Esc is way easier to hit than 'SPC s c' (or ':noh<cr>').

So, is there a way I can set up the same behavior for spacemacs?

@person808
Copy link
Contributor

(define-key evil-search-highlight-persist-map (kbd "ESC") 'evil-search-highlight-persist-remove-all)

@zeux
Copy link
Author

zeux commented Aug 24, 2015

I'm assuming that the above should be put in dotspacemacs/config (at least, putting it into dotspacemacs/init results in an error because evil-search-highlight-persist-map is not defined yet).

However, it does not work in terminal or GUI mode - using emacs-mac-port at least. I verified that replacing "ESC" with "C-k" works (as in, ctrl+k clears the highlight), so the code is being executed but does not work...

@robbyoconnor
Copy link
Contributor

Maybe try:
in dotspacemacs/config:

(with-eval-after-load 'evil-search-highlight-persist
    (define-key evil-search-highlight-persist-map (kbd "ESC") 'evil-search-highlight-persist-remove-all))

@zeux
Copy link
Author

zeux commented Aug 24, 2015

This does not work either.

Before I load the file, when I press Escape I see ESC prompt in the command bar - as if it's waiting for a second key in the chord. Using (kbd "ESC <escape>") makes double-Escape work in the start-up window, but as soon as I open the file even that stops working - not sure why. (I need single-Escape, I was just testing double-Escape to try to understand what's working and what's not)

@robbyoconnor
Copy link
Contributor

I'm out of ideas -- I didn't test it -- just proposed it :(

@person808
Copy link
Contributor

@zeux Try SPC h d k and press escape. I think that will show you the correct mapping

@zeux
Copy link
Author

zeux commented Aug 24, 2015

@person808 Interesting. So before I open any file SPC h d k once again waits for another key press after ESC. I don't care too much about this - just an interesting observation.

after I open a file SPC h d k tells me that Esc is bound to evil-force-normal-state - so probably the mapping that is set in dotspacemacs/config is overwritten.

P.S. All of this is in the default setup - the only changes to .spacemacs are the addition of define-key.

@zeux
Copy link
Author

zeux commented Aug 24, 2015

Hmm, even if I execute define-key after I open a file via SPC k : it does not override evil-force-normal-state according both to behavior and to SPC h d k.

@zeux
Copy link
Author

zeux commented Aug 24, 2015

Okay, this seems to work (when executed as part of dotspacemacs/config):

 (define-key evil-normal-state-map (kbd "<escape>") 'evil-search-highlight-persist-remove-all)

This overrides 'evil-force-normal-state but I can still exit out of insert mode using Esc - probably because the map is only active when normal mode is on...

@zeux zeux closed this as completed Aug 25, 2015
@Kethku
Copy link
Contributor

Kethku commented Aug 25, 2015

Be careful, I think this might screw with some of the operators in evil mode, but I can't be sure. Just to be safe you may want to write a function which calls evil-force-normal-state as well as evil-search-highlight-persist-remove-all and bind that to escape in normal mode just to make sure you don't clobber some important functionality.

@justin-calleja
Copy link

Any way I can globally change highlight results to turn off after searching with / and hitting RET? And is there a key binding to toggle last search result highlighting (in vim: :set hls! )?

@ndan
Copy link

ndan commented Jun 25, 2017

Any way I can globally change highlight results to turn off after searching with / and hitting RET? And is there a key binding to toggle last search result highlighting (in vim: :set hls! )?

Exclude evil-search-highlight-persist from your package list:
dotspacemacs-excluded-packages '(evil-search-highlight-persist)

@justin-calleja
Copy link

@ndan to be honest - I forgot what this was about. Haven't used Spacemacs in a while but recently I've re-installed it. It appears the behaviour is the same with or without evil-search-highlight-persist. Maybe a newer version of Spacemacs (or the evil-search-highlight-persist) has changed whatever it was which was bugging me.

I'm using search now and it seems ok.

One thing which isn't the same (when compared with Vim usage), is that I cannot figure out how to toggle highlighting of all matches of last search result. Any ideas?

Thanks!

@duianto
Copy link
Collaborator

duianto commented Jun 25, 2017

@justin-calleja
The evil-ex-search-next function that runs when n is pressed, calls the evil-ex-search function, in which the last line is:

(evil-ex-search-activate-highlight evil-ex-search-pattern)

This highlights the previous search pattern.

When it's called from the S-M ; "Eval: " prompt, then it returns a vector with what looks like 9 elements. If the command is made into a key binding, then it probably should be silenced some how.

@justin-calleja
Copy link

@duianto I'm not sure what you're suggesting. If it's too much customisation (e.g. writing Lisp code) - I'm afraid that'll have to wait until I learn more ^^;

btw, in case it's not clear what the desired effect is, I recorded this:

demo

Cheers

@duianto
Copy link
Collaborator

duianto commented Jun 25, 2017

Sorry for being unclear.

Here's some code that you can put into your .spacemacs file, dotspacemacs/user-config section:

  (defun toggle-search-pattern-highlight ()
    "Toggle the previous search patterns highlight On or Off."
    (interactive)
    (if (evil-ex-hl-active-p 'evil-ex-search)
        (evil-ex-nohighlight)
      (evil-ex-search-activate-highlight evil-ex-search-pattern)))

  (global-set-key (kbd "<f8>") 'toggle-search-pattern-highlight)

To make it work, first save the .spacemacs file, and either:

  • Restart Emacs
  • Or if you want to keep Emacs running, then evaluate the function and key binding by: putting the cursor on the closing parenthesis of the toggle-search-pattern-highlight function and press C-x C-e to evaluate the function, and do the same with the cursor on the closing parenthesis of the global-set-key line.

Now the F8 key will toggle the highlight.

toggle search highlight

Any corrections/improvements are welcome, if someone knows of a better way to accomplish this.

@justin-calleja
Copy link

justin-calleja commented Jun 26, 2017

Thanks @duianto :)

Unfortunately... it's not working for me ^^;

Here's my user-config:

(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."

  (defun toggle-search-pattern-highlight ()
    "Toggle the previous search patterns highlight On or Off."
    (interactive)
    (if (evil-ex-hl-active-p 'evil-ex-search)
        (evil-ex-nohighlight)
      (evil-ex-search-activate-highlight evil-ex-search-pattern)))

  (global-set-key (kbd "<f8>") 'toggle-search-pattern-highlight)
  )

I've restarted Emacs - tried searching - and then hitting F8 (or fn + F8, or SPC + F8). Maybe I have some config getting in the way of this working.

Would it be possible to make the key combination the following 2 characters inside quotes: "`/" ? Neither of these are really important though - so if you're busy don't worry about it.

@duianto
Copy link
Collaborator

duianto commented Jun 26, 2017

Your right, it only works on the develop branch, not on the master branch, I should have tested both.
I'll try to get it to work on the master branch as well.

@justin-calleja
Copy link

@duianto no problem my friend. Thanks!

@duianto
Copy link
Collaborator

duianto commented Jun 26, 2017

Update:

Unfortunately I haven't been able to get it to work 😞

The master branch uses another evil-search-module called isearch, the develop branch uses evil-search.
I haven't found a way to detect if there's an active highlight, with the isearch module.

@robbyoconnor
Copy link
Contributor

It doesn't matter what master uses -- all pull requests are against develop.

@justin-calleja
Copy link

@duianto don't worry - thanks anyway. I'll stop replying with "thanks msgs" here - using Gitter :)

@robbyoconnor and develop is eventually merged into master? In that case @duianto 's snippet might come in handy in the next release (I guess).

@sir4ur0n
Copy link
Contributor

Sorry for reviving a 3 year-old issue but as I struggled to have <escape> also clear search highlight (none of the aforementioned solutions worked for me) and customizing (Spac)Emacs can be overwhelming for newcomers (like me), here's how I had it work, in case it helps others:

;; This section should already exist in your .spacemacs file
(defun dotspacemacs/user-config ()

  (defun custom-evil-force-normal-state ()
    "Delegate to evil-force-normal-state but also clear search highlighting"
    (interactive)
    (evil-force-normal-state)
    (spacemacs/evil-search-clear-highlight)
    )

  (define-key evil-normal-state-map (kbd "<escape>") 'custom-evil-force-normal-state)

)

Basically we define custom-evil-force-normal-state as a wrapper of the default <escape> key (evil-force-normal-state) which calls spacemacs/evil-search-clear-highlight afterwards to clear search highlighting (the command run by SPC s c.

There may be cleaner/more efficient ways of achieving this, but at least this seems to work for me 😄

jramnani added a commit to jramnani/dotfiles that referenced this issue Jan 19, 2022
Appease my muscle memory for how to toggle search highlighting on and
off. It turns out I've used that F2 key mapping for ":nohls" for so long
I miss it when it's gone.

Tested this solution and it works great.  I may not need all that
belt-and-suspenders stuff for defining the key binding. Polish that up
later.

Credit:
syl20bnr/spacemacs#2719 (comment)
@ltylty
Copy link

ltylty commented Oct 12, 2024

(advice-add 'evil-force-normal-state :before #'evil-ex-nohighlight)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants