VS Code Markdown Bold Shortcut Not Working - visual-studio-code

Usually, cmd + b or ctrl + b (in Windows) will make the selected text bold in a Markdown file but that's not working. How can I fix it?

By default, cmd + b is the keyboard shortcut for toggling sidebar visibility. To change it, add the following block of code in the keybindings.json file:
{
"key": "cmd+b",
"scope": "markdown",
"command":"editor.action.insertSnippet",
"when":"editorTextFocus && editorLangId == 'markdown'",
"args":{
"snippet":"**$TM_SELECTED_TEXT**$0"
}
}

Related

Duplicate selected text to a new line VS code?

I already found that the "command": "editor.action.duplicateSelection"
will duplicate the selection right next to it.
I want to duplicate the selected text to a new line. The selection may not be the entire line.
If you are talking about a selection that is less than the entire line, there is no built-in way to duplicate selected text to the next line. It can be done with a macro extension which enables you to run multiple commands at once.
Using the macro extension multi-command try this keybinding (in your keybindings.json):
{
"key": "alt+i", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
"editor.action.clipboardPasteAction",
{ // to add text after the selection
"command": "type", // you could also put this before the paste command
"args": { "text": " myText here after paste " }
}
]
}
}
That will copy the selected text, insert a blank line after it and paste that text there. Demo:
Demo with adding static text to the duplicated text:
Click File > Preferences > Keyboard Shortcuts:
Look for the Copy Line Down keyboard shortcut.

How to set the insertion of automatic space after a colon in Visual Studio Code?

I want to set that if I write a colon, I am automatically added a space after it. I used to use IntelliJ IDEA, which did this automatically when working with JSON. Can this be set in VSC as well?
The extension HyperSnips is one way to do it.
In your json.hsnips file (so it is limited to json files):
snippet `:` "expand to : " A
``rv = ': '``
endsnippet
Now whenever you type a : it will be expanded to : in a json file.
See https://stackoverflow.com/a/62562886/836330 for more info.
Just using : as a key to a prefix in a keybinding won't work as you must have a modifier key - like alt, ctrl, etc. See https://code.visualstudio.com/docs/getstarted/keybindings#_accepted-keys.
You could do something like:
{
"key": "alt+;", // I chose ; because it is on the same key for me as :
"command": "type",
"args": { "text": ": " },
"when": "editorTextFocus && editorLangId == json"
},
and that would work.

How can vscode print (or paste) all open file paths to a new file in the editor?

In vscode Ctrl+Tab displays open files, but how can vscode print (or paste) the same 'all open file paths' to a new file in the editor?
If you have this setting set to a high enough number to show all your open files:
Editor > Open Editors: Visible
then you can select all the files from the Open Editors viewlet (with Ctrl+A for instance) in the Explorer, right-click and choose Copy Path or Copy Relative Path and then paste it yourself into a new file. Demo:
For how to automatically send selected (modify the variable for clipboard text) to a new file see my answer at https://stackoverflow.com/a/57612004/836330. I suppose the whole thing might be made into a macro.
Here is the macro. Using a macro extension like multi-command put this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.getOpenFilePaths",
"sequence": [
"workbench.files.action.focusOpenEditorsView",
"list.selectAll",
"copyFilePath", // full paths
// "copyRelativeFilePath", // relative paths
"workbench.action.files.newUntitledFile",
"editor.action.clipboardPasteAction",
// prompt for save immediately?
// "workbench.action.files.saveAs",
]
}
]
and some keybinding to trigger that macro:
{
"key": "alt+o", // whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.getOpenFilePaths" },
},
The Open Editors viewlet can be collapsed if you wish when you trigger the macro and it still works. Demo:
[Adding another answer because the first was long and very different.]
Here is a simpler way to print a list of the currently open files to a newly created file. It does not require that the Open Editors view be visible. It uses newer extension api to access the tabs currently open.
It uses an extension (I wrote), Find and Transform. In this extension, you can use the vscode extension api. Make this keybinding (in your keybindings.json) (it could also be made into a command accessible from the Command Palette):
{
"key": "alt+q", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"replace": [
"$${",
"let str = '';",
"const groups = vscode.window.tabGroups.all;", // if multiple editor groups
"groups.map((group, index) => {",
"str += 'Group ' + (index+1) + '\\n';",
"group.tabs.map(tab => {",
"if (tab.input instanceof vscode.TabInputText) str += '\\t' + tab.input.uri.fsPath + '\\n';",
// the above prints the full path, use below if you only want the tab file label only
// "str += tab.label + '\\n';",
"});",
"str += '\\n';",
"});",
"vscode.env.clipboard.writeText( str );",
"return '';",
"}$$"
],
// open a new file and paste to it
"postCommands": ["workbench.action.files.newUntitledFile", "editor.action.clipboardPasteAction"]
},
}
To open a save file dialog use this "postCommands" entry.
"postCommands": [
"workbench.action.files.newUntitledFile",
"editor.action.clipboardPasteAction",
"workbench.action.files.save"
]

