0

I tried to set a keyboard shortcut for :noh using space key, without success:

.vimrc

nnoremap ,<space> :noh<CR>

Expected behaviour:

  • / to highight text
  • press space: clear highlighting

What would be the right way to do it?

8
  • 2
    works for me. Try to debug your vimrc (start with vim --clean). Perhaps you have other mappings defined for the , possibly with the <nowait> flag? May 18 at 9:36
  • 2
    What does "without success" mean? The mapping isn't registered? You get an error on startup? You get an error when you type the mapped keys? Nothing happens when you enter the keys even though the mapping is registered? Vim sends you an email that says "no success"? Please include specifics.
    – B Layer
    May 18 at 9:36
  • I would like to have the highlighted patterns not hightlighted when I press space key. No error message just nothing happens
    – blondelg
    May 18 at 9:42
  • 3
    are you actually using this after :set hls? Did you actually press , followed by <Space>? May 18 at 9:48
  • 1
    Seconded. Everything looks right so let's make sure you understand what is supposed to happen.
    – B Layer
    May 18 at 9:50
2

According to :help :map (and maybe :help macro?), the keys after a :map command and it’s modifiers (none of which is a comma) are the mapped sequence—type those to produce the right hand-side (with some exceptions).

From the comments, you’re pressing space instead of comma-space. That’s why the mapping isn’t being triggered.

Comma (,) is a common leader for maps, though I recommend using a different key. It’s not all that easy to hit, but more importantly it becomes more difficult to use a regular comma, the inverse of semicolon (;), both of which have useful default actions.

For example, I use space as a leader key.

It is also common to see “manual” leader keys, by which I mean a series of mappings are created that all start with a common. This is contrast to leader mappings, where you set the mapleader (and maplocalleader) variable(s) to a key and then use <leader> in the mapping. This works similarly, but also makes it easy to adjust the prefix on a slew of mappings by changing one variable. (Important note: you must change mapleader before the mappings are defined; the mapping is static and does not dynamically update based on the variable. Mappings created with <leader> simply use whatever the value is when they are created.)

1

With this you could toggle the highlights:

" toggle highlighted searches
nnoremap <silent><expr> <SPACE>
            \ (v:hlsearch ? ':nohls' : ':set hls')."\n"

Once consequence of this mapping seems to be that if you source your vimrc, the highlighting will be toggled on... I'm not sure why, but if this happens to you, you can fix it by putting the following in your vimrc:

set hlsearch | noh " highlight matches for searched (turn off when sourcing)
2
  • 1
    I think you can skip the test for &hls. If the 'hls' setting is off, than v:hlseearch will be zero. This was changed shortly after v:hlsearch has been introduced May 18 at 12:50
  • 1
    @ChristianBrabandt, Thanks for this! I've removed it from the answer (and from my vimrc!)
    – mattb
    May 18 at 20:28

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