Questions tagged [vscode-snippets]

For issues relating to the creation, use, and maintenance of code snippets in Visual Studio Code

Overview

A snippet is a small region of re-usable source code, machine code or text. Ordinarily, these are formally-defined operative units to incorporate into larger programming modules. Snippets are often used to clarify the meaning of an otherwise "cluttered" function, or to minimize the use of repeated code that is common to other functions.

See also

384 questions
1
vote
1 answer

Visual Studio Code snippet dynamic import path

I'm trying to create a TypeScript snippet in Visual Studio Code which includes a bunch of import statements, but I want the path to be set dynamically depending on which directory you use the snippet. For example, I have a snippet like this: import…
CookieEater
  • 2,237
  • 3
  • 29
  • 54
1
vote
2 answers

How to add TIMEZONE to VS Code User Defined Snippets?

I see the time/date variables available to VS Code User Defined Snippets are: CURRENT_YEAR The current year CURRENT_YEAR_SHORT The current year's last two digits CURRENT_MONTH The month as two digits (example '02') CURRENT_MONTH_NAME The full name…
SeaDude
  • 3,725
  • 6
  • 31
  • 68
1
vote
1 answer

Regex working on regex101 but not in Vscode snippet

I am struggling with a regex that works fine on regex101: https://regex101.com/r/Hhj2l9/1, but not in a vscode snippet for html. From the following string: C:\folder0\folder1\folder2\libtest\folder3\folder4\folder5 I get the following results on…
Benoit00
  • 45
  • 4
1
vote
1 answer

VS Code when clause context (for keyboard shortcuts) for LaTeX math mode

In VS Code I can define keyboard shortcuts to only work in certain conditions: https://code.visualstudio.com/api/references/when-clause-contexts So i can add different variables, e.g. this keyboard shortcut would only work when writing latex…
1
vote
1 answer

Renaming namespaces on C# according to the last folder in the file path

I am trying to create a snippet that names a namespace in C# using the name of the last directory in the file path, but it returns everything that comes before it instead. Here is a demo of the regex, The VSCode snippet, with the replacement values…
Perry
  • 21
  • 1
  • 6
1
vote
1 answer

Visual Studio Code HTML snippets no longer working

I've been using HTML Snippets extension for HTML snippets in VSC for a while, however in my most recent project these have stopped working. I've tried researching why this is, and I can't find a straight answer on how to fix it. Being unable to use…
Evelyn
  • 99
  • 2
  • 10
1
vote
0 answers

Can you use textmate scopes/semantic tokens to restrict usage of snippets in VS Code?

Okay. This idea might sound crazy but bear with me. I was writing some snippets for myself earlier and realized how restrictive the "conditional" aspect of the snippets is. The only way to seemingly change anything about the snippet conditionally is…
1
vote
1 answer

VSCode snippet regex for extracting directory and replacing slashes?

I'm struggling to find a VSCode transformation regex that allows me take a string like this: MyProject.Api/Data/RandomPath/Entities/User.ts and produce: MyProject.Api.Data.RandomPath.Entities I can get this working in two passes; one to remove the…
Ben Wong
  • 339
  • 1
  • 8
1
vote
1 answer

VS Code if/else conditions with regular expression in user defined snippet

I am pretty bad at regex and need some help implementing my idea with the already complicated if-else syntax being used for user-defined snippets in VS Code. I want to achieve the following: Whenever I enter a number for variable $1 I want the…
Programmosaurus
  • 111
  • 1
  • 8
1
vote
0 answers

Assign snippet to a particular scope within mixed-language files

I'm trying to create snippets that would work only when I'm in a particular scope within a file, and not be tied to file's language. Ex: I have an HTML file where I mix regular HTML tags and TWIG tags.

Page Title

{{ include('content.twig',…
xms
  • 11
  • 1
1
vote
1 answer

VSC snippets placeholder choices not loading

As the title says, I'm having problems with VSC's snippets placeholder choices as it gets stuck loading and I never get any of the options. At the moment I'm testing with a really simple snippet: "console.log": { "scope":…
Tomás
  • 345
  • 1
  • 3
  • 9
1
vote
1 answer

VSCode: User Snippets showing over path Intellisense

I'm trying to prioritize path intellisense over my user snippets, or just group them with other non-user snippets really. The current behaviour is user snippets -> paths -> other snippets as seen in the screenshot: I need to navigate paths I don't…
1
vote
2 answers

Place plaintext right beside variables (without whitespace) in VS Code User Snippets

I would like to create a User Snipper in VS Code that is a combination of variables and plaint text. This can typically be achieved by combining variables and plain text with a whitespace between then. But I would like to ad a variable next to a…
Gangula
  • 5,193
  • 4
  • 30
  • 59
1
vote
1 answer

VSCode Snippet Help/Chaining transform

I thought I was getting the hang of making my own snippets, but I can't figure out to transform a tab stop so that 'Ignore/Before/Slashes/Oh Hey Text' becomes 'OhHeyText'. so far I have this ${4/.*\\/(.+$)/$1/g} or ${4/\\s//g} But I can't figure…
Ben Wong
  • 339
  • 1
  • 8
1
vote
0 answers

Custom Code Snippet not expanding on tab - this and other snippets otherwise work fine

Found a solution: snippet shortcuts cannot have underscores in them, or they will... well, they just won't expand from intellisense. I am on Visual Studio 2022 Snippets / Intellisense are otherwise working fine, if I start typing "for", I get the…