VS Code: multi line keybindings - visual-studio-code

I use a shortcut with keybindings.json to put a single line into quotes, ending with a comma.
text --> "text", this works well, but only for one line.
Now I have a long list of text and would like to put every line into quotes
like this
text1 --> "text1",
text2 --> "text2",
text3 --> "text3",
text4 --> "text4",
Is there a way to do it? Help highly appreicated, because I would gain a lot of time, if i dont have to do it line by line.
Best endo
edit:
here is the snippet from keybindings.json which puts every line in brackets
[
{
"key": "cmd+,",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\", "
}
}
]

try to use multi line selection with CTRL + ALT + Up/Down arrow and then, after you selected the rows, apply that shortcut of your

Related

Formating Visual Studio Column Jump

I'd like to edit my Visual Studio Code.
When I write my symbols on my keyboard to VSC, they'll get written Column by Column on the current Row.
\ Line of Code + Column.
(Ln , Col )
Extensions: "Select By"
= to jump to a certain column.
However, when i like to jump to 'column 50', it will only jump to the >End of Line.
// Example
// Goal is to get a Hotkey to Jump to Column 50
// Activated Hotkey*
cout << "Hello World:" << endl; // If i use my Hotkey i get to the Column 30, not 50. Picture is enhanced*
/* The end of line is here: 30. */
Question: How can I achieve my Jump
via the given Extension? (Any easy solution would be helpful).
I don't like to use the Ln, Col in Vsc, because clicking on the UI is for me over the time to slow.
I'd like to use f.e. Cmd + M to get to my "middle", So for me column 50.
Hence, i don't like to go to Middle of my current Screenformat.
Additional prefered Question: One way how i like to solve the issue is in C64 Style.
Is there any way to fill out the Visual Studio Code Screen by empty chars?
So as an Preset.
I know, that i can just copy & Paste. Or write an empty script. But that doesn't seem convenient inside the Configuration. I tryed to find that now unsucessfully since 2 weeks..
The reason is for me, that i currently try to code more cleaner.
And to achieve that (I have read many Clean Code Books during school and Github), i just like to try this time an historical approach by setting my own column points/ marks. (Basic C64-menu wise)
Thank you in advance!
Sincerely
Edit: I have enhanced beforehand.
Edit2: I have tryed the Find and Transform Extension pointed by Mark. Thank you for your time!
When i press the given Hotkey, It does marks the previous chars. but it doesn't start at column 50.
It marks at end of line.
I only like to move the cursor to column 50 and surpress the end of line.
does this key binding work
{
"key": "ctrl+i ctrl+m", // or any other key binding
"when": "editorTextFocus",
"command": "moveby.calculation",
"args": {
"charNrEx": "50"
}
}
Or use Ctrl+G and type lineNr:charNr
At the risk of not fully understanding the question, if what you want to do is pad any line to the 50th column with spaces, you can try this approach.
Install the extension Find and Transform
Make this keybinding (or it could be a command):
{
"key": "alt+q", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
// this will select the current line, cursor can start anywhere on line
"preCommands": ["cursorEnd", "cursorHomeSelect", "cursorHomeSelect"],
"replace": [
"$${",
"const str = `${TM_CURRENT_LINE}`;",
"let currentLength = str.length;", // current line length
"if (currentLength >= 49) return str;", // do nothing
// pad to whatever column you want
"else return `${str.padEnd(49, ' ')}`;", // pad with spaces
"}$$"
],
"restrictFind": "line", // only on lines with a cursor
"postCommands": ["cursorRight"] // just cancels the selection
}
}
It'll work on multiple lines too if each line has a cursor on it

How to add timeout after first letter of a chord in a custom VS Code keybinding

I have a custom keybind for Esc for my neovim extension in VS Code, that mapping being the chord "j k". When I am in Insert Mode, I can now successfully type "jk" to exit Insert Mode and enter Normal Mode. The problem is that I now cannot type the letter j followed by anything other than the letter k, or my neovim just gives up and tells me "The key combination (J, *) is not a command." (where * is any non-J key) The solution to this keybind problem would be to add a timeout after the first key of the chord (ie j) or to type j normally if a non-j key is typed after it. How does one do this? I would appreciate any help with either solution, although I would prefer the latter, as it solves the problem completely. I have attached screenshots to depict my keybind and the error from typing "j *". Thanks!
Turns out it was just in the documentation:
[
{
"command": "vscode-neovim.compositeEscape1",
"key": "j",
"when": "neovim.mode == insert && editorTextFocus",
"args": "j"
},
{
"command": "vscode-neovim.compositeEscape2",
"key": "k",
"when": "neovim.mode == insert && editorTextFocus",
"args": "k"
}
]

