Did you know… You can customize how search results are displayed in the Find Results window? – #363


Argh! I wish I had found this one in time for the book. A second edition, maybe? I’d hate to be known as a one-hit wonder. I’m sure there are more people who could use scholarship money.

Ever did a Find in Files and was annoyed that Visual Studio showed the entire file path, forcing you to scroll over just to see the name of the file and the search result?

Find Results 1 before registry change

Here’s what you can do, among some other tweaks. Note: These involve modifying registry settings. Please use at your own risk! Also Note:  You don’t have to restart Visual Studio to pick up on your registry changes. Sweet!!!

  1. Go to HKCU\Software\Microsoft\VisualStudio\9.0\Find
  2. Add a new string called Find result format with a value of $f$e($l,$c):$t\r\n where

$f is the filename

$e is the extension

$l is the line

$c is the column

$t is the text on the line

Now let’s take a look at that Find Results window again:

Find Results 1 after registry change

And here’s the full list of items you can specify in the registry

Files

$p      path                       

$f      filename               

$v      drive/unc share            

$d      dir                        

$n      name                       

$e      .ext                       

Location

$l      line                       

$c      col                        

$x      end col if on first line, else end of first line

$L      span end line

$C      span end col

Text

$0      matched text               

$t      text of first line

$s      summary of hit

$T      text of spanned lines

Char

\n      newline                   

\s      space                     

\t      tab                       

\\      slash                    

\$      $                         

If you come up with a great combination of values, please leave a comment and share with the group!

Comments (17)

  1. Perley says:

    Now if we can get it so that Search Entire Project doesn’t go through the generated xml files or Search All Open Documents only searched the opened documents…

  2. Michael Stum says:

    This is possibly one of the most excellent VS Tips ever. Thanks! 🙂

  3. Antonio says:

    Hey! this one is really good!!!

    I´m really annoyed with this "entire file path" format in the find results window. But not anymore!!! Thanks to you, Sara!!!

  4. Mike Chess says:

    Sara,

    This is one of those things that Visual Studio really needs to expose through a configuration dialog.  The ability to customize the search results shouldn’t be hidden away in a registry hack.  It couldn’t be that hard to add to the Options dialog …

  5. MarcT says:

    Sweet! Secret registry ninja powers! Go Sara Go!

  6. ez says:

    Really hard stuff for today! More like this!

  7. Knobloch says:

    Now, is there any way to get a relative path working? (ArtifactsMyArtifact.cs rather than C:blahblahblah…ArtifactsMyArtifact.cs or just MyArtifact.cs?

  8. redjoy says:

    This is a great one. All my paths are several layers deep. Now it is only one dir deep. 🙂

    Question: Can you put any RegEx in?

    I was thinking that I might want the last portion of the directory plus the filename.

    Instead of:

    c:projectsSolutionDirProjDirFilename.cs(x,y): blah, blah, blah.

    I would rather see:

    ProjDirFilename.cs(x,y): blah, blah, blah.

    Any comments/suggestions?

  9. Sverker Nordlund says:

    Great one! Works prefect!

    Is there anyway to do the same for "Find Symbol Results" for "Find all references"? I tried to add a simular key "Find symbol format" and also "Find reference format", but nothing happend.

  10. Nick says:

    Thank You!!!  Best. Tip. Ever!

  11. Mahendran Shanmugham says:

    Excellent feature!  Thanks Sara!

    Mahendran Shanmugham

  12. Chris Harang says:

    I cannot believe this is not exposed somewhere!  What a missed opportunity for a huge potential added value.

  13. rhettc says:

    Awesome!  I rarely used FiF for that reason and instead used a GREP tool (powerful but seperate from the IDE) or Symbol search when I could.  Like other readers I couldn’t wait to get rid of the path, but since we can include new lines and show a multi-line match, you have space for both.

    My first format string, however, is focused on the search text.  Also, I took liberties in creating another string value in the registry "Find result format_01" just to have a backup while messing around.

    Here’s my first custom format string:

    $lt$nn$0n********************n

    Which is:

    <line num>  <fileNm>

    <SEARCH  MATCH ….wraps as needed>

    *******************

    As the matches are listed the stars serve as your cheap, but effective result boundaries.  Putting the line num before the file creates a more predictable location (imo) then tacking the numbers on at the end of the name/path.

    So… We can start up the find in files window, put it in RegEx Mode, and put the following in the Find What box:

    (~(^:b+’))(~:b).+<From>(.|n)@[^_]$

    Run the search and you get a list of the LINQ queries w/in your search scope.  Using the new formrat string, you can see the entire query.  No more double clicking a result, reviewing the file, and realizing… oh wait, no, not that, maybe this one …  

    A few limitations with that pattern:

    -The expression is written to match VB.

    -There may be a few a false positives.  The RegEx flavor in VS leaves a bit to be desired.  Maybe in 2010 VS will get the same first-class RegEx engine found in the framework.  Probably a licensing issue that’s being worked out as we speak 😉

    -The double-click to go to result will no longer work right after the first result that spans more than one line.  Multi-line results will cause the result window’s index position to get out of sync with the actual results.  It is (seemingly) incremented with each new line in the window.  In fairness, the flexibility of the format string can leave you with results that start and stop in the middle of a line if you aren’t careful.  Maybe this issue has something to do with this format string feature being kept on the D/L.  

    Here’s another pattern for the expanded results window:  Find Catch blocks without an explicit Throw statement.  

    (^{:b+}Catch.*n)((~(^:b+Throw>.*n).*n)@)(^1End:bTry)

    As before, it’s written for VB syntax, but could probably be updated for C# w/o too much work.  The expression is capable of handling nested Try blocks, to a small degree…  It’s not a proper parser, it’s just looking for ‘interesting’ code.

    Last one, I promise, maybe.  This is just a fragment.  You fill in the %BLANK% with the search expresion of your choice and replace the two # characters with the number of context lines above and below your match that will be shown.  Note: if there aren’t enough lines above and below the match then the match attempt fails.  

    ((^.*$n)^#).@(%BLANK%)([^n]+)(n.*)^#

    Other readers asked about including only parts of the path, either display a relative path or the parent folder of the file with results.  I didn’t see any format tokens that would do that.  But, this quick and dirty RegEx could be worked into a VS macro that could do the trick.  The literal characters of the expression defines a root path that will not be captured in any named group.  The ‘Relative’ group gets the folder from the root all the way to the file.  The ‘ParentFolder’ group will get you the name of the folder that contains the file

    (?:^ *C:\Visual Studio Projects\(?<relative>(?<parentFolder>w+\)+)(?<file>w*.w+(?:[.]w+)*)(d+):)s+

    I could provide explanations of the patterns I’ve posted.

    Thanks for tip!

  14. MartinC says:

    Wonderful!  Thank-you.

    This also works on Express versions but the registry keys are different, e.g. for Visual C# 2008 Express it is

    HKCUSoftwareMicrosoftVCSExpress9.0Find

  15. Tip #0 Know your Keybindings! General Development Settings Searching Tips Tip #1 How to behold the power