原文:比NotePad++更好的文本代码(C#)编辑器Sublime Text
前言
前两天在博客园看到@晴天猪的博客发表的关于他使用的代码编辑器,自己索性试了一下,果断好用,自己也来记录一下。以便以后配置使用。接下来我配置的主要是简单的编译C#代码的。
配置一调用C#编译器
我现在电脑的系统为Win7哦。我要将C#编译器的csc.exe文件添加到环境变量中。
首先我的电脑==右键属性==高级系统设置==环境变量==系统变量==变量Path双击==在变量值中将路径添加到后面添加前用;分隔C:\Windows\Microsoft.NET\Framework\v4.0.30319
配置二新建编译器选项
打开Submile Text
工具 编译系统 最下面的编译新系统,然后将如下命令斤西瓜复制
{ "cmd": ["csc", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.cs", "encoding": "cp936" }
另存为ST2程序目录的Packages/User文件夹下面,文件名为:C#.sublime-build。
测试编译代码
下面在ST中编写一些简单的代码如下
using System; using System.Linq; class Program { static void Main(string[] args) { Console.WriteLine("Hello World"); Console.ReadLine(); } }
将代码随便保存为一个文件后缀为cs哦。然后Ctrl+B就OK了。
现在只能编译,但是不能测试运行。你我们接下来配置一下吧。
配置三能让代码运行起来
其实很简单,就是在C#编译器路径中我的电脑也就是C:\Windows\Microsoft.NET\Framework\v4.0.30319中创建RunCSharp.bat文件,然后在其中写入以下内容:
@ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.exe ( DEL %~n1.exe ) csc %~nx1 IF EXIST %~n1.exe ( ECHO -----------OUTPUT----------- start %~n1 )
当然光添加上述文件还不够,还需要修改刚刚在编译器中添加的C#.sublime-build,修改后内容如下:
{ "cmd": ["RunCSharp.bat", "$file"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.cs", "encoding": "cp936" }
下面再Ctrl+B一下刚刚写的简单的代码吧
配置四可以给代码添加注释
C#中的注释快捷键是无效的,这是因为Packages文件夹中缺少了定义注释行为的文件。打开Packages,在C#文件夹中添加一个名为:Comments.tmPreferences文件,输入如下内容:
View Code
View Code <?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>name</key> <string>Comments</string> <key>scope</key> <string>source.cs</string> <key>settings</key> <dict> <key>shellVariables</key> <array> <dict> <key>name</key> <string>TM_COMMENT_START</string> <key>value</key> <string>// </string> </dict> <dict> <key>name</key> <string>TM_COMMENT_START_2</string> <key>value</key> <string>/*</string> </dict> <dict> <key>name</key> <string>TM_COMMENT_END_2</string> <key>value</key> <string>*/</string> </dict> </array> </dict> <key>uuid</key> <string>FBA964F9-2013-44D1-A5FD-AE8AB3FF8954</string> </dict> </plist>
添加注释文件后,就可以为C#代码添加注释了。
配置五可以添加关键字高亮
编程语言的关键字在ST2中是高亮显示的,对于ST2我们需要自己定义一下关键字,例如:virtual,var等,这时我们需要修改Packages文件夹中的C#文件夹的C#.tmLanguage文件,修改后文件的内容如下:
View Code
<?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>fileTypes</key> <array> <string>cs</string> </array> <key>foldingStartMarker</key> <string>^\s*/\*|^(?![^{]*?//|[^{]*?/\*(?!.*?\*/.*?\{)).*?\{\s*($|//|/\*(?!.*?\*/.*\S))</string> <key>foldingStopMarker</key> <string>^\s*\*/|^\s*\}</string> <key>keyEquivalent</key> <string>^~C</string> <key>name</key> <string>C#</string> <key>patterns</key> <array> <dict> <key>begin</key> <string>///</string> <key>captures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.comment.source.cs</string> </dict> </dict> <key>end</key> <string>$\n?</string> <key>name</key> <string>comment.block.documentation.source.cs</string> <key>patterns</key> <array> <dict> <key>begin</key> <string>(</?)(?:([-_a-zA-Z0-9]+)((:)))?([-_a-zA-Z0-9:]+)</string> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>punctuation.definition.tag.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>entity.name.tag.namespace.source.cs</string> </dict> <key>3</key> <dict> <key>name</key> <string>entity.name.tag.source.cs</string> </dict> <key>4</key> <dict> <key>name</key> <string>punctuation.separator.namespace.source.cs</string> </dict> <key>5</key> <dict> <key>name</key> <string>entity.name.tag.localname.source.cs</string> </dict> </dict> <key>end</key> <string>(/?>)</string> <key>name</key> <string>keyword.other.documentation.source.cs</string> <key>patterns</key> <array> <dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>entity.other.attribute-name.namespace.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>entity.other.attribute-name.source.cs</string> </dict> <key>3</key> <dict> <key>name</key> <string>punctuation.separator.namespace.source.cs</string> </dict> <key>4</key> <dict> <key>name</key> <string>entity.other.attribute-name.localname.source.cs</string> </dict> </dict> <key>match</key> <string> (?:([-_a-zA-Z0-9]+)((:)))?([_a-zA-Z-]+)=</string> </dict> <dict> <key>include</key> <string>#doubleQuotedString</string> </dict> <dict> <key>include</key> <string>#singleQuotedString</string> </dict> </array> </dict> </array> </dict> <dict> <key>include</key> <string>#comments</string> </dict> <dict> <key>begin</key> <string>(?x)^\s* ((?:\b(?:new|public|protected|internal|private|abstract|sealed|static)\b\s)*) (class)\s+ ([A-Za-z_]\w+)\b</string> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>storage.modifier.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>storage.type.source.cs</string> </dict> <key>3</key> <dict> <key>name</key> <string>entity.name.type.class.source.cs</string> </dict> </dict> <key>end</key> <string>{</string> <key>name</key> <string>meta.definition.class.source.cs</string> <key>patterns</key> <array> <dict> <key>include</key> <string>#classInheritance</string> </dict> </array> </dict> <!-- Disabled because it causes some lines to be interpreted incorrectly, for example: else if (c == ')') ---> <!-- <dict> <key>begin</key> <string>(?x)^\s* # start of line ((?:\b(?:new|public|protected|internal|private|static|virtual|sealed|override|abstract|extern)\b\s*)*) # method-modifiers \b((?:\w+\.)*[A-Za-z_]\w*)\b\s* # type (operator)\s+ # operator overload ((?:\+|-|!|~|\+\+|--|true|false|\*|/|%|\&|\||\^|<<|>>|==|!=|<|>|<=|>=)\s*) # operator name (?=\()</string> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>storage.modifier.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>storage.type.source.cs</string> </dict> <key>3</key> <dict> <key>name</key> <string>storage.modifier.source.cs</string> </dict> <key>4</key> <dict> <key>name</key> <string>entity.name.function.source.cs</string> </dict> </dict> <key>end</key> <string>\)</string> <key>name</key> <string>meta.definition.operator.source.cs</string> <key>patterns</key> <array> <dict> <key>include</key> <string>#statementRemainder</string> </dict> </array> </dict> <dict> <key>begin</key> <string>(?x)^\s* # start of line ((?:\b(?:new|public|protected|internal|private|static|virtual|sealed|override|abstract|extern)\b\s*)*) # method-modifiers \b((?:\w+\.)*[A-Za-z_]\w*)\b\s* # type ([A-Za-z_]\w*)\s* # name (?=\()</string> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>storage.modifier.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>storage.type.source.cs</string> </dict> <key>3</key> <dict> <key>name</key> <string>entity.name.function.source.cs</string> </dict> </dict> <key>end</key> <string>\)</string> <key>name</key> <string>meta.definition.method.source.cs</string> <key>patterns</key> <array> <dict> <key>include</key> <string>#statementRemainder</string> </dict> </array> </dict> --> <dict> <key>match</key> <string>\b(true|false|null|this|base)\b</string> <key>name</key> <string>constant.language.source.cs</string> </dict> <dict> <key>match</key> <string>\b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\b</string> <key>name</key> <string>constant.numeric.source.cs</string> </dict> <dict> <key>match</key> <string>\b(if|else|while|for|foreach|do|return|continue|break|switch|case|default|goto|throw|try|catch|finally|lock|yield)\b</string> <key>name</key> <string>keyword.control.source.cs</string> </dict> <dict> <key>match</key> <string>\b(new|is|checked|unchecked|typeof|sizeof|override|in|out|ref|readonly|params|stackalloc|as)\b</string> <key>name</key> <string>keyword.operator.source.cs</string> </dict> <dict> <key>match</key> <string>\b(event|delegate|explicit|implicit|in|set|get)\b</string> <key>name</key> <string>keyword.other.source.cs</string> </dict> <dict> <key>match</key> <string>\b(internal|public|protected|private|static|const|new|sealed|abstract|virtual|override|extern|unsafe|readonly|volatile|operator)\b</string> <key>name</key> <string>storage.modifier.source.cs</string> </dict> <dict> <key>include</key> <string>#doubleQuotedStringLiteral</string> </dict> <dict> <key>include</key> <string>#doubleQuotedString</string> </dict> <dict> <key>include</key> <string>#singleQuotedString</string> </dict> <dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>keyword.other.using.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>entity.name.type.package.source.cs</string> </dict> </dict> <key>match</key> <string>^\s*(using)\s+([^ ;]*);</string> <key>name</key> <string>meta.keyword.using.source.cs</string> </dict> <dict> <key>include</key> <string>#builtinTypes</string> </dict> <dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>keyword.other.namespace.source.cs</string> </dict> <key>2</key> <dict> <key>name</key> <string>entity.name.type.namespace.source.cs</string> </dict> </dict> <key>match</key> <string>^\s*(namespace)\s+([^ ]+)(?:\s*{)?$</string> <key>name</key> <string>meta.keyword.namespace.source.cs</string> </dict> <dict> <key>captures</key> <dict> <key>2</key> <dict> <key>name</key> <string>keyword.control.import.source.cs</string> </dict> </dict> <key>match</key> <string>^(#)\s*(if|else|elif|endif|define|undef|warning|error|line|region|endregion)\b</string> <key>name</key> <string>meta.preprocessor.source.cs</string> </dict> </array> <key>repository</key> <dict> <key>builtinTypes</key> <dict> <key>patterns</key> <array> <dict> <key>match</key> <string>\b(bool|byte|sbyte|char|decimal|double|float|int|uint|long|ulong|object|short|ushort|string|void|class|struct|enum|interface|var|from|where|select|group|into|orderby|join|let|ascending|descending|on|by)\b</string> <key>name</key> <string>storage.type.source.cs</string> </dict> </array> </dict> <key>classInheritance</key> <dict> <key>patterns</key> <array> <dict> <key>begin</key> <string>:</string> <key>end</key> <string>(?={)</string> <key>patterns</key> <array> <dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>storage.type.source.cs</string> </dict> </dict> <key>match</key> <string>\s*,?([A-Za-z_]\w*)\b</string> </dict> </array> </dict> </array> </dict> <key>comments</key> <dict> <key>patterns</key> <array> <dict> <key>begin</key> <string>/\*</string> <key>captures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.comment.source.cs</string> </dict> </dict> <key>end</key> <string>\*/\n?</string> <key>name</key> <string>comment.block.source.cs</string> </dict> <dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>punctuation.definition.comment.source.cs</string> </dict> </dict> <key>match</key> <string>(//).*$\n?</string> <key>name</key> <string>comment.line.double-slash.source.cs</string> </dict> </array> </dict> <key>doubleQuotedString</key> <dict> <key>begin</key> <string>"</string> <key>beginCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.begin.source.cs</string> </dict> </dict> <key>end</key> <string>"</string> <key>endCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.end.source.cs</string> </dict> </dict> <key>name</key> <string>string.quoted.double.source.cs</string> <key>patterns</key> <array> <dict> <key>match</key> <string>\\.</string> <key>name</key> <string>constant.character.escape.source.cs</string> </dict> </array> </dict> <key>doubleQuotedStringLiteral</key> <dict> <key>captures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.begin.source.cs</string> </dict> </dict> <key>match</key> <string>@"([^"]|"")*"</string> <key>name</key> <string>string.quoted.double.literal.source.cs</string> </dict> <key>singleQuotedString</key> <dict> <key>begin</key> <string>'</string> <key>beginCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.begin.source.cs</string> </dict> </dict> <key>end</key> <string>'</string> <key>endCaptures</key> <dict> <key>0</key> <dict> <key>name</key> <string>punctuation.definition.string.end.source.cs</string> </dict> </dict> <key>name</key> <string>string.quoted.single.xml</string> <key>patterns</key> <array> <dict> <key>match</key> <string>\\.</string> <key>name</key> <string>constant.character.escape.source.cs</string> </dict> </array> </dict> <key>statementRemainder</key> <dict> <key>patterns</key> <array> <dict> <key>begin</key> <string>\(</string> <key>end</key> <string>(?=\))</string> <key>name</key> <string>meta.definition.param-list.source.cs</string> <key>patterns</key> <array> <dict> <key>include</key> <string>#builtinTypes</string> </dict> </array> </dict> </array> </dict> </dict> <key>scopeName</key> <string>source.cs</string> <key>uuid</key> <string>1BA75B32-707C-11D9-A928-000D93589AF6</string> </dict> </plist>
配置六可以添加代码片段
对于一些常用的代码片段,我们不需要每次都手动输入一遍,可以将它们配置问代码片段,减少手动代码输入量,效果类似于Visual Studio的智能提示,如下:
例如输入fore时,会出现foreach的提示:
然后Enter后就可以看到
static void Main(string[] args) { Console.WriteLine("Hello World"); foreach (var item in ) { } Console.ReadLine(); }
总结
对了,还可以修改字体大小哦
感觉很好用哦。
配置文件下载:C#.zip (将所有文件复制Packages文件夹下的C#文件夹即可,配置文件包括常用的代码片段,注释配置,和关键字的定义。)
这是我现在使用的Submile Text所有文件,下载解压http://url.cn/HI5z0T。然后设置环境变量,并设置一下配置三就可以了,当然有很多功能还有待自己摸索和开发哦
参考博客http://www.cnblogs.com/IPrograming/archive/2013/05/02/ST2_CSharpConfig.html