Sublime默认snippet修改和代码片段进阶技巧

自定义的触发快捷键

自定义的一些快捷键
全局

  • sign 签名 文件名 文件路径

LaTeX

  • am 行内
  • dm 行间公式
  • text
  • beg
  • / 分数
  • dh 多行公式
  • lim 极限

C++

  • for
  • #in
  • cout cout;
  • fre
  • sign

修改默认snippet的方法

  1. 打开命令面板输入:prv,选择Open Resource
    在这里插入图片描述
  2. 选择C++
  3. 选择Snippets
  4. 即可修改关于C++的Sublime默认模版
    在这里插入图片描述
  • 如我修改了默认的if模版
    在这里插入图片描述
  • 修改的for模版
<snippet>
    <description>For Loop</description>
    <content><![CDATA[
for (int ${2:i} = 0; $2 < ${1:n}; ${2:i}++) {
    ${0:}
}]]></content>
    <tabTrigger>for</tabTrigger>
    <scope>source.c, source.objc, source.c++, source.objc++</scope>
</snippet>

snippet代码片段示例

配置文件路径:
/Users/macos/Library/Application Support/Sublime Text 3/Packages/User/MySnippet/c++snippet/mb.sublime-snippet
${0:}表示最后一个tab跳转的位置
${1:占位符} 可以设置占位符

<!-- 
    Author: Wilson79
    Filename: mb.sublime-snippet
    Filepath: /Users/macos/Library/Application Support/Sublime Text 3/Packages/User/MySnippet/c++snippet/mb.sublime-snippet  
 -->

<snippet>
	<description>C++模版</description>
	<content><![CDATA[
#include <iostream>

using namespace std;

int main() {
	#ifdef ONLINE_JUDGE
	#else
    freopen("in.txt","r",stdin);
	#endif

	${0:}

	return 0;
}

]]>
	
</content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>mb</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

Sublime预定义的环境变量

环境变量名 描述
$TM_FILENAME 文件名
$TM_FILEPATH 文件全路径
$TM_FULLNAME 用户名
$TM_LINE_INDEX 插入多少列, 默认为0
$TM_LINE_NUMBER 一个snippet插入多少行
$TM_SOFT_TABS 如果设置translate_tabs_to_spaces : true 则为Yes
$TM_TAB_SIZE 每个Tab包含几个空格

生效范围scope的官方说明

关于<scope>,这里是官方定义的内容:
ActionScript: source.actionscript.2
AppleScript: source.applescript
ASP: source.asp
Batch FIle: source.dosbatch
C#: source.cs
C++: source.c++
Clojure: source.clojure
CoffeeScript: source.coffee
CSS: source.css
D: source.d
Diff: source.diff
Erlang: source.erlang
Go: source.go
GraphViz: source.dot
Groovy: source.groovy
Haskell: source.haskell
HTML: text.html(.basic)
JSP: text.html.jsp
Java: source.java
Java Properties: source.java-props
Java Doc: text.html.javadoc
JSON: source.json
Javascript: source.js
BibTex: source.bibtex
Latex Log: text.log.latex
Latex Memoir: text.tex.latex.memoir
Latex: text.tex.latex
LESS: source.css.less
TeX: text.tex
Lisp: source.lisp
Lua: source.lua
MakeFile: source.makefile
Markdown: text.html.markdown
Multi Markdown: text.html.markdown.multimarkdown
Matlab: source.matlab
Objective-C: source.objc
Objective-C++: source.objc++
OCaml campl4: source.camlp4.ocaml
OCaml: source.ocaml
OCamllex: source.ocamllex
Perl: source.perl
PHP: source.php
Regular Expression(python): source.regexp.python
Python: source.python
R Console: source.r-console
R: source.r
Ruby on Rails: source.ruby.rails
Ruby HAML: text.haml
SQL(Ruby): source.sql.ruby
Regular Expression: source.regexp
RestructuredText: text.restructuredtext
Ruby: source.ruby
SASS: source.sass
Scala: source.scala
Shell Script: source.shell
SQL: source.sql
Stylus: source.stylus
TCL: source.tcl
HTML(TCL): text.html.tcl
Plain text: text.plain
Textile: text.html.textile
XML: text.xml
XSL: text.xml.xsl
YAML: source.yaml

参考博客——Sublime Text3—Code Snippets
参考博客——手把手教你写Sublime中的Snippet

发布了182 篇原创文章 · 获赞 71 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_43827595/article/details/103329250