比NotePad++更好的文本代碼(C#)編輯器Sublime Text

原文: 比NotePad++更好的文本代碼(C#)編輯器Sublime Text

前言html

 前兩天在博客園看到@晴天豬的博客發表的關於他使用的代碼編輯器,本身索性試了一下,果斷好用,本身也來記錄一下。以便之後配置使用。接下來我配置的主要是簡單的編譯C#代碼的。shell

配置一調用C#編譯器編程

 我如今電腦的系統爲Win7哦。我要將C#編譯器的csc.exe文件添加到環境變量中。app

首先個人電腦==右鍵屬性==高級系統設置==環境變量==系統變量==變量Path雙擊==在變量值中將路徑添加到後面添加前用;分隔C:\Windows\Microsoft.NET\Framework\v4.0.30319編程語言

配置二新建編譯器選項編輯器

 打開Submile Textide

 工具  編譯系統   最下面的編譯新系統,而後將以下命令斤西瓜複製工具

{
      "cmd": ["csc", "$file"],
      "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
      "selector": "source.cs",
      "encoding": "cp936"
 }

另存爲ST2程序目錄的Packages/User文件夾下面,文件名爲:C#.sublime-build。post

測試編譯代碼測試

 下面在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/HI5z0T。而後設置環境變量,並設置一下配置三就能夠了,固然有不少功能還有待本身摸索和開發哦

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

相關文章
相關標籤/搜索