为Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换)

小编长期都在使用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. ’1. 在 vs.net 中 打开 Macros Explorer(宏 资源管理器) ,在 工具–宏–宏资源管理器。
  2. ’2. 在 宏资源管理器 的 MyMacros 中新建一个宏 MultilineSearch
  3. ’3. 双击 MultilineSearch ,则显出 宏的IDE和生成的新宏的vb代码
  4. ’4. 把 新宏的vb代码内容都删除,把如下代码拷进去
  5. ’5. 将 System.Drawing.dll 加入 宏工程 的引用
  6. ’6. 关闭宏IDE
  7. Imports EnvDTE
  8. Imports System.Diagnostics
  9. Public Module MultilineSearch
  10. Sub MultilineSearchReplace()
  11. Dim sf As New MultilineSearchForm
  12. sf.ShowDialog()
  13. If sf.result <> FindReplaceKind.none Then
  14. ‘ temporarily disable Tools - Options -
  15. ‘ Environment - Documents - Initialize Find text from editor
  16. Dim oldFindInit As Boolean
  17. Try
  18. Dim props As EnvDTE.Properties
  19. props = DTE.Properties(“Environment”, “Documents”)
  20. Dim prop As EnvDTE.Property = props.Item(“FindReplaceInitializeFromEditor”)
  21. oldFindInit = prop.Value
  22. prop.Value = False
  23. Catch ex As System.Exception
  24. End Try
  25. DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
  26. DTE.Find.FindWhat = sf.findText
  27. DTE.Find.ReplaceWith = sf.replaceText
  28. Select Case sf.result
  29. Case FindReplaceKind.find
  30. DTE.ExecuteCommand(“Edit.Find”)
  31. Case FindReplaceKind.findInFiles
  32. DTE.ExecuteCommand(“Edit.FindinFiles”)
  33. Case FindReplaceKind.replace
  34. DTE.ExecuteCommand(“Edit.Replace”)
  35. Case FindReplaceKind.replaceInFiles
  36. DTE.ExecuteCommand(“Edit.ReplaceinFiles”)
  37. Case Else
  38. End Select
  39. ‘ restore Tools - Options -
  40. ‘ Environment - Documents - Initialize Find text from editor
  41. Try
  42. Dim props As EnvDTE.Properties
  43. props = DTE.Properties(“Environment”, “Documents”)
  44. Dim prop As EnvDTE.Property = props.Item(“FindReplaceInitializeFromEditor”)
  45. prop.Value = oldFindInit
  46. Catch ex As System.Exception
  47. End Try
  48. End If
  49. End Sub
  50. End Module
  51. ”’<summary>Types of find/replace operations.</summary>
  52. Public Enum FindReplaceKind
  53. ”’<summary>Find</summary>
  54. find
  55. ”’<summary>Find In Files</summary>
  56. findInFiles
  57. ”’<summary>Replace</summary>
  58. replace
  59. ”’<summary>Replace in Files</summary>
  60. replaceInFiles
  61. ”’<summary>None. Cancel was pressed.</summary>
  62. none
  63. End Enum
  64. Public Class MultilineSearchForm
  65. Inherits System.Windows.Forms.Form
  66. #Region “ Windows Form Designer generated code ”
  67. Public Sub New()
  68. MyBase.New()
  69. ‘This call is required by the Windows Form Designer.
  70. InitializeComponent()
  71. ‘Add any initialization after the InitializeComponent() call
  72. End Sub
  73. ‘Form overrides dispose to clean up the component list.
  74. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  75. If disposing Then
  76. If Not (components Is Nothing) Then
  77. components.Dispose()
  78. End If
  79. End If
  80. MyBase.Dispose(disposing)
  81. End Sub
  82. ‘Required by the Windows Form Designer
  83. Private components As System.ComponentModel.IContainer
  84. ‘NOTE: The following procedure is required by the Windows Form Designer
  85. ‘It can be modified using the Windows Form Designer.
  86. ‘Do not modify it using the code editor.
  87. Friend WithEvents FindBox As System.Windows.Forms.TextBox
  88. Friend WithEvents Label1 As System.Windows.Forms.Label
  89. Friend WithEvents Label2 As System.Windows.Forms.Label
  90. Friend WithEvents ReplaceBox As System.Windows.Forms.TextBox
  91. Friend WithEvents FindBtn As System.Windows.Forms.Button
  92. Friend WithEvents FindInFilesBtn As System.Windows.Forms.Button
  93. Friend WithEvents ReplaceBtn As System.Windows.Forms.Button
  94. Friend WithEvents ReplaceInFilesBtn As System.Windows.Forms.Button
  95. Friend WithEvents CancelBtn As System.Windows.Forms.Button
  96. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  97. Me.FindBox = New System.Windows.Forms.TextBox
  98. Me.Label1 = New System.Windows.Forms.Label
  99. Me.Label2 = New System.Windows.Forms.Label
  100. Me.ReplaceBox = New System.Windows.Forms.TextBox
  101. Me.FindBtn = New System.Windows.Forms.Button
  102. Me.FindInFilesBtn = New System.Windows.Forms.Button
  103. Me.ReplaceBtn = New System.Windows.Forms.Button
  104. Me.ReplaceInFilesBtn = New System.Windows.Forms.Button
  105. Me.CancelBtn = New System.Windows.Forms.Button
  106. Me.SuspendLayout()
  107. ‘FindBox
  108. Me.FindBox.Location = New System.Drawing.Point(16, 24)
  109. Me.FindBox.Multiline = True
  110. Me.FindBox.Name = “FindBox”
  111. Me.FindBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
  112. Me.FindBox.Size = New System.Drawing.Size(400, 80)
  113. Me.FindBox.TabIndex = 0
  114. Me.FindBox.Text = “”
  115. ‘Label1
  116. Me.Label1.Location = New System.Drawing.Point(16, 8)
  117. Me.Label1.Name = “Label1″
  118. Me.Label1.Size = New System.Drawing.Size(160, 16)
  119. Me.Label1.TabIndex = 2
  120. Me.Label1.Text = “查找内容:”
  121. ‘Label2
  122. Me.Label2.Location = New System.Drawing.Point(16, 112)
  123. Me.Label2.Name = “Label2″
  124. Me.Label2.Size = New System.Drawing.Size(160, 16)
  125. Me.Label2.TabIndex = 4
  126. Me.Label2.Text = “替换为:”
  127. ‘ReplaceBox
  128. Me.ReplaceBox.Location = New System.Drawing.Point(16, 128)
  129. Me.ReplaceBox.Multiline = True
  130. Me.ReplaceBox.Name = “ReplaceBox”
  131. Me.ReplaceBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
  132. Me.ReplaceBox.Size = New System.Drawing.Size(400, 80)
  133. Me.ReplaceBox.TabIndex = 3
  134. Me.ReplaceBox.Text = “”
  135. ‘FindBtn
  136. Me.FindBtn.Location = New System.Drawing.Point(16, 232)
  137. Me.FindBtn.Name = “FindBtn”
  138. Me.FindBtn.Size = New System.Drawing.Size(80, 24)
  139. Me.FindBtn.TabIndex = 5
  140. Me.FindBtn.Text = “查找”
  141. ‘FindInFilesBtn
  142. Me.FindInFilesBtn.Location = New System.Drawing.Point(104, 232)
  143. Me.FindInFilesBtn.Name = “FindInFilesBtn”
  144. Me.FindInFilesBtn.Size = New System.Drawing.Size(96, 24)
  145. Me.FindInFilesBtn.TabIndex = 6
  146. Me.FindInFilesBtn.Text = “在文件中查找”
  147. ‘ReplaceBtn
  148. Me.ReplaceBtn.Location = New System.Drawing.Point(216, 232)
  149. Me.ReplaceBtn.Name = “ReplaceBtn”
  150. Me.ReplaceBtn.Size = New System.Drawing.Size(80, 24)
  151. Me.ReplaceBtn.TabIndex = 7
  152. Me.ReplaceBtn.Text = “替换”
  153. ‘ReplaceInFilesBtn
  154. Me.ReplaceInFilesBtn.Location = New System.Drawing.Point(304, 232)
  155. Me.ReplaceInFilesBtn.Name = “ReplaceInFilesBtn”
  156. Me.ReplaceInFilesBtn.Size = New System.Drawing.Size(112, 24)
  157. Me.ReplaceInFilesBtn.TabIndex = 8
  158. Me.ReplaceInFilesBtn.Text = “在文件中替换”
  159. ‘CancelBtn
  160. Me.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel
  161. Me.CancelBtn.Location = New System.Drawing.Point(168, 272)
  162. Me.CancelBtn.Name = “CancelBtn”
  163. Me.CancelBtn.Size = New System.Drawing.Size(80, 24)
  164. Me.CancelBtn.TabIndex = 9
  165. Me.CancelBtn.Text = “取消”
  166. ‘MultilineSearchForm
  167. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  168. Me.CancelButton = Me.CancelBtn
  169. Me.ClientSize = New System.Drawing.Size(432, 310)
  170. Me.Controls.Add(Me.CancelBtn)
  171. Me.Controls.Add(Me.ReplaceInFilesBtn)
  172. Me.Controls.Add(Me.ReplaceBtn)
  173. Me.Controls.Add(Me.FindInFilesBtn)
  174. Me.Controls.Add(Me.FindBtn)
  175. Me.Controls.Add(Me.Label2)
  176. Me.Controls.Add(Me.ReplaceBox)
  177. Me.Controls.Add(Me.Label1)
  178. Me.Controls.Add(Me.FindBox)
  179. Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow
  180. Me.Name = “MultilineSearchForm”
  181. Me.Text = “多行查找与替换 - www.wuleba.com”
  182. Me.ResumeLayout(False)
  183. End Sub
  184. #End Region
  185. #Region “Properties”
  186. Private m_result As FindReplaceKind = FindReplaceKind.none
  187. ”’<summary>Gets result button from this dialog.</summary>
  188. ”’<value>The value specifying which button was pressed.</value>
  189. Public ReadOnly Property result() As FindReplaceKind
  190. Get
  191. Return m_result
  192. End Get
  193. End Property
  194. Private m_findText As String
  195. ”’<summary>Gets escaped multiline text to be searched.</summary>
  196. ”’<value></value>
  197. Public ReadOnly Property findText() As String
  198. Get
  199. Return m_findText
  200. End Get
  201. End Property
  202. Private m_replaceText As String
  203. ”’<summary>Gets escaped multiline replace text.</summary>
  204. ”’<value></value>
  205. Public ReadOnly Property replaceText() As String
  206. Get
  207. Return m_replaceText
  208. End Get
  209. End Property
  210. #End Region
  211. ”’<summary>Transforms the text to regular expression syntax.</summary>
  212. ”’<param name=”original”>Original text.</param>
  213. ”’<returns>Text with escaped regex characters.</returns>
  214. Private Function escapeRegEx(ByVal original As String) As String
  215. Dim specialChars() As Char = “/.*+^___FCKpd___0gt;<[]|{}:@#()~”.ToCharArray
  216. Dim c As Char
  217. For Each c In specialChars
  218. original = original.Replace(c.ToString, “/” & c.ToString)
  219. Next
  220. original = original.Replace(vbCrLf, “/n”)
  221. Return original
  222. End Function
  223. Private Sub MultilineSearchForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  224. Try
  225. Me.Activate()
  226. Catch ex As System.Exception
  227. End Try
  228. End Sub
  229. Private Sub CancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
  230. Try
  231. m_result = FindReplaceKind.none
  232. Me.Close()
  233. Catch ex As System.Exception
  234. End Try
  235. End Sub
  236. Private Sub FindBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindBtn.Click
  237. Try
  238. m_findText = escapeRegEx(Me.FindBox.Text)
  239. m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
  240. m_result = FindReplaceKind.find
  241. Me.Close()
  242. Catch ex As System.Exception
  243. End Try
  244. End Sub
  245. Private Sub FindInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindInFilesBtn.Click
  246. Try
  247. m_findText = escapeRegEx(Me.FindBox.Text)
  248. m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
  249. m_result = FindReplaceKind.findInFiles
  250. Me.Close()
  251. Catch ex As System.Exception
  252. End Try
  253. End Sub
  254. Private Sub ReplaceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceBtn.Click
  255. Try
  256. m_findText = escapeRegEx(Me.FindBox.Text)
  257. m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
  258. m_result = FindReplaceKind.replace
  259. Me.Close()
  260. Catch ex As System.Exception
  261. End Try
  262. End Sub
  263. Private Sub ReplaceInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceInFilesBtn.Click
  264. Try
  265. m_findText = escapeRegEx(Me.FindBox.Text)
  266. m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
  267. m_result = FindReplaceKind.replaceInFiles
  268. Me.Close()
  269. Catch ex As System.Exception
  270. End Try
  271. End Sub
  272. End Class

