小编长期都在使用Visual Studio这个全球最强大的IDE(没有之一),但是有些时候,往往需要查找、或者是替换多行文本,这个时候,对于VS来说可能有点压力了,因为默认的替换只能支持单行文本(虽然宏里面的FindLine是可以支持多行查找的,但是不能多行替换,稍后会详细说明下)。
给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:上篇)
这个图是增加多行查找与多行替换功能的效果图,还不错吧!
接下来,等小牛来介绍一下如何添加多行查找与多行替换功能?
只要几个步骤就可加入功能。
1. 在 VS 中打开 Macros Explorer(宏 资源管理器) ,在 工具–宏–宏资源管理器(或者快捷键Alt+F8)。
2. 在 宏资源管理器 的 MyMacros 中新建一个宏 MultilineSearch
3. 双击 MultilineSearch ,则显出 宏的IDE和生成的新宏的vb代码
4. 把 新宏的vb代码内容都删除,把如下代码拷进去
C# Code复制内容到剪贴板 ’1.在vs.net中打开MacrosExplorer(宏资源管理器),在工具–宏–宏资源管理器。 ’2.在宏资源管理器的MyMacros中新建一个宏MultilineSearch ’3.双击MultilineSearch,则显出宏的IDE和生成的新宏的vb代码 ’4.把新宏的vb代码内容都删除,把如下代码拷进去 ’5.将System.Drawing.dll加入宏工程的引用 ’6.关闭宏IDE ImportsEnvDTE ImportsSystem.Diagnostics PublicModuleMultilineSearch SubMultilineSearchReplace() DimsfAsNewMultilineSearchForm sf.ShowDialog() Ifsf.result<>FindReplaceKind.noneThen ‘temporarilydisableTools-Options- ‘Environment-Documents-InitializeFindtextfromeditor DimoldFindInitAsBoolean Try DimpropsAsEnvDTE.Properties props=DTE.Properties(“Environment”,“Documents”) DimpropAsEnvDTE.Property=props.Item(“FindReplaceInitializeFromEditor”) oldFindInit=prop.Value prop.Value=False CatchexAsSystem.Exception EndTry DTE.Find.PatternSyntax=vsFindPatternSyntax.vsFindPatternSyntaxRegExpr DTE.Find.FindWhat=sf.findText DTE.Find.ReplaceWith=sf.replaceText SelectCasesf.result CaseFindReplaceKind.find DTE.ExecuteCommand(“Edit.Find”) CaseFindReplaceKind.findInFiles DTE.ExecuteCommand(“Edit.FindinFiles”) CaseFindReplaceKind.replace DTE.ExecuteCommand(“Edit.Replace”) CaseFindReplaceKind.replaceInFiles DTE.ExecuteCommand(“Edit.ReplaceinFiles”) CaseElse EndSelect ‘restoreTools-Options- ‘Environment-Documents-InitializeFindtextfromeditor Try DimpropsAsEnvDTE.Properties props=DTE.Properties(“Environment”,“Documents”) DimpropAsEnvDTE.Property=props.Item(“FindReplaceInitializeFromEditor”) prop.Value=oldFindInit CatchexAsSystem.Exception EndTry EndIf EndSub EndModule ”’<summary>Typesoffind/replaceoperations.</summary> PublicEnumFindReplaceKind ”’<summary>Find</summary> find ”’<summary>FindInFiles</summary> findInFiles ”’<summary>Replace</summary> replace ”’<summary>ReplaceinFiles</summary> replaceInFiles ”’<summary>None.Cancelwaspressed.</summary> none EndEnum PublicClassMultilineSearchForm InheritsSystem.Windows.Forms.Form #Region“WindowsFormDesignergeneratedcode” PublicSubNew() MyBase.New() ‘ThiscallisrequiredbytheWindowsFormDesigner. InitializeComponent() ‘AddanyinitializationaftertheInitializeComponent()call EndSub ‘Formoverridesdisposetocleanupthecomponentlist. ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean) IfdisposingThen IfNot(componentsIsNothing)Then components.Dispose() EndIf EndIf MyBase.Dispose(disposing) EndSub ‘RequiredbytheWindowsFormDesigner PrivatecomponentsAsSystem.ComponentModel.IContainer ‘NOTE:ThefollowingprocedureisrequiredbytheWindowsFormDesigner ‘ItcanbemodifiedusingtheWindowsFormDesigner. ‘Donotmodifyitusingthecodeeditor. FriendWithEventsFindBoxAsSystem.Windows.Forms.TextBox FriendWithEventsLabel1AsSystem.Windows.Forms.Label FriendWithEventsLabel2AsSystem.Windows.Forms.Label FriendWithEventsReplaceBoxAsSystem.Windows.Forms.TextBox FriendWithEventsFindBtnAsSystem.Windows.Forms.Button FriendWithEventsFindInFilesBtnAsSystem.Windows.Forms.Button FriendWithEventsReplaceBtnAsSystem.Windows.Forms.Button FriendWithEventsReplaceInFilesBtnAsSystem.Windows.Forms.Button FriendWithEventsCancelBtnAsSystem.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent() Me.FindBox=NewSystem.Windows.Forms.TextBox Me.Label1=NewSystem.Windows.Forms.Label Me.Label2=NewSystem.Windows.Forms.Label Me.ReplaceBox=NewSystem.Windows.Forms.TextBox Me.FindBtn=NewSystem.Windows.Forms.Button Me.FindInFilesBtn=NewSystem.Windows.Forms.Button Me.ReplaceBtn=NewSystem.Windows.Forms.Button Me.ReplaceInFilesBtn=NewSystem.Windows.Forms.Button Me.CancelBtn=NewSystem.Windows.Forms.Button Me.SuspendLayout() ‘ ‘FindBox ‘ Me.FindBox.Location=NewSystem.Drawing.Point(16,24) Me.FindBox.Multiline=True Me.FindBox.Name=“FindBox” Me.FindBox.ScrollBars=System.Windows.Forms.ScrollBars.Both Me.FindBox.Size=NewSystem.Drawing.Size(400,80) Me.FindBox.TabIndex=0 Me.FindBox.Text=“” ‘ ‘Label1 ‘ Me.Label1.Location=NewSystem.Drawing.Point(16,8) Me.Label1.Name=“Label1″ Me.Label1.Size=NewSystem.Drawing.Size(160,16) Me.Label1.TabIndex=2 Me.Label1.Text=“查找内容:” ‘ ‘Label2 ‘ Me.Label2.Location=NewSystem.Drawing.Point(16,112) Me.Label2.Name=“Label2″ Me.Label2.Size=NewSystem.Drawing.Size(160,16) Me.Label2.TabIndex=4 Me.Label2.Text=“替换为:” ‘ ‘ReplaceBox ‘ Me.ReplaceBox.Location=NewSystem.Drawing.Point(16,128) Me.ReplaceBox.Multiline=True Me.ReplaceBox.Name=“ReplaceBox” Me.ReplaceBox.ScrollBars=System.Windows.Forms.ScrollBars.Both Me.ReplaceBox.Size=NewSystem.Drawing.Size(400,80) Me.ReplaceBox.TabIndex=3 Me.ReplaceBox.Text=“” ‘ ‘FindBtn ‘ Me.FindBtn.Location=NewSystem.Drawing.Point(16,232) Me.FindBtn.Name=“FindBtn” Me.FindBtn.Size=NewSystem.Drawing.Size(80,24) Me.FindBtn.TabIndex=5 Me.FindBtn.Text=“查找” ‘ ‘FindInFilesBtn ‘ Me.FindInFilesBtn.Location=NewSystem.Drawing.Point(104,232) Me.FindInFilesBtn.Name=“FindInFilesBtn” Me.FindInFilesBtn.Size=NewSystem.Drawing.Size(96,24) Me.FindInFilesBtn.TabIndex=6 Me.FindInFilesBtn.Text=“在文件中查找” ‘ ‘ReplaceBtn ‘ Me.ReplaceBtn.Location=NewSystem.Drawing.Point(216,232) Me.ReplaceBtn.Name=“ReplaceBtn” Me.ReplaceBtn.Size=NewSystem.Drawing.Size(80,24) Me.ReplaceBtn.TabIndex=7 Me.ReplaceBtn.Text=“替换” ‘ ‘ReplaceInFilesBtn ‘ Me.ReplaceInFilesBtn.Location=NewSystem.Drawing.Point(304,232) Me.ReplaceInFilesBtn.Name=“ReplaceInFilesBtn” Me.ReplaceInFilesBtn.Size=NewSystem.Drawing.Size(112,24) Me.ReplaceInFilesBtn.TabIndex=8 Me.ReplaceInFilesBtn.Text=“在文件中替换” ‘ ‘CancelBtn ‘ Me.CancelBtn.DialogResult=System.Windows.Forms.DialogResult.Cancel Me.CancelBtn.Location=NewSystem.Drawing.Point(168,272) Me.CancelBtn.Name=“CancelBtn” Me.CancelBtn.Size=NewSystem.Drawing.Size(80,24) Me.CancelBtn.TabIndex=9 Me.CancelBtn.Text=“取消” ‘ ‘MultilineSearchForm ‘ Me.AutoScaleBaseSize=NewSystem.Drawing.Size(5,13) Me.CancelButton=Me.CancelBtn Me.ClientSize=NewSystem.Drawing.Size(432,310) Me.Controls.Add(Me.CancelBtn) Me.Controls.Add(Me.ReplaceInFilesBtn) Me.Controls.Add(Me.ReplaceBtn) Me.Controls.Add(Me.FindInFilesBtn) Me.Controls.Add(Me.FindBtn) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.ReplaceBox) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.FindBox) Me.FormBorderStyle=System.Windows.Forms.FormBorderStyle.SizableToolWindow Me.Name=“MultilineSearchForm” Me.Text=“多行查找与替换-www.wuleba.com” Me.ResumeLayout(False) EndSub #EndRegion #Region“Properties” Privatem_resultAsFindReplaceKind=FindReplaceKind.none ”’<summary>Getsresultbuttonfromthisdialog.</summary> ”’<value>Thevaluespecifyingwhichbuttonwaspressed.</value> PublicReadOnlyPropertyresult()AsFindReplaceKind Get Returnm_result EndGet EndProperty Privatem_findTextAsString ”’<summary>Getsescapedmultilinetexttobesearched.</summary> ”’<value></value> PublicReadOnlyPropertyfindText()AsString Get Returnm_findText EndGet EndProperty Privatem_replaceTextAsString ”’<summary>Getsescapedmultilinereplacetext.</summary> ”’<value></value> PublicReadOnlyPropertyreplaceText()AsString Get Returnm_replaceText EndGet EndProperty #EndRegion ”’<summary>Transformsthetexttoregularexpressionsyntax.</summary> ”’<paramname=”original”>Originaltext.</param> ”’<returns>Textwithescapedregexcharacters.</returns> PrivateFunctionescapeRegEx(ByValoriginalAsString)AsString DimspecialChars()AsChar=“.*+^___FCKpd___0gt;<[]|{}:@#()~”.ToCharArray DimcAsChar ForEachcInspecialChars original=original.Replace(c.ToString,“”&c.ToString) Next original=original.Replace(vbCrLf,“n”) Returnoriginal EndFunction PrivateSubMultilineSearchForm_Load(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMyBase.Load Try Me.Activate() CatchexAsSystem.Exception EndTry EndSub PrivateSubCancelBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesCancelBtn.Click Try m_result=FindReplaceKind.none Me.Close() CatchexAsSystem.Exception EndTry EndSub PrivateSubFindBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesFindBtn.Click Try m_findText=escapeRegEx(Me.FindBox.Text) m_replaceText=escapeRegEx(Me.ReplaceBox.Text) m_result=FindReplaceKind.find Me.Close() CatchexAsSystem.Exception EndTry EndSub PrivateSubFindInFilesBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesFindInFilesBtn.Click Try m_findText=escapeRegEx(Me.FindBox.Text) m_replaceText=escapeRegEx(Me.ReplaceBox.Text) m_result=FindReplaceKind.findInFiles Me.Close() CatchexAsSystem.Exception EndTry EndSub PrivateSubReplaceBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesReplaceBtn.Click Try m_findText=escapeRegEx(Me.FindBox.Text) m_replaceText=escapeRegEx(Me.ReplaceBox.Text) m_result=FindReplaceKind.replace Me.Close() CatchexAsSystem.Exception EndTry EndSub PrivateSubReplaceInFilesBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesReplaceInFilesBtn.Click Try m_findText=escapeRegEx(Me.FindBox.Text) m_replaceText=escapeRegEx(Me.ReplaceBox.Text) m_result=FindReplaceKind.replaceInFiles Me.Close() CatchexAsSystem.Exception EndTry EndSub EndClass
5. 将 System.Drawing.dll 加入 宏工程 的引用
6. 关闭宏IDE
操作完上面的6个步骤之后,你的VS2012就多了个多行搜索和替换文本的工具,效果图就是最上面的那个图。为了更方便大家学习,小牛打包了上面的这个vb代码,需要的,可以自己下载。
吾乐吧软件站补充说明:
小编在使用过程中发现,本文提供的这个方法虽然可以实现多行查找、多长替换文本,但是多行替换的时候,会出现一个问题:你不能把多行文本替换为多行文本(除非你手动写正则)。为了解决这个问题,小编专门想出了另一个更加有效的方法,请大家移步查看《给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:下篇)》