VSCode keyboard shortcut to navigate from search bar to highlighted selection - visual-studio-code

I'm constantly searching code within a file in VSCode.
Is there a keyboard shortcut to navigate from the search bar to the highlighted selection in the editor? That would make things much more efficient for me.
I know I can copy a highlighted selection from the editor to the search bar using CMD + F.

If you are talking about moving from the find widget to the first match, it looks like you have this option, assuming you don't want to just close the find widget with escape:
the workbench.action.focusActiveEditorGroup command is unbound you could use that. But you have to hit escape twice to unselect the find match if you don't want it to remain selected.
That's a pain though so you might a macro that does it all at once. Like (in settings):
"multiCommand.commands": [
{
"command": "multiCommand.focusEditorFromFind",
"sequence": [
"workbench.action.focusActiveEditorGroup",
"cancelSelection",
"cancelSelection",
]
}
]
and keybindings.json:
{
"key": "shift+e",
"command": "multiCommand.focusEditorFromFind",
"when": "editorFocus && findWidgetVisible"
}
So after typing your find term, just Enter until you get to the particular find match you want to switch focus to, and the Shift+E or whatever keybinding you go with above.
Maybe I am just missing it but it seems odd there isn't an easier way to toggle focus from the find widget.

Related

How can i save my current cursor position in Visual Studio Code (VS Code), so that i can come back to it quickly

I want to save my current cursor position, move to some other line (in the same file), do some editing and jump back to the original cursor position to continue coding.
Lets say i am in line 50 and realise i need to add a header/library file at line 5. I want save my position here so that i can jump back after making my changes at line 5. How can i do this?
I have looked into cursor navigation shortcuts but they all move cursor by tracing every position cursor was in, and its time consuming and confusing, instead i want to jump back to saved position in one shot.
You could use the selection anchor. There is no default keybinding for "Go to Selection Anchor" so you'd have to add one yourself.
There is no built-in command for that. The Go Back / Go Forward navigation commands doesn't allows you to save specific locations.
Instead, you should rely on extensions, like my Bookmarks extensions.
There is a bunch of other similar extensions, as you can see on this search https://marketplace.visualstudio.com/search?term=bookmarks&target=VSCode&category=All%20categories&sortBy=Relevance. You should take a look at some, and try out the one that better fit our needs.
Hope this helps
Thanks #scottfrazer for the suggesting selection anchor.
Thanks #alefragnani for suggesting extensions.
Based on these answers, i wrote a keybinding using multi command extension,
After installing the multi command extension,
Add the following snippets in the keybindings.json,
[
{
"key": "ctrl+alt+`",
"command": "extension.multiCommand.execute",
"when": "!selectionAnchorSet",
"args": {
"sequence": [
"editor.action.setSelectionAnchor",
]
}
},
{
"key": "ctrl+alt+`",
"command": "extension.multiCommand.execute",
"when": "selectionAnchorSet",
"args": {
"sequence": [
"editor.action.goToSelectionAnchor",
"editor.action.cancelSelectionAnchor",
]
}
},
]
Above snippet will create a key binding for "ctrl+alt+`" (Ctrl + Alt + BackTick).
When pressed,
Creates a selection anchor at cursor position
If an anchor already exists, Move to that cursor position and deletes that anchor at that position.

Fold Results in VSCode Search Editor (*not* search explorer)

I'm looking for a way to fold all results in a VSCode search editor. (It's clear how to do this in the search explorer.)
I can easily fold individual results with cmd+opt+[ but I want to fold all of the results. I'd actually like to be able to have that be the default.
You can fold all the results by doing this:
Select all Ctrl+A
Command Palette: Create Manual Folding Range From Selection
That Manual Folding Range command has a default keybinding: Ctrl+K Ctrl+,
You could put those two commands into a macro if you wanted, see the macro extension multi-command.
I don't think there is any way to have the Search Editor default to all folded - unless you put however you open that into the macro as well.
Doing that using multi-command, make this keybinding:
{
"key": "ctrl+w", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"interval": 500, // you need some delay to open the editor first
"sequence": [
"search.action.openInEditor",
"editor.action.selectAll",
"editor.createFoldingRangeFromSelection"
]
},
"when": "hasSearchResult && searchViewletFocus"
}
I found the cmd+k, cmd+0 will fold all up to the first line, but then I can open enough to see what I need. Not ideal, but useable.

How can I get line break on enter even when the search field is open (but not focused) in VS Code?

