5

In trying to search for the literal string ***, less puts up a couple roadblocks. The asterisk has a special meaning when used as the first character of the search pattern (ignore EOF). No problem, just escape it with a backslash. Now, since I'm searching for a literal string, I don't want the asterisks treated as regex special characters. Again, no problem: starting a search with ^R turns off regex searching.

See the problem yet? With regex off, there's no way to escape the initial asterisk! So my only choice seems to be: keep regex enabled, and either individually escape each asterisk (i.e. I have to type /\*\*\*), or use something like /\*{3}, neither of which is particularly pleasant to type. Other than relying on custom key bindings in ~/.lesskey, is there a better way to do this?

5
  • 1
    I'm struggling to imagine what would be easier than /\*\*\*. Did you have something in mind? Commented Feb 1, 2023 at 14:25
  • @jesse_b, OP mentioned ^R which normally allows you not to have to worry about what character may be a regexp operator, but here doesn't work. Commented Feb 1, 2023 at 14:32
  • 2
    You can do /<Ctrl-R> ***<Home><Delete><Enter> (ie add a leading space and remove it after). Not great either. Commented Feb 1, 2023 at 14:36
  • @jesse_b, I had hoped that something like /\*** or /^R*** would work, but no. Commented Feb 1, 2023 at 15:40
  • @dhm, /\*** matches 0 or more *s (so including the empty string, so everywhere) without ^R and \*** with ^R Commented Feb 1, 2023 at 16:19

3 Answers 3

1

You are looking for the literal string *** using less. You could just pipe the file into grep. Something like this...

$ cat stars.txt
*
**
***
****
text***text
***text***
**should not match**

$ less stars.txt | grep '\*\*\*' 
***
****
text***text
***text***

Also using grep manually without less...

$ grep '\*\*\*' stars.txt

The * character has special meaning in the regex world. It is a quantifier meaning find zero or more instances of the preceding character or sequence. If you want to match the literal * character in a string, you will have to escape it. I dont think there is a way around that.

2
  • That would work, but I want to remain in 'less' so I can scroll around elsewhere in the file. Commented yesterday
  • It might be a good idea to consider using a text editor instead of less. In emacs you can accomplish this using C-M-S-s then type \*\*\*. To show all matches at once in a separate buffer, M-x occur then type \*\*\*. Commented yesterday
0

I solved my problem by adding this to my .lesskey file:

#command
z forw-search \\*\\*\\*\n
Z back-search \\*\\*\\*\n

Now I just type 'z' or 'Z' to search forward or backward for '***'.

0

Yes, a number of characters, not just * are handled specially if entered at the start, even after ^R to turn regexp off (^R being one of them along with !, *, @ and all the control ones listed under /regex in the man page).

Some work arounds:

  • after /^R, enter an initial space before the pattern and delete it afterwards (by pressing Home then Delete). In the case of ***, that's /^R ***<Home><Del>.
  • If less was built with --with-regex=pcre2 (or pcre, though that library is now deprecated, superseded by pcre2), you can do a regex search (with /, without ^R) for \Q***\E where what's between \Quote and \End is not treated as regexp operators.

In the case of ***, neither of them is shorter or easier to type than /\*\*\* or /\*{3}. That would be more useful in the general case to search for arbitrary strings without having to figure out which character needs escaping.

Also note that for several control characters (the ones listed in the LINE EDITING section of the man page), you need to prefix them with ^A or ^V to enter them literally in the search string.

Another alternative would be to use view instead of less. With the vim implementation, you can prefix the pattern entered after / with \V (very nomagic) after which only \ has a special meaning. /\V*** for ***.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.