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
Variable | English Description | Chinese Description |
---|---|---|
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 | 光标下单词的内容或空字符串 |
CLIPBOARD | The contents of your clipboard | 剪贴板的内容 |
2. Line and Cursor Positioning
Variable | English Description | Chinese Description |
---|---|---|
TM_LINE_INDEX | The zero-index based line number | 基于零索引的行号,首行是0行 |
TM_LINE_NUMBER | The one-index based line number | 基于一索引的行号,首行是1行 |
CURSOR_INDEX | The zero-index based cursor number | 基于零索引的光标编号 |
CURSOR_NUMBER | The one-index based cursor number | 基于一索引的光标编号 |
3. File Information
Variable | English Description | Chinese Description |
---|---|---|
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 | 当前文档的完整文件路径 |
RELATIVE_FILEPATH | The relative (to the opened workspace or folder) file path of the current document | 当前文档相对于打开的工作区或文件夹的文件路径 |
4. Workspace Context
Variable | English Description | Chinese Description |
---|---|---|
WORKSPACE_NAME | The name of the opened workspace or folder | 打开的工作区或文件夹的名称 |
WORKSPACE_FOLDER | The path of the opened workspace or folder | 打开的工作区或文件夹的路径 |
5. Date Components
Variable | English Description | Chinese Description |
---|---|---|
CURRENT_YEAR | The current year | 当前年份 |
CURRENT_YEAR_SHORT | The current year's last two digits | 当前年份的后两位 |
CURRENT_MONTH | The month as two digits (example '02') | 月份(两位数字,例如 '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 as two digits (example '08') | 每月日期(两位数字,例如 '08') |
CURRENT_DAY_NAME | The name of day (example 'Monday') | 星期名称(例如 '星期一') |
CURRENT_DAY_NAME_SHORT | The short name of the day (example 'Mon') | 星期简称(例如 '周一') |
6. Time Components
Variable | English Description | Chinese Description |
---|---|---|
CURRENT_HOUR | The current hour in 24-hour clock format | 当前小时(24小时制) |
CURRENT_MINUTE | The current minute as two digits | 当前分钟(两位数字) |
CURRENT_SECOND | The current second as two digits | 当前秒数(两位数字) |
CURRENT_SECONDS_UNIX | The number of seconds since the Unix epoch | 自Unix纪元以来的秒数 |
CURRENT_TIMEZONE_OFFSET | The current UTC time zone offset as +HH:MM or -HH:MM (example -07:00) | 当前UTC时区偏移(例如 -07:00) |
7. Random Values
Variable | English Description | Chinese Description |
---|---|---|
RANDOM | 6 random Base-10 digits | 6个随机十进制数字 |
RANDOM_HEX | 6 random Base-16 digits | 6个随机十六进制数字 |
UUID | A Version 4 UUID | 版本4的UUID |
8. Code Comments
Variable | English Description | Chinese Description |
---|---|---|
BLOCK_COMMENT_START | Example output: in PHP /* or in HTML <!-- | 块注释开始(例如 PHP中 /* 或 HTML中 <!-- |
BLOCK_COMMENT_END | Example 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
---