How do you register a Key Chord for a Vscode Extension?

There are various standard key-cords in VsCode such as ctrl + e or ctrl + k sometimes. Their operations are a bit different to key binding, after pressing one of the starting key binds to initiate the sequence, you can press another key like a, b or any other hot keys to conclude the sequence. My question is, how do we register a starting key-bind (ctrl + e-like) or a key sequence (ctrl + e, a) using the VsCode API?
As answered by #rioV8, all we need to do is to separate the key chord with spaces.
},
{
"command": "SOME_COMMAND",
"key": "ctrl+' ctrl+b",
},
{
"command": "SOME_COMMAND",
"key": "ctrl+' a",
},

VSCode: move cursor to next occurrence of given word for each cursor

I have lines like:
"gg":: go to the top:: of the file
"G":: go to the bott::om of the file
"{":: go to the begi::nning of current paragraph
"}":: go to the end ::of current paragraph
"%":: go to the matc::hing pair of (), [], {}
"50%":: go to line a::t the 50% of the file
":NUM":: go to line ::NUM. :28 jumps to line 28
I have cursor in the beginning of each line. So, it looks like:
|"gg":: go to the top:: of the file
|"G":: go to the bott::om of the file
|"{":: go to the begi::nning of current paragraph
|"}":: go to the end ::of current paragraph
|"%":: go to the matc::hing pair of (), [], {}
|"50%":: go to line a::t the 50% of the file
|":NUM":: go to line ::NUM. :28 jumps to line 28
NOTE: Here I am using | as cursor.
Now, I want to move the cursor before first occurrence of :: for each cursor,so that it looks like:
"gg"|:: go to the top:: of the file
"G"|:: go to the bott::om of the file
"{"|:: go to the begi::nning of current paragraph
"}"|:: go to the end ::of current paragraph
"%"|:: go to the matc::hing pair of (), [], {}
"50%"|:: go to line a::t the 50% of the file
":NUM"|:: go to line ::NUM. :28 jumps to line 28
I tried selecting text. then search in selection (ctrl+f>alt+l). then alt+enter. But the issues is, it selects all occurrence of :: and not the first one.
what can I do?
Use the extension Select By
And define a key combo
{
"key": "ctrl+f6", // or any other key combo
"when": "editorTextFocus",
"command": "moveby.regex",
"args": {
"ask": true,
"properties": ["next", "start", "nowrap"]
}
}

How do I collapse sections of code in Visual Studio Code for Windows?

