Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to toggle Vim's search highlight visibility without disabling it
    primarykey
    data
    text
    <p>What I'd like is to map one key, e.g. <kbd>F4</kbd>, so that pressing <kbd>F4</kbd> will toggle the visibility of search highlights, <em>and</em> so that starting a new search enables visibility no matter the current visibility.</p> <p>What I've tried:</p> <ol> <li>Mapping <kbd>F4</kbd> to <code>:nohlsearch</code> temporarily disables highlight visibility without turning the <code>hlsearch</code> setting off, but it does not toggle visibility back again.</li> <li>Mapping <kbd>F4</kbd> to <code>:set hlsearch!</code> does toggle on/off, but I don't want to toggle the <code>hlsearch</code> setting off, just the visibility setting. If <code>hlsearch</code> is off then it doesn't come back automatically with a new search.</li> </ol> <p>There doesn't seem to be an opposite form of <code>:nohlsearch</code> and the command itself has problems being called from a function.</p> <p>I've found similiar questions, but they don't provide an answer.</p> <p><strong>Update:</strong><br> The first comment provides exactly what I was asking for, reproduced below: </p> <pre><code>let hlstate=0 nnoremap &lt;F4&gt; :if (hlstate == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=1-hlstate&lt;cr&gt; </code></pre> <p>(N.B. for anyone using this --- cramming the map onto one line instead of using a function is necessary since you can't effect a change on highlighting from inside a function.)</p> <p>Related question for slightly different functionality: <a href="https://stackoverflow.com/a/16750393/1176650">https://stackoverflow.com/a/16750393/1176650</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload