Visual Studio Code - Include context in search results
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.enableSearchEditorPreview
strong text.
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.
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 thesearch.searchEditor.doubleClickBehaviour
setting.
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:
------------------------------- 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
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"
.