How to sync search term between tab groups? - visual-studio-code

It's common for me to have two tab groups open to compare things side-by-side but when I am searching for a term I find it a bit annoying that the search term isn't shared by the two editors. e.g. I ctrl-F3 on a word in the file to the left to start searching, then move over to the file on the right and I'd like to just hit F3 in order to continue searching for the same word, but it has it's own term saved to search for.
I assumed that this would be an option somewhere, but I don't see it in the Text Editor -> Find section of the pref's. Am I just blind and not seeing it or does this option not exist in VS Code?

Update: see https://stackoverflow.com/a/65851872/836330 "Searching only in open editors" might work for you.
I don't think there is an option to tie the find's together between groups like that. But with a macro you could do this fairly easily. Using a macro extension like multi-command put this into your settings:
"multiCommand.commands": [
{
"command": "multiCommand.findAcrossGroups",
"sequence": [
"editor.action.addSelectionToNextFindMatch",
"editor.action.clipboardCopyAction",
"workbench.action.focusNextGroup",
"actions.find",
"editor.action.clipboardPasteAction",
"editor.action.nextSelectionMatchFindAction",
"workbench.action.focusPreviousGroup" // to return to first group
]
}
]
and a keybinding to trigger it (in keybindings.json):
{
"key": "alt+s", // whatever binding you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.findAcrossGroups" },
"when": "editorTextFocus"
}
How you would use this is when you trigger the keybinding, whatever word is under your cursor in group 1 will be transferred to the find widget in group 2 (and focus will return to group 1, don't use the last command "workbench.action.focusPreviousGroup" if you want to use this as you move to the next group. And then F3 works as you expect when you switch to group 2.
One downside to this approach is that it needs to use your clipboard to transfer the word under the cursor, that may not be an issue for you.
I made this to work with two groups primarily, although it really uses next/previous groups so it also works for any two adjoining group. It could be generalizeed to three groups pretty easily - beyond that it would probably be easier to make an extension to work with as many groups as you have open.

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.

VSCode: Store and reopen a group of file tabs so that I reset a specific environment in the future

VSCode remembers the file tabs that are open in my Workspace from the last environment, and so if I close VSCode and re-open, I have the same files opened.
Over a day or two of work, I may switch between 2 or 3 different feature branches while my PR's are going through review.
Each feature branch is usually on a totally different area of the code base, which means, I will want to open different groups of files when working on each branch.
I wonder if there is a way to save snapshots of open tabs so that I can re-reopen in future.
A really nice flow would be to have those files open automatically whilst other files close when VSCode detects a branch change.
Get a "snapshot" of currently opened files.
Save this snapshot somewhere; make it easy to change.
Use the snapshot to open all of its files; close all other files first.
Be able to make multiple snapshots and call each one easily.
(1) is harder than you might think. There are a number of vscode issues about searching only within the currently opened files and the problem remains largely unsolved after a few years for this reason.
Demo: get the relative paths of all opened files (unfortunately the gif creation software did a poor job of capturing all the keystrokes used in all these demos) :
Holy crap, what just happened. One keybinding and they are collected and formatted in a specific way.
A number of things happened. The only way I know of to efficiently get a list of opened files (maybe true even in an extension) is through the "Open Editors" view. So we
(a) focus that Open Editors view,
(b) select the entire list, and fortunately there is a
(c) copyRelativeFilePath command (or copyPath for the full path) that will get them all in a list.
But the list initially looks like:
1.html
simple\\gulpfile.js
test1.txt
which isn't going to do us much good. But it is now on the ClipBoard and there is an extension, Replace Rules that is able to run the Clipboard content through a series of regex's (without modifying the Clipboard content either) and paste the result somewhere. So you will need that extension and a macro extension, here using multi-command to run all the steps. Here is the macro which goes into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.getOpenedEditorsForTaskOpenAll",
"interval": 50,
"sequence": [
"workbench.files.action.focusOpenEditorsView",
"list.selectAll",
"copyRelativeFilePath",
// "copyFilePath",
"workbench.action.focusActiveEditorGroup",
{
"command": "replacerules.pasteAndReplace",
"args": {
"ruleName": "Prepare file list for task open"
}
},
"editor.action.formatSelection",
"cancelSelection",
"deleteLeft"
]
}
]
Here is the replacerules rule that is used in the macro:
"replacerules.rules": {
"Prepare file list for task open": {
"find": ["(\\\\)", "^(.+)$"],
"replace": ["\\\\", "\"'$1'\","]
}
}
It just modifies that bare file list into something we can use in a task. Here is a keybinding to run that macro (keybindings.json):
{
"key": "ctrl+shift+i",
"command": "extension.multiCommand.execute",
"args": {
"command": "multiCommand.getOpenedEditorsForTaskOpenAll"
},
}
You should be able to test this already to see if it'll dump the formatted file list to wherever your cursor is in the current text editor.
One way to open up all these files at once is to put them into a task (in tasks.json):
{
"label": "Open snapshot 1",
"command": "code",
"args": [
],
"type": "shell",
"problemMatcher": [],
"dependsOrder": "sequence",
"dependsOn": [ "close all editors" ]
},
{
"label": "close all editors",
"command": "${command:workbench.action.closeAllEditors}",
"type": "shell",
"problemMatcher": []
},
You see the task Open snapshot 1 is dependent on the close all editors task so that happens first. In the Open snapshot 1 the command is code to open all the arg files. Put your cursor in the args array and that is where the properly formatted list of files to open will be written by the macro. Demo:
If you want to update that file list you can just select them and rerun the macro. And you can now set up as many Open snapshot <n> tasks as you want (with whatever labels you want to give them).
Now, to trigger the task we will use a keybinding as well:
{
"key": "alt+s 1",
"command": "workbench.action.tasks.runTask",
"args": "Open snapshot 1"
},
{
"key": "alt+s 2",
"command": "workbench.action.tasks.runTask",
"args": "Open snapshot 2"
},
etc. As noted earlier, running this task will first run the dependent task which closes all currently opened files. If you just wanted to open a batch of files you frequently use without closing the others, just remove the "dependsOn": [ "close all editors" ] option.
Here is a final demo of the task closing the open files and opening the snapshot files (I just changed the file list above a little to make it look different).
Two things to remember:
(1) the Editor > Open Editors: Visible setting must be enable with a number high enough to show all your opened files. The Open Editors can be hidden so you don't have to look at it all the time if you don't want, but it will be opened automatically by the macro - that can't be avoided. You can see it opening in the demos. It can be hidden by its context menu.
(2) The terminal is used, so you see it opening.
It seems like a lot of set-up but the operation is actually pretty simple - just a couple of keybindings to remember.
Try an extension called File Group.
My searching reveled that a lot of people are looking for this option but vsCode does not seem to have any good way to do it. This extension lets you list out the files with full path and associate them to a key shortcut.
Three files below will load by hitting ctrl-alt-1 if you add this to youProject.code-workspace:
"1": {
"files": [
"C:\\temp\\file1.txt",
"C:\\svn\\foo.txt",
"C:\inetpub\wwwroot\iisstart.htm"
]
}
(It is a pain to set up if you are new to vsCode, hopefully it gets developed further.)
This worked for me. But it may have side affects when git does refresh
https://marketplace.visualstudio.com/items?itemName=gkotas.restore-git-branch-tabs
It seems that there is an extension that does the job: Editor Group Minimizer
There's this extension, that seems to kind of work:
Save and Restore Tabs
It seems to get most of the files and maintains the file split, but if I have multiple tabs on multiple groups, sometimes the secondary ones aren't opened.
There's an extension that would save your open tabs and swap them based on your git branch. Seems very useful, but in practice it was a little too intrusive.
Looks like its been abandoned, but there's this fork: Restore Git Branch Tabs Improved Tried it but isn't worked like I remember but maybe I just missed something for setup.

