Chen Xiang
Chen Xiang

Reputation: 73

How to remove the file extension using a snippet variable. List of snippet variables

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

Upvotes: 7

Views: 6865

Answers (4)

Mark
Mark

Reputation: 180875

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

Upvotes: 20

AliN11
AliN11

Reputation: 2553

You can use TM_FILENAME_BASE to get only the filename:

${TM_FILENAME_BASE}

Upvotes: 26

Jos Hickson
Jos Hickson

Reputation: 124

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.

Upvotes: 8

MD Tarik Hasan Khan
MD Tarik Hasan Khan

Reputation: 71

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

${TM_FILENAME/(.js)//}

It will turn FileName.js to FileName

Upvotes: 6

Related Questions