92

When you use a / forward search or a ? backward search in less, all instances of the file get highlighted. After I've found the instance of the word I'm looking for, what is the most correct way to unhighlight something?

Currently I just press / then mash gibberish into the input field. No results = no highlights!

I'm looking for something akin to vim's :nohl feature, in less.

Volker Siegel
  • 17,283
VxJasonxV
  • 1,567
  • And of course, immediately after asking this question, I start hitting the related questions and ultimately come up with the answer via (where else), the man page for less... Guess this question just became rep fodder. – VxJasonxV Mar 18 '11 at 21:43
  • 2
    So either delete your question or post the answer. – Gilles 'SO- stop being evil' Mar 18 '11 at 21:56
  • What if I wanted to give the rep to someone instead of throwing it away? :) – VxJasonxV Mar 18 '11 at 21:58
  • 4
    Don't let reputation influence you on whether to post something. Only think whether you're improving the content of the site. – Gilles 'SO- stop being evil' Mar 18 '11 at 22:04
  • I consider cjm's content an improvement, and he got rep to show it. I'm not saying this to rub it in, I'm pointing it out because it's dually beneficial to a user. I didn't post the question for the rep either, I just found the answer on my own after the fact. – VxJasonxV Mar 18 '11 at 22:26

3 Answers3

101

You can use Alt+u to remove the highlight on last search results.
You can highlight them again with Alt+u, it's a toggle.

Switching off the highlight does not switch off the status column, showing marks on each line containing a match, if the column is enabled using options -J or --status-column or keys -J.

To hide the status column, use -+J.

To show the status column, use -J.

(Technically, Alt+u it's equivalent to ESCu on terminal level - that is why the Alt-key is not mentioned in the man page.)

Volker Siegel
  • 17,283
  • 23
    Esc + u is a very important way to do this on OSX where Alt + u gives you a totally unrelated character. – 9000 Feb 23 '16 at 21:35
  • 2
    I assume on OSX it's pressing Esc, releasing it, then pressing u, right? – Volker Siegel Feb 23 '16 at 23:15
  • Yes; I would like to remove the "+", but it's too late to edit the comment. – 9000 Feb 24 '16 at 16:01
  • As a workaround, you can just copy it to a new comment, edit that, and delete the old one. – Volker Siegel Feb 24 '16 at 20:59
  • Either that, or you could just add a notice about OS X to to answer. – 9000 Feb 25 '16 at 02:56
  • Actually, that is what I planed - there is somewhere a Firefox tab with an almost finished edit, but not yet saved... Hey, it's easy to get distracted on StackExchange... So, why don't i just finish it now? I have other unfinished comments and edits... In short: Yes, I agree! – Volker Siegel Feb 25 '16 at 04:06
  • For me Alt+u works for clearing the search highlights, but it does not work as a toggle. When I press the combo initially, it clears the highlighting, when I press it again, it doesn't seem to do anything, and when I press it a third time, it displays "No previous regular expression (press RETURN)" in the status line. I'm on Arch Linux with less version 530. – Stefan van den Akker Apr 03 '19 at 06:50
  • 4
    That difference has nothing to do with OS X, but with your terminal emulator. iTerm for example has the ability to assign Alt something that is in accordance with the behavior described in this post. – rien333 Jun 14 '19 at 14:43
  • @rien333 This can also be done in macOS's native terminal: Terminal > Preferences > Profiles > Keyboard > Use Option as Meta key – Jivan Pal Nov 05 '21 at 10:50
31

From man less:

ESC-u

Undo search highlighting. Turn off highlighting of strings matching the current search pattern. If highlighting is already off because of a previous ESC-u command, turn highlighting back on. Any search command will also turn highlighting back on. (Highlighting can also be disabled by toggling the -G option; in that case search commands do not turn highlighting back on.)

cjm
  • 27,160
  • For those who like me didn't understand at first, it's ESC and u at the same time. The hyphen is just a separator. – Nuno Lopes Oct 10 '23 at 10:05
2

Since version 580 from 2021-03-04 there is ESC-U (note the uppercase “U”), which removes inline highlights as well as marks on the status column:

ESC-U: Like ESC-u but also clears the saved search pattern. If the status column is enabled via the -J option, this clears all search matches marked in the status column.

Note that, unlike ESC-u, this does not toggle. Therefore, without a status column, ESC-u is probably the better option, which is also easier to type:

ESC-u: Undo search highlighting. Turn off highlighting of strings matching the current search pattern. If highlighting is already off because of a previous ESC-u command, turn highlighting back on. Any search command will also turn highlighting back on. (Highlighting can also be disabled by toggling the -G option; in that case search commands do not turn highlighting back on.)

(The citations are from the man page of version 590.)