VSCode keyboard shortcut to navigate from search bar to highlighted selection

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.

Can I define a task for Replace Operation in vscode?

I replace every debugger; phrase with "" in my solution some times a day. I thought it could be well if there is a way to define a task for this operation.
Do you know the solution. is there this feature?
You will need a macro extension such as multi-command. It will allow you to chain commands together to run with one keybinding.
In your settings.json:
{
"command": "multiCommand.removeDebugger",
// "interval": 250,
"sequence": [
"workbench.action.findInFiles",
// "toggleSearchRegex", // depending if the default is regex on or off
// and where you want it to end up
"search.action.refreshSearchResults",
"workbench.action.replaceInFiles",
"search.focus.nextInputBox",
"editor.action.clipboardCutAction",
"search.action.replaceAll"
]
},
This will open the "Find In Files" panel. Then it will run the command to actually search for your chosen string across files (which is a necessary step before doing a replace). Then it will move to the "replace input box", clear its contents (since you want to replace "debugger;" with nothing) and run the replace in all files command. VSCode will prompt you if you really want to do it.
In your keybindings.json put some keybinding of your choice, such as:
{
"key": "ctrl+alt+u",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.removeDebugger" }
},
The only requirement is that you highlight the phrase you want to search for first - in your case debugger;- and then trigger it with Ctrl-Alt-U or whatever.
Here is a demo gif of it working - I slowed it way down so it could seen going through the steps:
. The gif software is wonky on the keystrokes - it is just Ctrl-Alt-U.
I have created a VSCode extension for this task. It registers a command Remove Debugger Statements bound to ctrl+alt+shift+d on windows and ctrl+cmd+shift+d on mac to perform the required action.
Additionally, it also has settings to specify the files/folders to include/exclude while performing the action.