C-style comments in VS Code - visual-studio-code

Is it possible to configure VS Code to use C-style comments (/**/) instead of C++ style ones (//) for C code only? My Gooogle-fu might be weak, but I haven't found any useful solution to it so far.

Select the line or lines then press Ctrl + Shift + A.

You can actually redefine it entirely to your liking, but it will require some fiddling with the package.json of the C/C++ extension.
This file will probably be located (at least on my Ubuntu it is) in ~/.vscode/extensions/ms-vscode.cpptools-YOUR_VERSION_NUMBER. So open it
code ~/.vscode/extensions/ms-vscode.cpptools-YOUR_VERSION_NUMBER/package.json
in terminal.
In this file you need to locate an object named "contributes".
Within this object you'll need to create an array called "languages" and add a language object:
"languages": [
{
"id": "c",
"extensions": [
".c", ".h"
],
"configuration": "./my-c-configuration.json"
}
]
now the path in the configuration property needs to point to another .json file in which you will add the new definition for the comment command:
(in the "my-c-configuration.json" file, created in the same directory)
{
"comments": {
"lineComment": [ "/*", "*/" ],
"blockComment": [ "/*", "*/" ]
}
}
Save both files, reopen the VSCode, and that's all.

Was this changed ?
To me, it works with Ctrl+Alt+A.

It's July, 2020 and latest vs code stable version is Version 1.47.
Toggle block comment shortcut is : Shift+Alt+A.
Official Source : VS-Code Keyboard Shortcuts

Related

How do I find out the names of various Visual Studio Code commands when they aren't displayed in a camelCase convention?

I'm using the VSCodeVim extension and am trying to configure some shortcuts for normal mode without the need of turning the extension off then on again via its togglevim command. I'm trying to add the "View: Close Editor", "View: Open Previous Editor", and "View: Open Next Editor" commands as they're named in the keyboard shortcuts menu to the "vim.normalModeKeyBindings" setting but don't know what to put for the "commands" value.
The commands towards the bottom of the shotrcuts menu are listed in their name form while most of the commands above them have a "cleaned up" name that displays in the F1 command palette and doesn't seem to correspond to the underlying command name.
I've tried the following which results in a command not found notification:
"vim.normalModeKeyBindings": [
{
"before": [ "<C-w>" ],
"commands": [
"view.closeEditor" // I've tried all kinds of variations of capital/lowercase letters and periods/hyphens.
]
},
{
"before": [ "<C-pageup>" ],
"commands": [
"view.openPreviousEditor" // See above.
]
},
{
"before": [ "<C-pagedown>" ],
"commands": [
"view.openNextEditor" // See above.
]
}
]
How do I find out what the underlying command name is for these "View" commands (or any such named command) so I can reference them in the settings.json?
Even though there is only a title for some commands, like View: Close Editor you can still right-click on that command title and choose
Copy Command ID
from the context menu. That'll give you the version you need, like
workbench.action.closeActiveEditor
I don't use vim so I hope you can adapt that to use as you need.

Is there a way to tell VS Code that the firstLine match in my language extension should overrule any file extension?

I am currently developing a language extension for VS Code. The language I am developing it for usually comes with either its own file extension (let's call it .myext) or simply .txt (it's just a scripting language).
I have run into the issue that I am not sure how to tell VS Code to use my extension when the first line matches a specific string when opening .txt files.
This is what I am trying (excerpt from my package.json):
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
This does not work when I open a .txt file that starts with MyLang. It does however work if I open a file that has an unknown (e.g., file.foobar1234) or no extension at all.
When I now change the configuration to include .txt files, it will activate my extension for any .txt file I open:
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext",
".txt"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
However, I do not want this, I want to keep the default plain text setting when opening normal .txt files.
In short, what I want to achieve:
have my extension activate on my custom file extension .myext (this already works without any issues)
have my extension activate when opening .txt files that start with the string specified via firstLine
do not have my extension activate when opening .txt files that do not start with the string specified via firstLine
Is there a way to do this?
After some extensive search: this is currently not possible.
There was a recent change (mid-late 2017) that allows language extensions that match the file extension and the first line of a file to get precedence over an extension that just matches one of the two.
This does, however, not work with the built-in language modes of VS Code. Matching a file extension will make it so VS Code always chooses your language extension for such files if there if there is no other extension available for that language, regardless of any first line rule that might have been set in addition.
See here and here for additional information.
If you have the latest version of VS Code, I say this because it's what I have might be probably is supported in earlier versions. On the bottom toolbar you should see the language that is auto-detected for the file you have open. For example, if you have a .js file it will auto-config for JavaScript. Whatever language defaults for you file, just click on it and it will bring you into 'Select Language Mode'.
From there you'll have options. You can configure/customize what happens and/or Search for an extension to fulfill the Language mode. So, I think you'd want to do the latter especially if you're creating your own language file.

Assign file with no extension to a language on VS Code as default

In VS Code I usually open files that have no extension just filename . I know I can change the language syntax with Change Language Mode --> Language that I want but I don't want to do this manually every time I open such a file. Can I make a default to this language every time I open a file with no extension?
I know I can do this:
"files.associations": {
"*.myphp": "php"
}
But what if there is no extension? Also I want to be able to do this without affecting the other file types (that have extension).
VS Code's globbing doesn't currently seem to have a way to detect files with no extension. Every time someone opens an issue, they point it back to this issue here. They detail their globbing support here.
That said, I do have a hacky solution to this. Put this in your "WORKSPACE SETTINGS" (not your general settings unless you really want this to be global).
{
"files.associations": {
"[!.]": "php",
"[!.][!.]": "php",
"[!.][!.][!.]": "php",
"[!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php"
},
}
This works by adding a rule for every file name length and ensuring that the file cannot have a period in it (up to 20 characters in my example). This is a horrible hacky solution, but if you hold your nose and set it once, you can forget it and things just work. I do this in my dotfiles repo to associate extension-less files with "shellscript".
You an also do this just for a specific directory by using the double star glob: "**/just_this_dir_name/[!.]": "php".
Thanks to the closing of issue Expose 'change language' as command and the development of plugin Modelines, we can now use vim style modeline inside files without suffix and vscode can detect the file type.
Example:
filename: post-commit
file content:
#!/bin/sh
# vim: set ft=sh
echo "hello world linked"
After installing the Modelines extension, the file is highlighted properly:
If you choose mattmc3's solution you can use pattern-list entry to write it in one line:
{
"file.associations": {
// all files up to 30 characters long without extension will be associated with `php`
"{[!.],[!.][!.],[!.][!.][!.],[!.][!.][!.][!.],[!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}": "php"
}
}
In example above, all files up to 30 characters long without extension will be associated with php.
You can shorten this entry with multiple pattern lists combined in one line, such as {...}{...}{...}. The example below works like the previous one, but it is much shorter:
{
"file.associations": {
// all files up to 30 characters long without extension will be associated with `php`
"{[!.],[!.][!.],[!.][!.][!.],[!.][!.][!.][!.],[!.][!.][!.][!.][!.]}{[],[!.],[!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}{[],[!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}": "php"
}
}
Also, if you want to set this up for files in a directory where you only have root access, or you don't want to create a .vscode folder in it, you can create a rule in your user settings.json with the path in this rule, e.g.:
{
"file.associations": {
"/etc/**/{[!.],[!.][!.],...}": "shellscript"
}
}
Another solution for files in specific directory is to use multiple rules:
{
"file.associations": {
"/etc/**/*": "shellscript", // this rule will work on all files
"/etc/**/*.list": "debsources", // and this one to exclude .list files
"*.list": "debsources" // due to this rule won't work for .list
// files in /etc/** directories
}
}
This is because a more specific rule takes precedence over a more general rule.

Create a Sublime Text 3 .sublime-completions autocomplete file on mac

The Sublime Text documentation is clear on the syntax for HTML, but not clear on where to place the file for mac using version 3. I want to generate my own auto-completes for Plain Text. Ideally allowing me to down/up arrow through a list of likely auto-completes.
{
"scope": "text.html - source - meta.tag, punctuation.definition.tag.begin",
"completions":
[
{ "trigger": "a", "contents": "$0" },
{ "trigger": "abbr\t<abbr>", "contents": "<abbr>$0</abbr>" },
{ "trigger": "acronym", "contents": "<acronym>$0</acronym>" }
]
}
All package resource files need to be stored in a Package for Sublime to be able to find and load it. For your own customizations, the appropriate place to place the file is in your User package, which you can find via Preferences > Browse Packages from the menu.On MacOS, that would be Sublime Text > Preferences > Browse Packages.
In order to have completions for plain text, you need to change the scope from the HTML specific scope to one for plain text.
In order to determine the scope that you want to apply (which also counts for things like Key Bindings, Snippets, Build Systems, and so on) you can select Tools > Developer > Show Scope Name from the menu (see the menu for the key binding assigned to this) to see what the full scope is for the current cursor location.
As evidenced from the scope you mentioned in your question, scopes can be quite complex to allow you to dial in as much specificity as you want.
For the case of simple plain text, as evidenced by the command I mentioned above, the following is the example completions set to work in plain text:
{
"scope": "text.plain",
"completions":
[
{ "trigger": "a", "contents": "$0" },
{ "trigger": "abbr\t<abbr>", "contents": "<abbr>$0</abbr>" },
{ "trigger": "acronym", "contents": "<acronym>$0</acronym>" }
]
}
Note that along with the location of the file, the extension is also important, otherwise Sublime won't know what it's supposed to contain.

Write custom emmet snippets with higher priority than default

How can I create custom emmet snippet with the highest possible priority to display in vscode?
When I updated html.json to add my own snippet for comment (with autor), I still see default emmet "c" snippet as first in list:
"Comment": {
"prefix": "c",
"body":[
"<!-- atiris: $1 -->$2"
],
"description": "Insert comment into html code"
}
That means I always have to choose the second option first.
Is there any property / settings to prioritize my own snippets?
You can use BetterSnippets vscode extension. It provides way more customization then builtin snippets. For your case you can use smth like this:
"betterSnippets.customSnippets": [
{
"name": "c",
"body": "<!-- atiris: $1 -->$2",
"when": {
"languages": [
"html"
],
},
"sortText": "!1"
},
]
SortText property is what you're looking for. I use !1 to make snippets order more customizable e.g. if I would assing sortText: "!2" to other snippet it will appear under the snippet with !1
You are editing the intellisense snippets with the above code rather than the Emmet snippets.
Regarding your question, the following link may help you prioritize which snippets are shown first: https://github.com/Microsoft/vscode/issues/32346
Regarding adding snippets to Emmet, the following should help:
In the settings.json file you can add the line
"emmet.extensionsPath": "/Users/username/Documents/code snippets"
(changing the path to a folder of your choosing)
Then add in that folder a file named snippets.json
You can use the format outlined in the link below to add snippets.
https://code.visualstudio.com/docs/editor/emmet