How to prevent VS Code from deleting the next word characters on IntelliSense auto-completion? - visual-studio-code

This question is analogous to How to keep Visual Studio autocomplete from overwriting the next word,
but targeted at Visual Studio Code instead of Visual Studio.
When a completion suggestion is selected from the list, it is inserted, but all characters from the word after the cursor are deleted. (So, nothing happens if there is a whitespace after the cursor. But if autocompletion is triggered while the cursor is placed at the beginning of a word, said word will be deleted).
Is there a way to disable this deletion behavior and get it to add the selected suggestion without deleting text to the right of the caret?

Check your settings.json files (your user settings.json and your workspace .vscode/settings.json).
You probably have a line that says:
"editor.suggest.insertMode": "replace"
You can either delete it to get the default behaviour, which is "insert" instead of "replace", or just change it to "insert".
The setting's description says:
Controls whether words are overwritten when accepting completions. Note that this depends on extensions opting into this feature.
The "insert" value's description says:
Insert suggestion without overwriting text right of the cursor.
The "replace" value's description says:
Insert suggestion and overwrite text right of the cursor.
For some languages, the default might be changed. You can check all default settings by viewing the defaultSettings.json file using the Preferences: Open Default Settings (JSON) command.
To set settings per-language, enclose them in blocks like this (example for C++):
"[cpp]": {
"editor.suggest.insertMode": "insert"
}

Related

How to disable highlight of full string in VS Code?

I have an issue with double clicking and highlighting in VS Code (v 1.71.2).
Sometimes the double-click does not select the word double-clicked.
In this screen recording you can see the issue:
Every time I used a double click.
First time the word "Condition" was not selected, but the whole string literal.
Then it was selected but the whole line was highlighted and when copying, only "Condition" would be pasted. That is correct.
The time that the word "Condition" was colored in fuchsia is the correct one but I want only that word to be highlighted instead of the entire string.
Anyone know the setting for this?
This issue was reported and fixed already, see:
Double Clicking On A Word Does Select The Entire String, Not Just The Word · Issue #125052 · microsoft/vscode · GitHub
Try to re-install the version 1.71.2 for release "August 2022 Recovery 2" and see if double-click selection works as expected.
Note that different click-counts have different outcomes:
double-click should select the word (using configured word-separators)
triple-click should select the entire string
quadruple-click should select the entire text
Configure word-separators with property editor.wordSeparators from preferences: Visual Studio Code—Customizing word separators
See also related: Double-click to select text does not always work · Issue #84319 · microsoft/vscode · GitHub.
To have finer control over selecting text you can use keyboard shortcuts like explained in Select all text between quotes in VS Code?.
I concluded that the issue was that the string was highlighted due to the cursor being placed somewhere inside the string.
To disable this, I disabled the Occurrences Highlight ("editor.occurrencesHighlight": false)
By doing so, the editor did not highlight open/closing tags though.
The (partial) solution to this was to install the extension Highlight Matching Tag

VSCode will copy full line when only a word is selected (single click)

I'm on OSX and running Version 1.42.0 of Visual Studio Code. I have noticed that when I single click a word it will highlight. But if I copy CMD + c and then paste CMD+v, the full line will be in the clipboard. This causes problems from time to time, when the screen has given me every indication that I have only selected a single word. Is there some setting that I can set that will make the default behavior to select a word on a single quote and never ghost select a full line?
What it looks like when I single click a word:
And what it looks like after I've copied and pasted:
After filing an issue, it turns out that this behavior is by design.
The word the cursor is on (from a single click) is highlighted along with every occurrence of that word. The word is not selected (that would be a deeper blue).
By default copying without a selection copies the current line.
This in my opinion is an Accessibility issue, as there are strong visual clues that a word is selected. I have found that the behavior can be made more intuitive if you set these to settings.
// Controls whether copying without a selection copies the current line.
"editor.emptySelectionClipboard": false,
// Controls whether the editor should highlight semantic symbol occurrences.
"editor.occurrencesHighlight": false
With these two settings the word the cursor is on will not be highlighted (nor any other occurrences of that word). And if you do happen to copy, with an empty selection, the editor will behave similar to how other applications behave and not copy the current line.

Any way to manually trigger highlighting of semantic symbol occurrence in Visual Studio Code?

