How do I get the Visual Code Studio Yassnippet extension to pick up my snippets? - visual-studio-code

I've started to experiment with Visual Studio Code.
I've built up a large library of snippets managed by Yassnippet. I want to use them with VSC. A third-party developer, Mads Hartmann has published an extension that offers to connect an existing yassnippet library to VSC.
Following the instructions in the readme, I copied the following to my user settings. (This is the sole contents of my user settings)
{
"editor.tabCompletion": false,
"editor.snippetSuggestions": "none",
"yassnippet.path": "/home/me/.emacs.d/list/yasnippet/snippets"
}
This appears to be inadequate. Snippets are not being picked up.
Also, if I look under the 'Contributions' tab in the Yassnippet extensions dialogues, I can see that the setting remains set to it's default.
What do I need to do to get this extension working?

You also need to define a map between the languages and the folders inside Yassnippet.
For example, in order to include the python snippets (located inside the python-mode folder) you should add to your settings:
"yassnippet.mapping":{
"python": "python-mode"
}

Related

Where do code snippets reside in Visual Studio Code?

The only C/C++ extension I have is the official one from Microsoft. I do not have any other snippet/intellisense/autocompletion extension.
See below all the extensions loaded:
In trying to create shortcut keys/prefixes for my snippets, I would like to make sure that it does not clash with any pre-existing shortcuts/prefixes for other snippets. Is there a way to know / look through all currently available code snippets in VSCode and their shortcut keys/prefixes?
I tried to Insert Snippet via the command pallette in the hope that it would reveal all available snippets. Unfortunately, it does not list all snippets. See, for instance, below image, where this command pallette does not show the existence of a for snippet and yet inside the editor, when I type for, there is an option to add such a snippet.
I obtained confirmation officially from vscode-cpptools folks that C/C++ snippets are only available via Ctrl-Space autocomplete and not via Ctrl-Shift-P Command Pallette.
See link to their answer here.

Is there a way to put a VS Code snippets file in a non standard folder and have VS Code use it?

I want to create a standard VS Code snippets file for my team. We use Perforce for version control and I'd like to create a snippet file in our project folder and point VS Code to it. I just want people to be able to sync up and have them just have updated snippets.
I think Project-level snippets will do what you want:
Project level snippets
Snippets can now be scoped to a project and
shared with your team. Simply use the Preferences: Configure User
Snippets command or create *.code-snippets file in the .vscode folder.
Project snippets work just like other snippets, they show up in
IntelliSense and in the Insert Snippet action where they now have
their own category
More info: https://code.visualstudio.com/updates/v1_28#_project-level-snippets

Copy enable/disable extensions settings to another workspace

Idea:
I wanted to clean up my vscode extensions, because I'm working with a lot of different languages/file types and having all these extensions installed and enabled at once is just too much.
Problem:
I disabled some extensions for a specific workspace, and wanted to copy these settings to another workspace, but vscode is not storing the information about enabled/disabled extensions in .vscode/settings.json.
Questions:
Is there any way to copy these settings from one workspace to another?
Is there a better way of dealing with lots of extensions?
Can you recommend tools/extensions for managing extensions per workspace or language(s)
I assume vscode is not loading all extensions at once, but rather when needed. But some extensions display icons on the left or bottom of the window and overcrowd the "Show All Commands" list/search.
VS Code stores this info in its internals instead of the .vscode folder, so you can't copy this info between workspaces. There is an open issue asking exactly what you want.
But, you have an alternative. Use the Profile Switcher extension.
Its description:
This extension allows you to define a number of settings profiles that you can easily switch between. The original idea for this extension came from my desire to have an easy way for me to switch my VS Code to a setup that was better optimised for presenting (changed themes, increase font size, etc).
And this is how it handles extensions:
A profile isn't just the settings you have enabled, but also the extensions that were installed. This allows you to create different profiles for different styles of development (e.g. a React profile and a Vue profile, loading their respective extensions only).
Hope this helps
There is a github issue for this problem: Feature Request: Enable/disable extensions from config file #40239.
I posted there a workaround using multiple vscode instances: link
Here is a copy-paste:
I use some kind of workaround to be able to use the extensions I want.
According to the vscode-cli your can specify the folders for extensions and user-data:
Options Description
--extensions-dir <dir> Set the root path for extensions.
--user-data-dir <dir> Specifies the directory that user data is kept in. Can be used to open multiple distinct instances of Code.
Basically, I create a specific folder for my specific tasks (one of front, one for back, ..) and set basic extensions to my default vscode.
To launch my custom config:
code --extensions-dir "$HOME/.vscode/profiles/my-super-profile/extensions" --user-data-dir "$HOME/.vscode/profiles/my-super-profile/data"
The problem are that:
It's not REALLY a project config file but a global preference file
I had to install manually the extensions. I believe there is a hackish way to do this
It use more size than necessary (multiple vscode data / duplicate extensions)
It doesn't solve in a clean way the team-sharing problem

