在Sublimet Text2中创建snippet片段


=Start=

为Golang格式的代码创建snippet片段
$ cd $HOME/Library/Application\ Support/Sublime\ Text\ 2/Packages/
$ find . -type f -iname "*.tmSnippet"
$ cp ./Go/func.tmSnippet ./User/go_comment.tmSnippet

$ vim ./User/go_comment.tmSnippet
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>content</key>
	<string>/*
$0
*/</string>
	<key>name</key>
	<string>Multi-line comment</string>
	<key>scope</key>
	<string>source.go</string>
	<key>tabTrigger</key>
	<string>comment</string>
	<key>uuid</key>
	<string>E9B44CC5-B004-4793-B125-7E429FDCCE99</string>
</dict>
</plist>

上面的内容中有几个注意事项:

1.作用范围设定为Golang代码
<string>source.go</string>

2.uuid的值需要唯一
<key>uuid</key>
<string>E9B44CC5-B004-4793-B125-7E429FDCCE99</string>

3.后缀名
需要为".tmSnippet"

4.保存路径
保存路径为"$HOME/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/"
参考链接:
为Bash格式的代码创建snippet片段

在菜单上点击”Tools -> New Snippet”,会新建一个XML格式的文件,初始内容如下:

<snippet>
	<content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<!-- <tabTrigger>hello</tabTrigger> -->
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.python</scope> -->
</snippet>

修改为:

<snippet>
    <content><![CDATA[#!/bin/bash
# Author:	ixyzero.com
# Date:		${1:datetime}
# set -x
export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin/:/usr/local/sbin

VERBOSE="N"

function func_name() {
    # to do
    :
}

function func_main() {
    func_name
}
func_main
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>ibash</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.shell</scope>
</snippet>

注:本来想插入的snippet是功能更全的一个模板,但是,因为$1、$0这些和Bash的命令行参数冲突,并且,${BASH_SOURCE}、${LINENO}、${FUNCNAME[0]}、……等冲突,所以只能缩减为上面那样才行。

=EOF=


《“在Sublimet Text2中创建snippet片段”》 有 2 条评论

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注