如何从代码段变量$ {TM_FILENAME}中删除扩展名?

时间:2017-04-21 03:02:55

标签: visual-studio-code vscode-extensions

我使用vscode。

我想使用costum代码段,但{TM_FILENAME}有一个扩展名。

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

像这样:“

在文件MyModule.js中:

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

输出:

MyModule

4 个答案:

答案 0 :(得分:2)

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

${TM_FILENAME/(.js)//}

它将FileName.js转换为FileName

答案 1 :(得分:2)

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

${TM_FILENAME_BASE}

答案 2 :(得分:1)

实际上,由于提出了问题,所以添加了一些新的内置变量,  包括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

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 <!-- -->

The snippet below inserts /* Hello World */ in JavaScript files and <!-- Hello World --> in HTML files:

{
  "hello": {
      "scope": "javascript,html",
      "prefix": "hello",
      "body": "$BLOCK_COMMENT_START Hello World $BLOCK_COMMENT_END"
  }
}

答案 3 :(得分:0)

目前您无法执行此操作,但在vscode GitHub页面上有一个功能请求,可以满足您的需求:https://github.com/Microsoft/vscode/issues/6920