How to set which extensions are enabled when opening a new folder or workspace?

So I have a great many extensions, and on any given project, I find myself using maybe a third of all of them (If I'm working on a Python project, I don't really need JavaScript related extensions now do I?). The problem is that right now VS Code loads and enables every single extension installed at the start of each new project. I find it a pain to then have to manually go and disable all the extensions I won't be needing for the project.
Is there a way to setup VS Code to only enable certain extensions when opening a new folder / workspace? I couldn't find anything in the docs on that subject and when looking at the extension section of the preferences I couldn't find such a functionality.
As of Feb 2023, it is now possible to create different Profiles in VS Code.
A Profile can include extensions, settings, keyboard shortcuts, UI state, tasks, and user snippets. You can customize VS Code for different development scenarios like data science, documentation writing, or for multiple programming languages like Python or Java. If you have different VS Code setups based on workflow such as "Work" or "Demo", you can also save those as different profiles. You can open multiple workspaces (folders) with different profiles applied simultaneously.
The following image demonstrates a folder opened with a Work profile that is customized for a work setup.
To my knowledge, the best you can do is use the Enable, Enable (Workspace) and Disable, Disable (Workspace) options.
Here is how ended up doing this:
Open a new workspace / folder.
Disable all extensions using Disable.
Enable all extension that you want to have enabled in every new project with Enable.
Enable extensions that you only want to use in this particular workspace / folder with Enable (Workspace)
From then on, when you open a new workspace, only the "default" extensions should be enabled. If ever you want to disable one of these "default" extensions in a particular workspace but still want it to be a "default" extension, make sure to use the Disable (Workspace) option.

How to edit existing VS Code Snippets

Is there a way to remove or edit some of the default code snippets in Visual Studio CODE ?
For example when i type req+TAB i need require not requestAnimationFrame
The extensions snippets can be found inside each snippet directory below:
(if there are snippets in the extension)
Mac/Linux: $HOME/.vscode/extensions/
Windows: %USERPROFILE%\.vscode\extensions/
Select the extension you want to modify and then dive into the javascript.json file in snippets/ directory in there, and change whatever you like.
Just remember that if/when you choose to download and update the extension someday, all your personal modifications will get overwritten/replaced out with the updated version of the file.
(unless of course you squirrel away your changes outside of the extension's directory...)
Edit/Aside:
Looking closely at all the copied editions already present in this directory, it appears that at least some of the extension updates keep the former version around. If this is the case, when you update an extension when a new version is released, you wouldn't need to worry about storing a copy of your modified file somewhere else; returning a file to active duty might just be as easy as a copy-paste from the old into the appropriate, newer, higher numbered directory.
Resources/citations/acknowledgements:
Thanks to here for helping initially pointing me towards the relevant directory.
The suggestion item requestAnimationFrame is coming from the JavaScript language service. It's not coming from the snippets.
However, you can define your own snippets and tell Visual Studio Code to show the snippets first. How to do it:
Go to File -> Preferences -> User Snippets and select JavaScript in order to edit snippets for that language
Add this entry to the opened file javascript.json and save it
"require": {
"prefix": "req",
"body": [
"require"
],
"description": "Add 'require'"
}
Add the following line to your preferred settings.json (user or workspace settings) and save it
"editor.snippetSuggestions": "top"
Now you get your self defined require suggestion in first place as soon as you type req in a .js file.
On my Windows10 machine the log and other default javascript snippets can be found in :
C:\Users\$USER\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\javascript\snippets\javascript.json
On my Windows installation the default/built-in JavaScript snippets are located in
C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\javascript\snippets\javascript.json
I renamed that snippet to "logx" (requires admin privileges to modify the file) and restarted vsCode and now have just my user "log" snippet.
There are some threads about this on the issue tracker -
https://github.com/Microsoft/vscode/issues/10565
https://github.com/Microsoft/vscode/issues/48315
Attention everyone!
This is now possible in the latest vscode. Solution here: https://github.com/microsoft/vscode/issues/10565#issuecomment-721832613
That solution tells you how to disable any snippet (including built-in or extension snippets). While this is technically not editing the snippet, disabling the snippet and then creating your own user snippet accomplishes the same exact goal. Yay!
I found mine at ~/.config/Code/User/snippets
If you want to create a global snippet, create a file named snippet_name.code-snippets
If you want a language specific snippet, create it like php.json
Hiding default VSCode snippets is easy:
you can hide specific snippets from showing in IntelliSense (completion list) by selecting the Hide from IntelliSense button to the right of snippet items in the Insert Snippet command dropdown.
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_can-i-remove-snippets-from-intellisense