How do I fold or collapse sections of code in Visual Studio Code?
Is this feature supported?
Folding has been rolled out and is now implemented since Visual Studio Code version 0.10.11. There are these keyboard shortcuts available:
Fold folds the innermost uncollapsed region at the cursor:
Ctrl + Shift + [ on Windows and Linux
⌥ + ⌘ + [ on macOS
Unfold unfolds the collapsed region at the cursor:
Ctrl + Shift + ] on Windows and Linux
⌥ + ⌘ + ] on macOS
Fold All folds all regions in the editor:
Ctrl + K, Ctrl + 0 (zero) on Windows and Linux
⌘ + K, ⌘ +0 (zero) on macOS
Unfold All unfolds all regions in the editor:
Ctrl + K, Ctrl + J on Windows and Linux
⌘ + K, ⌘ + J on macOS
References: https://code.visualstudio.com/docs/getstarted/keybindings
As of Visual Studio Code version 1.12.0, April 2017, see Basic Editing > Folding section in the docs.
The default keys are:
Fold All: CTRL+K, CTRL+0 (zero)
Fold Level [n]: CTRL+K, CTRL+[n]*
Unfold All: CTRL+K, CTRL+J
Fold Region: CTRL+K, CTRL+[
Unfold Region: CTRL+K, CTRL+]
*Fold Level: to fold all but the most outer classes, try CTRL+K, CTRL+1
Macs: use ⌘ instead of CTRL (thanks Prajeet)
Also see the ability to fold any arbitrary lines of code as of Insiders v1.70. That is any lines you select can be folded!
See https://stackoverflow.com/a/72954133/836330 for the command and demo.
Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection
This is bound to the above create command: Ctrl+K Ctrl+,
Remove Manual Folding Ranges
editor.removeManualFoldingRanges
This is bound to the above remove command: Ctrl+K Ctrl+.
Code folding by regions has arrived with v1.17. Folding by regions documentation. And v1.19 and 1.23.
[Generally you can add a space, for example // region and // endregion to //region and //endregion and it will also work.]
TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#: #region and #endregion
C/C++: #pragma region and #pragma endregion
F#: //#region and //#endregion
PowerShell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
PHP: #region and #endregion
Bat: ::#region and ::#endregion or REM #region and REM #endregion
Markdown: <!-- #region --> and <!-- #endregion -->
Golang //region and //endregion or //#region and //#endregion
Java //#region and //#endregion
CSS/SCSS/Less: /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less: // #region and // #endregion
Go: // region, // endregion and // #region, // #endregion
shellscript: # region and # endregion
Perl5 #region and #endregion or =pod and =cut
sql --#region and --#endregion
Important: If you don't see your language in the list ::
Each language also has snippets available for the markers. Type '#' and invoke code completion to see them. To have region markers configured for your language, contact the language extension provider.
So type # and then Ctrl+Space to see the region markers for any language.
This feature is available in the standard build now. To make the collapse/expand controls appears, you need to mouse over the area just to the right of the line numbers as shown in this screenshot:
You should add user settings:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}
ctrl + k + 0 : Fold all levels (namespace , class , method , block)
ctrl + k + 1 : namspace
ctrl + k + 2 : class
ctrl + k + 3 : methods
ctrl + k + 4 : blocks
ctrl + k + [ or ] : current cursor block
ctrl + k + j : UnFold
The default shortcut for collapse/extend are:
Ctrl + Shift + [ : "Fold"
Ctrl + Shift + Alt + [ : "Fold all"
Ctrl + Shift + ] : "Unfold"
Ctrl + Shift + Alt + ] : "Unfold all"
Or go to keybindings.json and change as you wish.
For example:
{
"key": "cmd+k cmd+m",
"command": "editor.foldAll",
"when": "editorFocus"
},
{
"key": "cmd+m cmd+k",
"command": "editor.unfoldAll",
"when": "editorFocus"
},
If none of the shortcuts are working (like for me), as a workaround you can also open the command palette (Ctrl + 3 or View -> Command Palette...) and type in fold all:
Collapsing is now supported in release 1.0:
Source Code Folding Shortcuts
There are new folding actions to collapse source code regions based on
their folding level.
There are actions to fold level 1 (Ctrl+K Ctrl+1) to level 5 (Ctrl+K
Ctrl+5). To unfold, use Unfold All (Ctrl+Shift+Alt+]).
The level folding actions do not apply to region containing the
current cursor.
I had a problem finding the ] button on my keyboard (Norwegian layout), and in my case it was the Å button. (Or two buttons left and one down starting from the backspace button.)
With JavaScript:
//#region REGION_NAME
...code here
//#endregion
This is the latest built-in(default) keyboard shortcuts for folding and unfolding the code
vscode Keyboard shortcut
Ctrl+Shift+[ Fold (collapse) region
Ctrl+Shift+] Unfold (uncollapse) region
Ctrl+K Ctrl+[ Fold (collapse) all subregions
Ctrl+K Ctrl+] Unfold (uncollapse) all subregions
Ctrl+K Ctrl+0 Fold (collapse) all regions
Ctrl+K Ctrl+J Unfold (uncollapse) all
Nb: But in some cases, your vs code extension or user will alter the keyboard binding(shortcut). So best option that Checks like this
view->command palette OR cntrl+shift+p
type "fold" .It will suggest the fold and unfold and there shortcut. You can type that shortcut instead of command-palette
eg:
Fold All
UnFold All
Just press ctrl + shift + p, and then type 'fold'.
all keybinds about (un)fold will be shown.
If ctrl k does not work, probably it's because the vim extension overrides the key.
in this case, you should modify settings.json (press ctrl + shift + p, and then type 'settings' ) with
"vim.handleKeys": {
"<C-k>": false,
},
This feature is now supported, since Visual Studio Code 1.17. To fold/collapse your code block, just add the region tags, such as //#region my block name and //#endregion if coding in TypeScript/JavaScript.
Example:
v1.42 is adding some nice refinements to how folds look and function. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting:
Folded Range Highlighting
Folded ranges now are easier to discover thanks to a background color
for all folded ranges.
Fold highlight color Theme: Dark+
The feature is controled by the setting editor.foldingHighlight and
the color can be customized with the color editor.foldBackground.
"workbench.colorCustomizations": {
"editor.foldBackground": "#355000" }
Folding Refinements
Shift + Click on the folding indicator first only folds the inner
ranges. Shift + Click again (when all inner ranges are already folded)
will also fold the parent. Shift + Click again unfolds all.
When using the Fold command (kb(editor.fold))] on an already folded
range, the next unfolded parent range will be folded.
No technical tips here, just simple adjustments of the preferences of VsCode.
I managed to show code folding controls always in VsCode by going to Preferences and searching for 'folding'. Now just select to always show these controls. This works with the Typescript code and HTML of templates in the Angular 8 solution I tested it with.
This was tested with VsCode Insiders 1.37.0 running on a Windows 10 OS.
Here is the most common useful default keymap of VSCode. And you cab easily customize them with your own keymap. CTRL + K and then:
Fold All: CTRL + 0
Unfold All: CTRL + J
Fold Region: CTRL + [
Unfold Region: CTRL + ]
Fold Level 1: CTRL+ 1
Fold Level 2: CTRL+ 2
Fold Level 3: CTRL+ 3
Fold Level 1: CTRL+ 4
As of version 1.3.1 (2016-07-17), Block Collapse is much more convenient.
Any line followed by an indented line will have a '-' character to allow collapse. If the block is collapsed, it will then be replaced by a '+' character that will open the collapsed block.
The (Ctrl + Shift + Alt + ]) will still affect all blocks, closing one level. Each repeated use closed one more level. The (Ctrl + Shift + Alt + [) works in the opposite way.
Hooray, block collapse finally works usefully.
to fold/unfold current block use (ctrl+k)+(ctrl+l)
VSCode extension: Fold Level, one key fold to the level you want.
Note: these shortcuts only work as expected if you edit your keybindings.json
I wasn't happy with the default shortcuts, I wanted them to work as follow:
Fold: Ctrl + Alt + ]
Fold recursively: Ctrl + ⇧ Shift + Alt + ]
Fold all: Ctrl + k then Ctrl + ]
Unfold: Ctrl + Alt + [
Unfold recursively: Ctrl + ⇧ Shift + Alt + [
Unfold all: Ctrl + k then Ctrl + [
To set it up:
Open Preferences: Open Keyboard Shortcuts (JSON) (Ctrl + ⇧ Shift + p)
Add the following snippet to that file
Already have custom keybindings for fold/unfold? Then you'd need to replace them.
{
"key": "ctrl+alt+]",
"command": "editor.fold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+alt+[",
"command": "editor.unfold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+]",
"command": "editor.foldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+[",
"command": "editor.unfoldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+[",
"command": "editor.unfoldAll",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+]",
"command": "editor.foldAll",
"when": "editorTextFocus && foldingEnabled"
},
On a Mac, it's the RHS Command key, ⌘K, not the left for the code folding commands.
Otherwise the left hand Command key will delete the current line, ⌘K.
I wish Visual Studio Code could handle:
#region Function Write-Log
Function Write-Log {
...
}
#endregion Function Write-Log
Right now Visual Studio Code just ignores it and will not collapse it.
Meanwhile Notepad++ and PowerGUI handle this just fine.
Update: I just noticed an update for Visual Studio Code. This is now supported!
Or, if you want to remove the folding buttons, for extra space:
"editor.folding": false
(add to your settings.json file)
The command K + command 0 does not work.
More info here: region extensionhttps://marketplace.visualstudio.com/items?itemName=maptz.regionfolder
After installing the extension and using python, it works like this:
# region ARBITRARY_REGION_NAME
code goes here...
# endregion
Also, selecting the desired area, use Ctrl+M+Ctrl+R.(that is: first hit and hold Ctrl, then hit m, let go m, then hit r, and let go all)