Fold Results in VSCode Search Editor (*not* search explorer) - visual-studio-code

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.

Related

Is there a way to fold all groups of lines that start with a certain word for a .txt file in Visual Studio Code?

I've been using Visual Studio Code version 1.74.3 on Windows 10 to take all my math notes. So my txt file is mainly composed of the following structure:
[Theorem]
[Indentation] Proof:
[Indentation] [Indentation] [Proof per see]
Here is an example: https://imgur.com/a/2MD3EQq
I want to fold all the groups of lines that start with the word "Proof" (it occurs 3454 times as of now) because most of the time I don't need to see it, and they're usually quite lengthy. I want to collapse all the lines immediately below each Proof line that share the same indentation. It's also the case that every proof ends with the character "∎".
The problem is that the file is very long (75k lines) and subdivided into many different regions with different indentation levels, so I cannot just use the "Fold by level K" option, since not all proofs are at the same level.
Therefore, folding all the lines that start with the word "Proof" seems to be the way to go.
I've read the section https://code.visualstudio.com/docs/editor/codebasics#_folding for a way to fold all the lines that start with the word "Proof", but I couldn't find any.
Does anyone know how to do it, or have any other suggestion?
Thanks in advance! :-)
This can be done thanks to the command editor.createFoldingRangeFromSelection which will fold anything you can select. It looks like this find would select each of your proof-groups:
^[\t ]*Proof[\s\S\n]*?∎ // I didn't thoroughly test this
So you could (1) find those using the Find Widget, then
(2) Alt+Enter (the editor.action.selectAllMatches command will select each find match separately) and then
(3) Ctrl+k Ctrl+, to apply the editor.createFoldingRangeFromSelection command and it should fold each of those find matches.
To automate this a bit, you can use an extension I wrote, Find and Transform, and this keybinding (in your keybindings.json):
{
"key": "alt+c", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "^[\\t ]*Proof[\\s\\S\n]*?∎", // need double-escaping here
"isRegex": true,
"postCommands": [
"editor.action.selectAllMatches",
"editor.createFoldingRangeFromSelection"
]
},
"when": "editorTextFocus && !editorReadonly && editorLangId == plaintext"
}
It does the same find, separately selects each match and then folds it.
The command Remove Manual Folding Ranges will unfold any selected manually folded range. So if you wanted to unfold all your proof-ranges at once, this keybinding would work:
{
"key": "alt+d", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "^[\\t ]*Proof[\\s\\S\n]*?∎",
"isRegex": true,
"postCommands": [
"editor.action.selectAllMatches",
"editor.removeManualFoldingRanges"
]
},
"when": "editorTextFocus && !editorReadonly && editorLangId == plaintext"
}

Keyboard shortcut in VSCode for Markdown links?

from other text editors I'm used to adding Markdown links by
selecting the word I want to be linked,
pressing cmd-K on my Mac's / iPad Pro's keyboard, which puts square brackets around the marked word, appends a pair of normal parenthesis () and places the cursor right in beetween those two parenthesis so that I can
just paste the URL I have in my clipboard into the right place by pressing cmd-V.
So, select -> cmd-K -> cmd-V is a nice and short sequence for adding links in a Markdown document and cmd-K has become some kind of pseudo standard for adding links in several writing apps.
However, in VSCode that's not possible. But I'd love to make it possible. Any ideas? cmd-K is (hard-wired?) bound to listen for a next key press.
But it doesn't have to be cmd-K. I can learn another keystroke. But I need to be able to put additional text (square brackets and parenthesis) into the text and move the cursor to the right position. How's that done?
Thanks so much!
This extension Markdown All In One looks like it does what you want in one step.
Paste link on selected text
Just select your link and hit Ctrl+V and it creates the link and inserts the clipboard link.
If for some reason you don't want to use this extension, it would be pretty easy to create a snippet to do what you want.
Adding another answer that doesn't use the extension Markdown All In One I mentioned in the other answer and because a couple of commenters requested a different way. #MarcoLackovic
First Method: keybinding in keybindings.json, then manually paste
{
"key": "alt+w", // use whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
},
"when": "editorHasSelection && editorLangId == markdown "
}
Select the link text and, trigger your keybinding - the cursor will be placed where you want it and paste.
Second Method: use a macro to insert the snippet and paste in one step
You will need a macro extension like multi-command to run multiple commands in series. Then this keybinding:
{
"key": "alt+w",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
}
},
"editor.action.clipboardPasteAction"
]
},
"when": "editorHasSelection && editorLangId == markdown "
}
Demo of second method:

How to Create Custom Key Binded Snippets in VS Code

I am a huge Sublime Text user, and learned ways to improve my productivity using customizations in Sublime text. But as VScode is becoming popular day by day, wanted to check if there is any way which I can bind the shortcut keys to the custom actions.
For example, I select a word ABC in any file in VSCode and hit CTRL+B, and it places my own defined values around it like it should become
<b>ABC</b>
I had created the following snippet in Sublime Text, which when I wrote in Visual Studio Code - keybindings.json nothing worked.
{
"keys": [
"ctrl+b"
],
"command": "insert_snippet",
"args": {
"contents": "<b>${0:$SELECTION}</b>"
}
}
This will work in your keybindings.json:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"when": "resourceExtname == .html", // this is optional
"args": {
"snippet": "<b>${TM_SELECTED_TEXT}</b>"
}
},
The optional when clause is if you want to limit the snippet's operation to .html files.
More general though is to use the emmet command which is built-in: Emmet: Wrap with Abbreviation in the command palette. Select your text, open the command palette, find that command and trigger it - type b or whatever your element is and it will wrap the selected text with the opening and closing elements.
[Note that there is a command workbench.action.toggleSidebarVisibility already bound to Ctrl-B, but the snippet above version seems to take precedence - meaning you lose the toggleSidebarVisibility keybinding functionality - that may be acceptable to you?]

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.

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}$"
}
}