vim-snipmate编写snippet的语法

vim-snipmate真的很好用,以前好多编写代码的问题得到完美的解决。还附带提升我对vim的理解和信心,在这里感谢一下作者。thank you。

1、现说一下我浓缩的重要语法。

  1、定义是下面这样,注意中间必须是一个制表符<TAB>不能用空格代替。     

snippet<TAB>关键字代码片段

  2、变量。${#},#代表变量数值,从1开始。还可以添加默认值。例如:${1:true},${2:/*condition*/},${3},${4:data},${5}等等。每按一次<TAB>键,光标会跳想下一个变量,如果没有变量了,则调到括号外。${0}有特殊意义,代表最后一跳的意思,${0}之后<TAB>键恢复正常。

  3、${0}的难点。for(${1:true}){ ${2}}和for(${1:true}){${0}}的区别,表达是1的变量${2}内不可按<TAB>键,否则会直接跳转的大括号外面。而表达式2可以按<TAB>键,因为,${0}代表最后一跳,所以在按<TAB>就可以进行其他的snippet了,而表达1里第一个snippet还没有执行完,这是按<TAB>就会跳到大括号外面。

  4、设置代码片段要综合考虑所有情况,并结合实践应用。例如: printf("${1:%s}"${2});  ,如果我只是打印一个字符串,那么在输入完${1:%s}后,我按 <ESC>o ,如果需要输入变量,那么我在按<TAB>跳转到下一个变量${2},这样就可以一举两得了。

  5、镜像,或者叫变量的引用。例如: printf("${1:test}=%d",$1); ,后面的$1会根据前面的${1:test}自动输入。

2、vim-snippet的主要语法是根据textmate来的,所以,snippet没找到,textmate的找到就可以喽,呵呵。

Snippets

A snippet is a piece of text that you would like to insert in your document. It can include code to run at insertion time, variables (like selected text), tab stops/placeholders for missing information (which you can tab through after insertion) and perform transformations on the data which you enter in the placeholders.

Snippet Editor

7.1 Plain Text

In the simplest case, you can use snippets to insert text that you do not want to type again and again, either because you type it a lot, or because the actual text to insert is hard to remember (like your bank account details or the HTML entities for the Apple modifier keys).

If you use snippets to insert plain text there is only one thing you should be aware of: $ and ` are reserved characters. So if you want to insert one of these, prefix it with an escape (i.e. \$). An escape not followed by one of these two characters (or followed by another escape) will be inserted as a literal character.

7.2 Variables

You can insert the value of a variable by prefixing the name of the variable with $. All the normal dynamic variables are supported, the most useful probably being TM_SELECTED_TEXT. If for example we want to create a snippet which wraps the selection in a LaTeX \textbf command, we can make a snippet which is:

\textbf{$TM_SELECTED_TEXT}

If no text is selected the variable will not be set, so nothing will be inserted in its place. We can provide a default value by using this syntax: ${«variable»:«default value»}. For example:

\textbf{${TM_SELECTED_TEXT:no text was selected}}

The default value can itself contain variables or shell code. If you want the default text to contain a }, you need to escape it. But all other characters are used verbatim.

Variables also support regular expression replacements using this syntax: ${«variable»/«regexp»/«format»/«options»}. If the variable is not set the replacement will be performed on the empty string. For example, to prepend a bullet to each non-empty line in the selection (and insert that) we can do:

${TM_SELECTED_TEXT/^.+$/• $0/g}

7.3 Interpolated Shell Code

You can use backticks to have shell code executed when the snippet is inserted. The result from running the code gets inserted into the snippet, though with the last newline in the result removed (if present). So for example to create a snippet that wraps the selection in an HTML link, where the URL of that link comes from the clipboard, we can do:

<a href="`pbpaste`.html">$TM_SELECTED_TEXT</a>

Since this is normal bash code, we can write a small program. For example we can let it verify that the clipboard contains only a single line of text like this:

<a href="`if [[ $(pbpaste|wc -l) -eq 0 ]]then pbpasteelse echo http://example.com/fi
`">$TM_SELECTED_TEXT</a>

Inside shell code, the only character you need to escape is the backtick.

7.4 Tab Stops

After insertion, the caret will be placed after the last character of the snippet. This is not always desirable and we can change that by using $0 to mark where we want the caret to be. So if for example we make an HTML div-snippet and want the caret to end between the opening and closing tags, we could make it like this:

<div>$0
</div>

Often though we want to fill in text in several places in the snippet. Multiple tab stops can be provided by inserting $1-$n. The caret will start at $1, then when pressing tab it will move to $2 and $3 on next tab etc. until there are no more tab stops. If you do not explicitly set $0, the caret will be at the end of the snippet.

So we could for example change the above to:

<div$1>$0
</div>

This allows us to fill in an argument and then tab on to $0.

7.5 Placeholders

Like variables, tab stops can also have default values (and are generally referred to as placeholders when they do). The syntax is the same: ${«tab stop»:«default value»}. And the default value can contain both text, shell code and other placeholders. So we can refine the previous example further:

<div${1: id="${2:some_id}"}>$0
</div>

Inserting this snippet will insert a div tag with the id argument selected and we can then decide either to overtype the argument (i.e. delete it) and press tab again to reach $0, or we can press tab immediately to get to the second tab stop (the value part of the argument) and edit that.

When you edit the placeholder text, any embedded tab stops will be removed.

7.6 Mirrors

There are times when you need to provide the same value several places in the inserted text and in these situations you can re-use the tab stop to signal that you want it mirrored at that location. So for example to create a LaTeX environment with a snippet, we can use:

\begin{${1:enumerate}}$0
\end{$1}

After inserting this snippet, enumerate will be selected and if we edit it, the changes will be reflected in the \end part as well.

7.7 Transformations

There are situations where we want our placeholder text mirrored but with slight changes or where we want some text to appear depending on the value/presence of a placeholder.

We can accomplish this by doing a regular expression substitution on the placeholder text (when mirroring it). The syntax for this is: ${«tab stop»/«regexp»/«format»/«options»}.

As an example, the Objective-C getter/setter methods (prior to the @property keyword) often look like this (in the thread-unsafe form):

- (id)foo
{return foo;
}- (void)setFoo:(id)aValue
{[foo autorelease];foo = [aValue retain];
}

In the format string we can use \u to uppercase the next character, so a snippet that only asks for the name of the instance variable once could look like this:

- (${1:id})${2:foo}
{return $2;
}- (void)set${2/./\u$0/}:($1)aValue
{[$2 autorelease];$2 = [aValue retain];
}

We can also use conditional insertions in the format string to make decisions. For example if we create a snippet for a method we can let the return type decide whether or not the method should include a return statement like this:

- (${1:void})${2:methodName}
{${1/void$|(.+)/(?1:\n\treturn nil;)/}
}

Here we match placeholder 1 against void or anything (.+) and put the latter match in capture register 1. Then only if we did match something (other than void) will we insert a newline, tab and the return nil; text.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/293675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android之Launcher分析和修改2——Icon修改、界面布局调整、壁纸设置

上一篇文章说了如何修改Android自带Launcher2的默认界面设置&#xff08;http://www.cnblogs.com/mythou/p/3153880.html&#xff09;。 今天主要是说说Launcher里面图标、布局、壁纸等的设置问题。毕竟我们一般修改Launcher&#xff0c;这些都是需要修改的地方&#xff0c;也是…

捷径 - The certain shortcut

赛斯高汀(Seth Godin)的博客&#xff1a;http://sethgodin.typepad.com/seths_blog/2013/05/the-certain-shortcut.html The shortcut thats sure to work, every time: 所谓的捷径向来如此&#xff1a; Take the long way. 脚踏实地&#xff0c;一步一个脚印(遍历) Do t…

高效率三大法则总结

原则一&#xff1a;专注 之前经常听到很多人所谓的牛人&#xff0c;说什么边干活的时候可以开着msn、qq&#xff0c;工作和娱乐两不误&#xff0c;边听下属汇报的同时边写下午会议的发言稿&#xff0c;然而科学事实证明人类的大脑硬件结构决定了根本不可能一脑两用&#xff0c;…

N 年沉淀,腾讯这套系统终于开源!

大家好&#xff0c;我是鱼皮&#xff0c;前段时间给大家介绍了字节跳动开源的两套设计系统&#xff0c;分别是 Arco Design 和抖音 Semi Design。而就在几天前&#xff0c;腾讯终于也开源了自家的设计系统 TDesgin &#xff01;这次&#xff0c;终于能介绍自己公司的项目了。如…

在php中使用sockets:从新闻组中获取文章

PHP能打开远程或本地主机上的Socket端口。本文是一个使用Socket的小例子&#xff1a;连 接到一个Usenet新闻组服务器&#xff0c;同服务器对话&#xff0c;从新闻组中下载一些文章。在php中打开一个socket 使用fsockopen()打开一个socket.这个函数在php3和php4种都可以使用。函…

Android之Launcher分析和修改3——Launcher启动和初始化

前面两篇文章都是写有关Launcher配置文件的修改&#xff0c;代码方面涉及不多&#xff0c;今天开始进入Launcher代码分析。 我们开机启动Launcher&#xff0c;Launcher是由Activity Manager启动的&#xff0c;而Activity Manager是由system server启动。 原创博文&#xff0c…

史上最强物理科普!

全世界只有3.14 % 的人关注了爆炸吧知识一沙见世界 一花窥天堂手心握无限 须臾纳永恒杨振宁曾说读上面的四句诗可以感受到物理的美但物理的美不止于此物理还有一种庄严美一种神秘美一种初窥宇宙奥秘的畏惧美物理就是如此的迷人任何语言在它的面前都很贫瘠数学让人摆脱了愚昧而…

mysql 怎么实现随机查询并分页,不重复查询

2019独角兽企业重金招聘Python工程师标准>>> 需求&#xff1a; 企业应用中&#xff0c;一般数据量不是很特别多&#xff0c;同时大多在局域网内&#xff0c;性能不会有问题&#xff0c;一般不会分页处理&#xff0c;随机排序数据一般不会有问题 web网站中&#xf…

评分卡模型剖析之一(woe、IV、ROC、信息熵)

信用评分卡模型在国外是一种成熟的预测方法&#xff0c;尤其在信用风险评估以及金融风险控制领域更是得到了比较广泛的使用&#xff0c;其原理是将模型变量WOE编码方式离散化之后运用logistic回归模型进行的一种二分类变量的广义线性模型。 本文重点介绍模型变量WOE以及IV原理&…

主机远程唤醒配置

配置&#xff1a;win10戴尔主机F2进biosbios设置2.系统设置3.以太网设置4.电源管理设置以上设置完成后关机&#xff0c;就可以在远程电脑上打开WakeMeOnLan&#xff0c;然后添加或者搜索需要唤醒的电脑的ip&#xff0c;mac和主机名称&#xff0c;然后就可以唤醒远程主机了查看m…

Java字节序,java整型数与网络字节序 byte[] 数组转换关系

Java字节序 http://origin100.iteye.com/blog/267165 /*** 通信格式转换** Java和一些windows编程语言如c、c、delphi所写的网络程序进行通讯时&#xff0c;需要进行相应的转换* 高、低字节之间的转换* windows的字节序为低字节开头* linux,unix的字节序为高字节开头* java则无…

python dict.fromkeys()研究

def unique(seq): #return [x for x in my_list if x not in locals()[_[1]]] return {}.fromkeys(seq).keys() dict.fromkeys(seq,valNone) #创建并返回一个新字典&#xff0c;以序列seq中元素做字典的键&#xff0c;val为字典所有键对应的初始值(默认为None) 例子&#xff…

unix高级编程apue.h问题

2019独角兽企业重金招聘Python工程师标准>>> apue.h头文件为作者自己编写而非系统自带&#xff0c;故需要自行添加&#xff01;第一&#xff1a;打开网站 http://www.apuebook.com/第二&#xff1a;选择合适的版本&#xff08;一共有三个版本&#xff0c;根据书的版…

Vim文本编辑器 常用指令大全 提升编程效率必备法宝之一

经常处理文本以及经常需要写代码的人&#xff0c;都会有自己比较常用的编辑器&#xff0c;本人喜欢用Vim&#xff0c;理由就是Vim编辑器灵活&#xff0c;并且可以达到纯键盘操作&#xff0c;使用纯熟情况下&#xff0c;根本不需要鼠标操作&#xff0c;听起来是不是很酷的&#…

IT与业务之间的鸿沟根源

目前世界经济进入到数字经济时期&#xff0c;IT与业务的融合已经不是什么新鲜的话题&#xff0c;其实从计算机的诞生的那一天开始&#xff0c;IT与业务融合的问题就已经开始了。1、经常听到一种观点&#xff1a;IT与业务之间的鸿沟这归结于谁领导谁的问题。&#xff08;1&#…

Java里面Unreachable code

今天写了一段Java代码&#xff0c;不小心出了“Unreachable code”错误&#xff0c;开始的时候MyEclipse一直提示我要我重命名一个对象&#xff0c;但我重命名了也不好用&#xff0c;最后才看到这个“Unreachable code”错误&#xff0c;在网上调查了一下&#xff0c;这个错误翻…

EIGRP stub SIA

转载于:https://blog.51cto.com/liushuo890/1202399

C++Vector使用方法

C内置的数组支持容器的机制&#xff0c;可是它不支持容器抽象的语义。要解决此问题我们自己实现这种类。在标准C中&#xff0c;用容器向量&#xff08;vector&#xff09;实现。容器向量也是一个类模板。标准库vector类型使用须要的头文件&#xff1a;#include <vector>。…

岛国人气美少女竟然每晚跟 3 个人通宵打麻将?

1 桥本怕不是四川人?▼2 借口总比困难多▼3 突然觉得自己是大厨▼4 连自己都不放过▼5 这就是纹身的烦恼▼6 这又是什么黑暗料理&#xff08;via.坏蛋王师傅&#xff09;▼7 哦吼&#xff0c;是在下输了▼7 哦吼&#xff0c;是在下输了▼你点的每个赞&#xff0c;我都认…