How to format Blazor code correctly in VS code? [duplicate]

Anyone have a good solution for formatting Razor files inside of VSCode? I've tried making it work with prettify-vscode and beautify. But in both cases it can't tell that cshtml files. I don't want to change my razor to html as I'll lose a lot of the razor-ness.
You can introduce them as HTML files (File -> Preferences -> Settings) without any 3rd party extensions:
{
"editor.formatOnSave": true,
"emmet.includeLanguages": {
"aspnetcorerazor": "html"
},
"files.associations": {
"*.cshtml": "html"
}
}
Update: v1.17.0 of C# for Visual Studio Code add-on added preview Razor (cshtml) language service with support for C# completions and diagnostics.
There is an extension that we can switch between Language Modes by shortcuts quickly: changeLanguageMode.change
I use these shortcuts for js, html, and cshtml:
{
"key":"ctrl+k j",
"command":"changeLanguageMode.change",
"args": {
"languageId":"javascript"
}
},
{
"key":"ctrl+k h",
"command":"changeLanguageMode.change",
"args": {
"languageId":"html"
}
},
{
"key":"ctrl+k k",
"command":"changeLanguageMode.change",
"args": {
"languageId":"aspnetcorerazor"
}
}
To open keybindings.json and add these shortcuts:
open up the control palette with CTRL +SHIFT + P and select Preferences: Open Keyboard Shortcuts File.
Then use Ctrl + K, Ctrl + F to Format Selection only.
Firstly: open file setting.json, which is in .vscode folder, then add next block:
{
"editor.formatOnSave": true,
"emmet.includeLanguages": {
"razor": "html"
},
"files.associations": {
"*.cshtml": "html"
}
Sometimes is necessary restart vscode
Finally: in the file to format Ctrl + k , Ctrl + f

Preserve spacing on indent or outdent with tab in VSCode

In VSCode, when I have:
/*
* Comment
*/
If I select it and hit tab, I get:
/*
* Comment
*/
If instead I had hit shift-tab, I get:
/*
* Comment
*/
Same happens with Ctrl-] and Ctrl-[ (if those are supposed to make a difference)
I hoped turning off autoIndent would stop this, but no dice. I also turned off C++ formatting in the JSON config:
{
"editor.autoIndent": false,
"editor.detectIndentation": false,
"C_Cpp.formatting": "Disabled"
}
There's an extension which shifts text by one character at a time which is a sort of proof-of-concept you could override your tab key with something like that. But it doesn't seem you should need an extension to disable this formatting.
Is editor.autoIndent: false supposed to do what I want, and just broken?
UPDATE: I have also raised this as an issue on the VSCode GitHub
If you set the Tab size to 1, it will do the same job as the extension you referenced.
You can set the Tab or Space size by clicking on the bottom-right corner:
Click on Spaces:4. Then, select Indent Using Spaces or Indent Using Tabs and choose the size 1.
UPDATE:
I found an approach that fully satisfies your requirement (though it's through an extension). After choosing a Tab/Space size of 1, install and load the multi-command extension to perform the 1-space indentation 'four' times. Then, go to your settings.json (File > Preferences > Settings) and add these two commands:
{
"macros": {
"tab4times": [
"tab",
"tab",
"tab",
"tab"
],
"shifttab4times": [
"outdent",
"outdent",
"outdent",
"outdent"
]
}
}
Then, in the keybindings.json file (CTRL+P and then type keybindings.json), modify the CTRL+] and CTRL+[ keys to execute the newly created commands:
[
{
"key": "ctrl+]",
"command": "macros.tab4times",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+[",
"command": "macros.shifttab4times",
"when": "editorTextFocus && !editorReadonly"
}
]
After saving these configurations, go to your text. Now press the CTRL+] and CTRL+[ to see you desired behavior of indentation and outdentation, respectiovely.
Hope it helps.