7

I use vscode.

I want to use costum snippets, but {TM_FILENAME} has an extension name.

How can I delete the extension from {TM_FILENAME}?

like this:`

In file MyModule.js:

Transform: ${TM_FILENAME/(\w+)\.js/\1/g}

Output:

MyModule

Mark
  • 143,421
  • 24
  • 428
  • 436
Chen Xiang
  • 73
  • 1
  • 4
  • To improve the quality of your Question, please include the text in your image as text in the Question. You can format that text in the Question by selecting it and clicking on the `{}` button above the text entry area, or by indenting each line by four spaces. This is more convenient to those reading the Question than having to follow a link. Also, links may break over time, and it is considered ideal if you write a Question to last. – toonice Apr 21 '17 at 03:37
  • sorry,i had edited – Chen Xiang Apr 21 '17 at 10:51
  • 1
    Can you change the accepted answer to @Mark 's please? –  Dec 25 '19 at 15:58

4 Answers4

26

You can use TM_FILENAME_BASE to get only the filename:

${TM_FILENAME_BASE}
AliN11
  • 2,387
  • 1
  • 25
  • 40
20

Actually, since the question was posed a few new built-in variables have been added, including TM_FILENAME_BASE. See snippet variables documentation. So there is no need to do a transform just to get the filename without the extension.

Here is the current list of snippet variables:

 TM_SELECTED_TEXT           The currently selected text or the empty string
 TM_CURRENT_LINE            The contents of the current line
 TM_CURRENT_WORD            The contents of the word under cursor or the empty string
 TM_LINE_INDEX              The zero-index based line number
 TM_LINE_NUMBER             The one-index based line number
 TM_FILENAME                The filename of the current document
 TM_FILENAME_BASE           The filename of the current document without its extensions
 TM_DIRECTORY               The directory of the current document
 TM_FILEPATH                The full file path of the current document
 CLIPBOARD                  The contents of your clipboard
 WORKSPACE_NAME             The name of the opened workspace or folder

 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 of the month (example 'July')
 CURRENT_MONTH_NAME_SHORT   The short name of the month (example 'Jul')
 CURRENT_DATE               The day of the month
 CURRENT_DAY_NAME           The name of day (example 'Monday')
 CURRENT_DAY_NAME_SHORT     The short name of the day (example 'Mon')
 CURRENT_HOUR               The current hour in 24-hour clock format
 CURRENT_MINUTE             The current minute
 CURRENT_SECOND             The current second
 CURRENT_SECONDS_UNIX       The number of seconds since the Unix epoch

For inserting line or block comments, honoring the current language:

 BLOCK_COMMENT_START        Example output: in PHP /* or in HTML <!--
 BLOCK_COMMENT_END          Example output: in PHP */ or in HTML -->
 LINE_COMMENT               Example output: in PHP // or in HTML <!-- -->

vscode v1.66 will add two new variables:

CURSOR_INDEX                     0-based
CURSOR_NUMBER                    1-based

The above two work with multiple cursors so that each cursor position (same as a selection) will have an incremented integer inserted.

For an example of this, see https://stackoverflow.com/a/69946559/836330


It looks like v1.40 will add:

WORKSPACE_FOLDER           Path of workspace directory
RANDOM                     Insert 6 random digits
RANDOM_HEX                 Insert 6 random hex digits

See https://github.com/microsoft/vscode/pull/82529 and https://github.com/microsoft/vscode/pull/79764


v1.53 will add the relative path from the root folder to the current file:

RELATIVE_FILEPATH
UUID

See https://github.com/microsoft/vscode/pull/114208 and https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_53.md#new-snippet-variables

Mark
  • 143,421
  • 24
  • 428
  • 436
8

You can't do this at the moment but there's a feature request on the vscode GitHub page for what you want: https://github.com/Microsoft/vscode/issues/6920.

[EDIT]

My answer above is now out of date - you can use the ${TM_FILENAME_BASE} variable as mentioned by other contributors.

Jos Hickson
  • 124
  • 5
6

You can try something like this if you know the file extension,

${TM_FILENAME/(.js)//}

It will turn FileName.js to FileName