81

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.

5
  • 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, 2011 at 21:43
  • 2
    So either delete your question or post the answer. Mar 18, 2011 at 21:56
  • What if I wanted to give the rep to someone instead of throwing it away? :)
    – VxJasonxV
    Mar 18, 2011 at 21:58
  • 3
    Don't let reputation influence you on whether to post something. Only think whether you're improving the content of the site. Mar 18, 2011 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, 2011 at 22:26

3 Answers 3

88

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.)

9
  • 17
    Esc + u is a very important way to do this on OSX where Alt + u gives you a totally unrelated character.
    – 9000
    Feb 23, 2016 at 21:35
  • 2
    I assume on OSX it's pressing Esc, releasing it, then pressing u, right? Feb 23, 2016 at 23:15
  • Yes; I would like to remove the "+", but it's too late to edit the comment.
    – 9000
    Feb 24, 2016 at 16:01
  • As a workaround, you can just copy it to a new comment, edit that, and delete the old one. Feb 24, 2016 at 20:59
  • 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, 2019 at 14:43
28

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.)

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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