5. 将 System.Drawing.dll 加入 宏工程 的引用

6. 关闭宏IDE

操作完上面的6个步骤之后,你的VS2012就多了个多行搜索和替换文本的工具,效果图就是最上面的那个图。为了更方便大家学习,小牛打包了上面的这个vb代码,需要的,可以自己下载。

吾乐吧软件站补充说明:

小编在使用过程中发现,本文提供的这个方法虽然可以实现多行查找、多长替换文本,但是多行替换的时候,会出现一个问题:你不能把多行文本替换为多行文本(除非你手动写正则)。为了解决这个问题,小编专门想出了另一个更加有效的方法,请大家移步查看《给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:下篇)》

(0)

相关推荐

  • 安装MSDN后Visual Studio 2010编辑时报错

    装了个VS2010,安装了2008的MSDN,结果问题就出现了: 只要在VS2010源码编辑器中输入一个字符,它就报道错误了 "visual studio 2010 遇到了异常,可能是由某个扩展导致的 ...

  • Visual Studio 2010 安装 Mono for Android 的图文教程

    安装 Mono for Android for Visual Studio 2010 需要下面4个步骤: 安装 JDK安装 Android SDK配置模拟器安装 Mono for Android Vi ...

  • Win10系统下怎样使用visual studio 2010新建win32项目?

    Visual Studio可以称得上是目前最流行的Windows平台应用程序开发环境.一位windows10系统用户反馈自己因为工作需求,想要使用visual studio 2010新建win32项目 ...

  • visual studio 2010怎么创建c项目?

    本文旨在介绍Microsoft Visual Studio 2010如何创建C项目,并编写一个简单的测试程序,为C的初级开发者提供帮助. 1.首先,打开Microsoft Visual Studio ...

  • Visual Studio 2010 编写一个控制台程序

    在VS2010平台,开发一个控制台Hello World的应用程序. 操作方法 01 打开Visual Studio 2010新建一个项目.选择Win32项目. 02 点击下一步后,选择控制台应用程序 ...

  • visual studio 2010教程

    最近小编收到很多问题,其中一个就是下面小编为大家整理一下关于visual studio 2010教程的步骤,希望这些方法能够帮助到大家. 操作方法 01 首先,启动visual studio 2010 ...

  • 如何完全卸载Visual Studio 2010

    Microsoft Visual Studio 2010  是微软的一个综合性的应用程序生命周期管理工具套件,可供团队用于确保从设计到部署的整个过程都能取得较高质量的结果.无论是创建新的解决方案,还是 ...

  • 如何用visual studio 2010 写c++程序

    今天,小编就带大家演示一下用visual studio 2010 写c++程序,因为现在的vs升级成了2010的版本,有的朋友就不会用了.其实和vs 08 是类似的,下面就让我们来看看吧. 操作方法 ...

  • visual studio 2010 旗舰版简单安装教程和下载

    Visual Studio是微软公司推出的开发环境.是目前最流行的Windows平台应用程序开发环境.本文介绍了visual studio 2010 旗舰版的简单安装方法.关键在于更改序列号.最后附上 ...