3

When I do git diff HEAD^! (to diff changes in the HEAD commit), output is piped to LESS. I need to search the contents of the diff for /* (start of C-style comment). I haven't been able to get this working. I hit the forward-slash key on my keyboard to begin searching. I've tried:

//*
//\*
/\/\*

None of these work (first / is to initiate search mode in LESS).

fedorqui
  • 275,237
  • 103
  • 548
  • 598
void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • `[/][*]` is one easy approach -- single-character classes have the advantage of being hard to misinterpret and safe against unescaping. – Charles Duffy Jun 30 '15 at 23:04
  • 1
    Also, boo hiss re: asking this here rather than SuperUser. – Charles Duffy Jun 30 '15 at 23:04
  • @CharlesDuffy Could also be on the Linux SE or Programmers too? Sometimes things aren't as clear cut as they should be. – void.pointer Jun 30 '15 at 23:05
  • Hmm. I'd lean towards SU myself -- it's not a programming-specific tool in any way, and neither is it Linux-specific (since GNU tools are pretty much everywhere). Unix SE, maybe. – Charles Duffy Jun 30 '15 at 23:07
  • Yet it is perfectly acceptable to ask questions about git itself on SO. It's just not clear at all where things should go sometimes. – void.pointer Jul 01 '15 at 00:01
  • Git is explicitly a programming tool. I mean, yes, it's a distributed filesystem if considered more abstractly, but the explicit target audience is software developers (initially, kernel developers). – Charles Duffy Jul 01 '15 at 00:01
  • (From the tour in the help center, at http://stackoverflow.com/tour: "Don't ask about [...] anything not directly related to writing computer programs"). – Charles Duffy Jul 01 '15 at 00:04

2 Answers2

10

Borrowing from this fine answer, after you press / to start the search, you can press Ctrl + R to disable regexes.

This is great if you want to paste in a long string full of special characters to search for and don't want to have to go through escaping each one by hand!

Malvineous
  • 25,144
  • 16
  • 116
  • 151
7

When you enter / in less, the following expression is a regex. As such, * has special meaning there; /* thus searches for zero-or-more instances of /.

To prevent any character from having special meaning, you can enter it as a character class:

/[/][*]

...ensures that both of the characters you're searching for are treated literally. This works in other regular-expression contexts (grep, etc) as well.


That said, the above is somewhat more paranoid than necessary. In my tests, the below work as well:

//[*]
//\*
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Are backslashes not applicable here or do they add more chaos to the madness? – void.pointer Jun 30 '15 at 23:07
  • The backslashes should work -- I can't reproduce the case where they don't (with less 418 or 458). That said, I tend to prefer the character-class approach simply because it's less error-prone: There are several situations where backslashes can be destroyed by unintended unescaping. – Charles Duffy Jun 30 '15 at 23:09