If I have the search field open and press enter in my code it automatically tries to search instead of giving me a line break. I've heard that there is a way to turn off this behavior and only search when the search field has focus. Anyone know how? Currently I have to hit ESC to close the search before I can add a line break and I forget to do it 95% of the time and my editor jumps to a different section of the code. Thanks!
Changing findWidgetVisible to findWidgetFocus in my keyboard shortcuts settings fixed it!
/**
* amVim Finder Fix
**/
{
"key": "enter",
"command": "editor.action.nextMatchFindAction",
"when": "findWidgetFocus"
},
{
"key": "shift+enter",
"command": "editor.action.previousMatchFindAction",
"when": "findWidgetFocus"
},

VS code autocompletion customization - do not automatically select any suggestion

I would like to customize VScode autocompletion behaviour to suit my liking.
Namely:
When the suggestions list appears, I don't want any suggestion to be selected.
When I press Tab and Shift-Tab, I want to cycle through the suggestions (thus selecting one). Esc should unselect any selected suggestion (and can close the suggestions list, optionally).
When a suggestion is selected, any character should accept that suggestion (so edit my code with the text of that suggestion).
Item 2. is achieved by editing the keybinds.
I haven't found a way to get the behaviour of item 1.
For item 3., a dirty hack could be to exploit the editor.acceptSuggestionOnCommitCharacter setting, and using all characters as commit characters, but I haven't found how to edit which characters are commit characters.
Is there a way to achieve this behaviour using the settings?
If not, is there an extension that provides this behaviour?
Ideally, I would like to avoid coding my own extension, but I could resort to that if no other solution is available.
Note: this question is different from this one, because I do not want to press Return to accept a suggestion (unless i want to accept the suggestion and insert a new line).
edit: I believe this answer can implement item 3 using the dirty hack described above, I just have to copy the same keybind for all possible characters with the same "when" conditions, now I just need to find a way to get item 1. (and ideally find a better way to get item 3.).
edit again: to make my question clearer, I've recorded the desired behaviour from vim, with the keys pressed.
For item 1, you could press the up-arrow key de-select a suggestion.
or you could go to VSC, From Visual Studio, select “Tools” > “Options“.
Select “Text Editor” in the left pane.
Select the language you are using (C#, C++, Basic, etc.).
For C# and Basic, choose “IntelliSense“. ...
For C# and Basic, check the “Show completion list after a character is typed” to disable it.
For the 3rd item, you could just not write any parenthesis, < or >, { or }, [ or ], or < and >.
instead, just write what's inside of these. And the auto completion will put every sign in it's right place.
Hope I helped!
I'm also looking for the same. But the closest I came was to preview the suggestion, which I turned on: Put this in your settings.json in VScode
"editor.suggest.preview": true
But I don't think the actual insert feature is available in VScode yet.
for item 2:
first you will need to disable selecting a suggestion with Enter. for that, change the keybinding for "insertSnippet"
{
"key": "enter",
"command": "insertSnippet",
"when": "editorTextFocus && hasSnippetCompletions && !editorTabMovesFocus && !inSnippetMode"
},
then, to add navigation with Tab and Shift + Tab modify "selectNextSuggestion" and "selectPrevSuggestion"
*make sure that both shortcuts have "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
{
"key": "down",
"command": "-selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "shift+tab",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
for exiting the snippet selection Esc should alredy be the default key. If that's not the case, modify the "leaveSnippet" keybinding.
{
"key": "escape",
"command": "leaveSnippet",
"when": "editorTextFocus && inSnippetMode"
},
There is a new approach to how suggestions are handled coming in vscode v1.75, see Implement "Suggestion Mode" from Visual Studio. New setting:
Editor > Suggest: Selection Mode
Controls whether a suggestion is selected when the widget shows. Note
that this only applies to automatically triggered suggestions
(Editor: QUick Suggestions and Editor: Suggest on Trigger Characters) and that a suggestion is always selected when explicitly
invoked, e.g. via Ctrl+Space.
Options:
always Always select a suggestion when automatically Intellisense
never Never Always select a suggestion when automatically Intellisense
whenTriggerCharacter Select a suggestion only when triggering Intellisemse from a trigger character
whenQuickSuggestion Select a suggestion only when triggering Intellisense as you type
Demo 1: modes never and always - note that with never there is no selected item in the suggestion box and when I press Enter or Tab a newline is inserted into the code. Option always has a selected suggestion item automatically and Enter selects and inserts that.
Demo2: mode whenTriggerCharacter, note that although suggestions are shown none are selected and I can Enter and Tab and they are inserted. Only when a trigger character like . is entered is a suggestion selected and Enter and Tab will insert that selected suggestion item into the code (depending on your setting regarding using Enter as a suggestion completion).
Demo3: mode whenQuickSuggestion You will get selected suggestions as you type, except for trigger characters. Trigger characters will still show suggestions but none will be selected so you can Enter and Tab.
Note that in all cases above where there are suggestions shown but none are selected the DownArrow and UpArrow keys will scroll and select items in the suggestions.
There is another demo at the link above: https://github.com/microsoft/vscode/issues/139825#issuecomment-1364056148

Visual Studio Code Surround With

I can't find any way to surround a selection with something in VS Code.
For example doing something like that : text => "text" just by selecting the word text and typing key "
Another example with the following text :
mon
tue
wed
thu
fri
sat
sun
By selecting all of theses words :
mon|
tue|
wed|
thu|
fri|
sat|
sun|
and typing " I would like to perform something like this :
"mon"
"tue"
"wed"
"thu"
"fri"
"sat"
"sun"
Selecting some text and pressing " already works in VSCode to surround a single item, and works for multi-line selections as well.
NOTE: this is language dependent. The language syntax must define opening and closing braces, e.g. quotes, braces, etc. So this will not work in a "plaintext" file, for example. Change your language mode with CTRL+SHIFT+P and type Change Language Mode ENTER and select something like JavaScript where this is supported.
What you are after though is not really that efficient like that. Your best bet is to use multi-cursors.
Place the cursor at the start of the first line, press CTRL+ALT+DOWN to add another cursor below on the next line. Keep doing that until you have a cursor in front of all your words.
Then just type " then END then " and all your lines are surrounded by quotes.
NB: To check if you have a key bound, and what it is, you can always press CTRL+SHIFT+P and type Add Cursor Below and if there's a keybinding it will show to the right of that text.
In VS Code hold Command + Shift + P
then write:
"> Preferences: Open Keyboard Shortcuts (JSON)"
In this area that you are allowed to modify, paste this inside the brackets:
{
"key": "ctrl+p",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
}
** note that in this example the key is set to Ctrl + p, you can change the key to whatever you prefer
Maybe you can try this extension, you can write your own custom wrappers:
https://marketplace.visualstudio.com/items?itemName=yatki.vscode-surround
A simple yet powerful extension to add wrapper templates around your code blocks.
Features
Supports multi selections
Fully customizable
Custom wrapper functions
You can assign shortcuts for each wrapper function separately
Nicely formated
Demo 1: Choosing wrapper function from quick pick menu
Demo 2: Wrapping multi selections
Using Yuri Aps' suggestion, I added the following JSON to keybindings.json. This provides the functionality Ronan Lamour requested for any file type, and without requiring an extension. It works for either single or multiple selections when using either single or double quotes. Coming from Sublime, this is helpful since it reproduces functionality Sublime provides natively.
{
"key": "'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "'${TM_SELECTED_TEXT}'"
}
},
{
"key": "shift+'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
},
I was coming from (neo)vim switching to VS Code, and was using Tim Pope's wonderful "vim-surround" plugin for vim before. I found a port of that plugin for VS Code. It's very useful, and incredibly efficient once you learn the shortcuts, in my opinion!
Links:
Original plugin by Tim Pope for vim
Port of plugin to VS Code
If you use vim or vim bindings in VS Code, please enjoy!
Edit: the VSCodeVim plugin includes the surround functionality automatically, so if you have that plugin installed, you don't really need the vscode-surround plugin.
Update 15-02-2022:
VS Code has introduced Surround with snippets for JS/TS natively.
It may not be totally related with the question but it may help someone who landed in this question with the intent of "surround with" in vs code.
This extension also exists if you want custom surround with text.
https://marketplace.visualstudio.com/items?itemName=sifue.surrounding.
I just installed it and got it working perfectly
Select the word you want to surround it with and enter Ctrl + Alt + T. Then just key in whatever key you want to surround it with.
A more generic solution: in keybindings.json:
{
"key": "alt+m",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$1${TM_SELECTED_TEXT}$1$0"
}
}
Whatever you type after triggering the keybinding will be added to both ends of all selections.
Just tab to the end of the word(s) when you are done and, if you had multiple cursors Esc to remove extra cursors leaving just one.
Since GitHub supports math in Markdown now, I need to wrap my formulas with dollar signs:
$E = mc^2$
When I select a formula and press dollar sign $ on my keyboard I get my formula wrapped automatically. Here is one way to achieve it:
Open Keyboard Shortcuts menu:
Press on Open Keyboards Shortcuts (JSON) button:
In shortucts.json file, which opens, paste this snippet:
{
"key": "shift+4",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$${TM_SELECTED_TEXT}$"
}
}