VSCode 提供 Snippet 功能,让常用代码模板通过简短前缀展开为完整代码块。
配置位置
- 全局:
Code → Preferences → Configure User Snippets - 按语言:
{language}.json(如cpp.json、python.json)
示例:ROS2 C++ Publisher Snippet
{
"ROS2 C++ Publisher": {
"prefix": "ros2pub",
"body": [
"#include \"rclcpp/rclcpp.hpp\"",
"#include \"std_msgs/msg/${1:string}.hpp\"",
"",
"class ${2:NodeName} : public rclcpp::Node {",
"public:",
" ${2:NodeName}() : Node(\"${3:node_name}\") {",
" publisher_ = this->create_publisher<std_msgs::msg::${1:string}>(\"${4:topic}\", 10);",
" timer_ = this->create_wall_timer(",
" std::chrono::milliseconds(${5:500}),",
" [this]() { auto msg = std_msgs::msg::${1:string}(); publisher_->publish(msg); });",
" }",
"private:",
" rclcpp::Publisher<std_msgs::msg::${1:string}>::SharedPtr publisher_;",
" rclcpp::TimerBase::SharedPtr timer_;",
"};"
],
"description": "Create a ROS2 C++ Publisher node"
}
}
2026/7/27...大约 1 分钟
