Skip to main content

vscode snippsets vaiables/vscode常用变量

在编程或写作的时候,我们在vscode中会插入快速插入一些系统信息,如当前文件名,当前的日期等等。如果每次都是复制文件名,编辑速度会很慢。通过查找资料,我发现可以利用snippets的变量来实现这些功能。vscode snippsets使用$name${name:default}插入变量的值。

常用的变量

Below are the variables organized into 8 categories as Markdown tables with minimal spacing for easier editing. Each table includes the variable name, English description, and Chinese translation.

1. Text Selection and Content

VariableEnglish DescriptionChinese Description
TM_SELECTED_TEXTThe currently selected text or the empty string当前选中的文本或空字符串
TM_CURRENT_LINEThe contents of the current line当前行的内容
TM_CURRENT_WORDThe contents of the word under cursor or the empty string光标下单词的内容或空字符串
CLIPBOARDThe contents of your clipboard剪贴板的内容

2. Line and Cursor Positioning

VariableEnglish DescriptionChinese Description
TM_LINE_INDEXThe zero-index based line number基于零索引的行号,首行是0行
TM_LINE_NUMBERThe one-index based line number基于一索引的行号,首行是1行
CURSOR_INDEXThe zero-index based cursor number基于零索引的光标编号
CURSOR_NUMBERThe one-index based cursor number基于一索引的光标编号

3. File Information

VariableEnglish DescriptionChinese Description
TM_FILENAMEThe filename of the current document当前文档的文件名
TM_FILENAME_BASEThe filename of the current document without its extensions当前文档不含扩展名的文件名
TM_DIRECTORYThe directory of the current document当前文档的目录
TM_FILEPATHThe full file path of the current document当前文档的完整文件路径
RELATIVE_FILEPATHThe relative (to the opened workspace or folder) file path of the current document当前文档相对于打开的工作区或文件夹的文件路径

4. Workspace Context

VariableEnglish DescriptionChinese Description
WORKSPACE_NAMEThe name of the opened workspace or folder打开的工作区或文件夹的名称
WORKSPACE_FOLDERThe path of the opened workspace or folder打开的工作区或文件夹的路径

5. Date Components

VariableEnglish DescriptionChinese Description
CURRENT_YEARThe current year当前年份
CURRENT_YEAR_SHORTThe current year's last two digits当前年份的后两位
CURRENT_MONTHThe month as two digits (example '02')月份(两位数字,例如 '02')
CURRENT_MONTH_NAMEThe full name of the month (example 'July')月份全名(例如 '七月')
CURRENT_MONTH_NAME_SHORTThe short name of the month (example 'Jul')月份简称(例如 '七')
CURRENT_DATEThe day of the month as two digits (example '08')每月日期(两位数字,例如 '08')
CURRENT_DAY_NAMEThe name of day (example 'Monday')星期名称(例如 '星期一')
CURRENT_DAY_NAME_SHORTThe short name of the day (example 'Mon')星期简称(例如 '周一')

6. Time Components

VariableEnglish DescriptionChinese Description
CURRENT_HOURThe current hour in 24-hour clock format当前小时(24小时制)
CURRENT_MINUTEThe current minute as two digits当前分钟(两位数字)
CURRENT_SECONDThe current second as two digits当前秒数(两位数字)
CURRENT_SECONDS_UNIXThe number of seconds since the Unix epoch自Unix纪元以来的秒数
CURRENT_TIMEZONE_OFFSETThe current UTC time zone offset as +HH:MM or -HH:MM (example -07:00)当前UTC时区偏移(例如 -07:00)

7. Random Values

VariableEnglish DescriptionChinese Description
RANDOM6 random Base-10 digits6个随机十进制数字
RANDOM_HEX6 random Base-16 digits6个随机十六进制数字
UUIDA Version 4 UUID版本4的UUID

8. Code Comments

VariableEnglish DescriptionChinese Description
BLOCK_COMMENT_STARTExample output: in PHP /* or in HTML <!--块注释开始(例如 PHP中 /* 或 HTML中 <!--
BLOCK_COMMENT_ENDExample output: in PHP /* or in HTML >块注释结束(例如 PHP中 *\ 或 HTML中 >

案例

我们在写blog时,想要插入update date,就可以在markdown的snippets中加入这么一段。

"last_update: $CURRENT_YEAR-$CURRENT_MONTH-CURRENT_DATE",

测试一下效果

---
tags:
- tag1
draft: true
last_update:
date: 2025-03-21
---

References