比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>(&lt;/?)(?:([-_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>(/?&gt;)</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|\*|/|%|\&amp;|\||\^|&lt;&lt;|&gt;&gt;|==|!=|&lt;|&gt;|&lt;=|&gt;=)\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/UXdqo4。然后設置環境變量,並設置一下配置三就可以了,當然有很多功能還有待自己摸索和開發哦

參考博客http://www.cnblogs.com/IPrograming/archive/2013/05/02/ST2_CSharpConfig.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM