9

Is there a way to show context for search results in Visual Studio Code?

By default, if I search "debug" for example I might get 2 lines of code returned.

filea.rb
  def debug(str)
fileb.js
  function debug(str) {

I want to see what the code is for, say, 3 lines above and below each match.

filea.rb
  def somefunca
    puts "some func a"
  end

  def debug(str)
    puts str.inspect
  end

  def somefuncb

Is it possible to add context like this to the search results?

Mark
  • 143,421
  • 24
  • 428
  • 436
steel
  • 11,883
  • 7
  • 72
  • 109
  • Visual Studio Code search feature is kind of weird. For instance, it's possibly the only editor where you can't keep search results if you need to do a second search. I'm sure it'll be improved in the future but, for now... – Álvaro González Jul 30 '18 at 20:14

2 Answers2

6

v1.41 is adding a preview of a feature which will display search results in an editor thus allowing for some context lines around the actual search result. See search.enableSearchEditorPreview

Preview: Search Editor

In this milestone, we've started work on showing the results of a search in an editor. This provides much more space to view search results and allows users to maintain multiple collections of search results simultaneously. With this release, in a search editor you can:

  • Navigate to results using Go to Definition-family commands, including Peek Definition and Open Definition to Side.

  • Rerun a search to update the list of results

  • View lines of context surrounding a result

  • Persist results to disk to be referenced later or even tracked in SCM

We will be continuing to add functionality and increase usability in the coming releases.

Note: You can preview this feature by enabling the setting search.enableSearchEditorPreviewstrong text.

preview search in an editor demo


v1.42 is adding a bit more functionality, see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#search-editor. Like selecting the context width around the search result and running another search right in the search editor itself.

search context demo


By the way, you can directly open a search editor without first doing a search in the panel with the command New Search Editor (search.action.openNewEditor) currently unbound (and renamed in v1.48). That command will always open a new Search Editor.

If instead, you wish to re-use a Search Editor (rather than opening a new one), a command is being added to v1.48:

Open Search Editor : search.action.openEditor // also unbound by default


v1.43 release notes https://code.visualstudio.com/updates/v1_43#_search-editors

In a search editor, results can be navigated to using "Go to Definition" actions, such as kb(editor.action.revealDefinition) to open the source location in the current editor group, or kb(editor.action.revealDefinitionAside) to open the location in an editor to the side. Additionally, double clicking can optionally open the source location, configurable with the search.searchEditor.doubleClickBehaviour setting.

search editor image

You can open a new search editor with the Search Editor: Open New Search Editor command, or using the "Open New Search Editor" button at the top of the search viewlet. Alternatively, you can copy your existing results from a search viewlet search over to a search editor with the "Open in Editor" link added to the top of the results tree, or the Search Editor: Open Reuslts in Editor command.

Note You can try out the experimental Search Editor: Apply Changes extension to synchronize edits you make in a search editor back to source files:

search and apply changes

------------------------------- see edit below:

Showing the context lines does not appear to be persistent between uses of the search editor. But Alt+L acts as a toggle to show/hide the context. The value chosen for the number of context lines is persistent.

However, in v1.44 and the Insiders' Build are two new commands fro increasing/decreasing the number of context lines surrounding each search result:

{
  "key": "alt+-",
  "command": "decreaseSearchEditorContextLines",
  "when": "inSearchEditor"
},
{
  "key": "alt+=",
  "command": "increaseSearchEditorContextLines",
  "when": "inSearchEditor"
}

They are unbound by default - these are just sample keybindings. The context lines input box does not need to be visible for these to work. So Alt+L to enable context lines or these new commands to change the number.


In v1.46 there is a new setting to make the amount of context lines shown persistent:

 "search.searchEditor.defaultNumberOfContextLines": 4,  // default is now 1

and

search.searchEditor.reusePriorSearchConfiguration - Reuse the last active search editor's configuration when creating a new search editor

(defaultNumberOfContextLines seems to take precedence over reusePriorSearchConfiguration)

See v1.46 release notes: Search Editor improvements

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Mark, do you know if it's possible to always show the context with the new search editor? It seems like I have to click on "Show Context" every time, and it doesn't remember my choice. – freeall Mar 10 '20 at 10:47
  • @freeall No, it doesn't look like it will remember being enabled. I added an edit to the end of my answer about this - there is a toggle available. – Mark Mar 10 '20 at 14:04
  • thanks for the answer and the command! Annoying it doesn't remember it. – freeall Mar 12 '20 at 12:36
  • 1
    @freeall Looks like a persistent context line setting is coming in v1.46. – Mark May 30 '20 at 00:10
2

You can single click the results which will open the relevant code in a "preview" editor. With the preview you can navigate the results list (clicking, /, ctrl+n/ctrl+p) without opening new editors.

But it sounds like you want to avoid the preview altogether. In that case, here's a feature request, but it looks like it was closed prematurely and needs to be submitted again. The only solution that was actually implemented was a setting for placing the search results in the panel, rather than the sidebar: "search.location": "panel".

jabacchetta
  • 45,013
  • 9
  • 63
  • 75
  • I did find the search location setting, which has helped. I did not realize there was a preview, and being able to scroll through that will help. Not the same as context, but better than nothing. Thank you. – steel Jul 30 '18 at 22:06