如何使用代码段变量删除文件扩展名。片段变量列表

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

我用vscode.

我想使用服装片段,但 {TM_FILENAME} 有一个扩展名。

如何从 {TM_FILENAME} 中删除扩展程序?

像这样:`

在文件 MyModule.js 中:

变换:${TM_FILENAME/(\w+)\.js//g}

输出:

MyModule

您目前无法执行此操作,但 vscode GitHub 页面上有您想要的功能请求:https://github.com/Microsoft/vscode/issues/6920.

[编辑]

我上面的回答现在已经过时了 - 您可以使用其他贡献者提到的 ${TM_FILENAME_BASE} 变量。

如果您知道文件扩展名,您可以尝试这样的操作,

${TM_FILENAME/(.js)//}

它将 FileName.js 转换为 FileName

实际上,自从提出问题以来,已经添加了一些新的内置变量, 包括 TM_FILENAME_BASE。参见 snippet variables documentation。所以没有必要为了得到没有扩展名的文件名而进行转换。

这是当前的代码段变量列表:

 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 将添加两个新变量:

CURSOR_INDEX                     0-based
CURSOR_NUMBER                    1-based

以上两个使用多个游标,因此每个游标位置(与选择相同)将插入一个递增整数。

有关此示例,请参阅


看来v1.40会增加:

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

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


v1.53会添加从根文件夹到当前文件的相对路径:

RELATIVE_FILEPATH
UUID

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

您可以使用 TM_FILENAME_BASE 只获取文件名:

${TM_FILENAME_BASE}