In VSCode, when you place cursor inside a symbol (variables, functions, etc), all occurrence of the same symbol will be highlighted.
This feature is somewhat useful but annoying as well. Even I can make it less obtrusive by customizing the color theme in settings.json, it will suppress the selection highlighting when you select a variable by double clicking it with mouse cursor.
I've learned that I can completely disable this feature by adding "editor.occurrencesHighlight":false in settings.json, but this feature is still useful because it can label occurrences of a symbol with different color, to represent read/write status of each occurrence.
So my question is: is there any way to disable the automatic semantic matching feature, and only enable it manually with keyboard shortcuts or commands ?
If you only need textual matches, you can select some text use the Select all occurrences of find match command. This will select every occurrence of the selected text in the current document (and also create a cursor at it)
For symbol based information, try using the Find all references or Peek references commands. The flow is different but it gives the same information.
Alternatively, use an extension like this one to create a keyboard shortcut that toggles editor.occurrencesHighlight

Visual Studio Code - Include context in search results

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?
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.
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 the
search.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".

How to filter files shown in Visual Studio Code?

How would you filter the files shown in the folder view in Visual Studio Code?
Reference: Filter files shown in folder
Hiding files and folders
The files.exclude setting lets you define patterns to hide files and folders from several places in VS Code like the explorer and search. Once defined, files and folders matching any of the patterns will be hidden.
{
"files.exclude": {
"**/*.js": true
}
}
Hide derived resources
If you use a language that compiles to another file at the same location of the source file, like TypeScript does to JavaScript, you can easily set an expression to hide those derived files:
"**/*.js": { "when": "$(basename).ts"}
Such a pattern will match on any JavaScript file (**/*.js), but only if a sibling file with the same name and extension, *.ts in this example, is present. The same technique can be used for other transpiled languages, like Coffee Script or Less/Sass, too.
Source: VS Code v0.5.0 (July 2015)
In version after VScode 1.70 (July 2022) all you need to do is press Ctrl+F or F3 to search.
Please refer following post
Searching in Explorer panel after VSCode 1.70
Only applicable for v1.69 and below.
Step #1
Click on Explorer window. This is critical as without focus on Explorer it will not work.
Step #2
Start Typing name you want to filter. It's weird that there is no textbox to take input but... take a leap of faith and type. You will see your typed text in top-right corner in brown background. Now hover on that text.
Step #3
Click on 3 stacked lines to filter..
They look like hamburger menu but they are not. They are saying if it's filtered or not. They are toggled between filtered and just highlight. So, make sure they are like inverted pyramid.
That's it. It should be filtered now.
If you only want to change the setting for this project, then do the following:
File > Save Workspace As > ... enter your {project name}
Then open file: {project name}.code-workspace
And update section settings.
Sample:
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.exclude": {
"**/*.log": true
}
}
}
VScode 1.70 (July 2022) should improve on this "tree filter" feature.
(Available today in Code insiders)
See issue 70646 and PR 152481:
Support find widget in lists/trees
This PR replaces the existing list/tree type filter:
with an bona fide find widget:
While a seemingly simple change, this has some (desired) consequences when searching/filtering in trees. Namely:
We will restore simple keyboard navigation by default.
That is: pressing the letter A will focus the next element which starts with A.
Initiating a search operation requires pressing Ctrl-F or F3, like the editor.
While searching, focus is in the find input box as opposed to the list itself.
Pressing DownArrow will focus the first list element which was found.
We'll preserve all custom behavior of context keys, eg. used by the VIM extension).
In VIM, the pre-existing / command will trigger simple keyboard navigation, as opposed to opening the find widget.
The VIM extension has the option to change this behavior themselves.
And:
In general:
Keyboard navigation is now called type navigation
Filter on type is now called find mode, aligned with a new find concept
Settings
workbench.list.keyboardNavigation has been renamed to workbench.list.defaultFindMode
workbench.list.automaticKeyboardNavigation has been deleted
Commands
list.toggleKeyboardNavigation has been renamed to list.triggerTypeNavigation
list.find has been added
list.closeFind has been added
list.toggleFilterOnType has been renamed to list.toggleFindMode
Context Keys
Mainly used by the vim extension:
listSupportsKeyboardNavigation has been renamed to listSupportsTypeNavigation
listAutomaticKeyboardNavigation has been renamed to listTypeNavigationMode
"With the focus on the File Explorer start to type part of the file name you want to match.You will see a filter box in the top-right of the File Explorer showing what you have typed so far and matching file names will be highlighted."
"Hovering over the filter box and selecting Enable Filter on Type will show only matching files/folders."
documentation: https://code.visualstudio.com/docs/getstarted/userinterface#_filtering-the-document-tree