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

所属分类: 软件教程 / 编程开发 阅读数: 247
收藏 0 赞 0 分享
小编长期都在使用Visual Studio这个全球最强大的IDE(没有之一),但是有些时候,往往需要查找、或者是替换多行文本,这个时候,对于VS来说可能有点压力了,因为默认的替换只能支持单行文本(虽然宏里面的FindLine是可以支持多行查找的,但是不能多行替换,稍后会详细说明下)。

给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:上篇)

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

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

给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:上篇)

6. 关闭宏IDE

给 Visual Studio 2010 增加多行查找与多行替换功能(VS跨行查找替换:上篇)

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

吾乐吧软件站补充说明:

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

更多精彩内容其他人还在看

精易模块新手入门图文教程

精易模块是最好用的开源易模块,全中文命令,多个常用类功能,采用windows Api+核心命令打造,性能强大、使用简单、功能免费、免费开源,这篇文章主要介绍了精易模块新手入门图文教程,需要的朋友可以参考下
收藏 0 赞 0 分享

scratch3.0怎么制作会变色的鹦鹉动画?

scratch3.0怎么制作会变色的鹦鹉动画?scratch3.0中想要制作一个会变色的鹦鹉,该怎么制作这个效果呢?下面我们就来看看详细的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

windows下jdk安装图解(覆盖安装报错)

这篇文章主要介绍了windows下jdk安装图解,覆盖安装报错,第一次安装和第二次安装区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Java配置 JDK开发环境搭建及环境变量配置详细图文教程

这篇文章主要介绍了Java配置 JDK开发环境搭建及环境变量配置详细图文教程,需要的朋友可以参考下
收藏 0 赞 0 分享

IntelliJ IDEA搭建Android开发环境图文详解

这篇文章主要介绍了IntelliJ IDEA搭建Android开发环境图文详解,需要的朋友可以参考下
收藏 0 赞 0 分享

visual studio 2019的安装以及使用方法

这篇文章主要介绍了visual studio 2019的安装以及使用图文方法,一款专为帮助程序设计人员更好,设计更优质程序开发的功能强大,需要的朋友可以参考下
收藏 0 赞 0 分享

基于IntelliJ IDEA 13搭建Android集成开发环境(图文教程)

使用IntelliJ IDEA搭建Android集成开发环境,但是感觉不详细,所以打算自己整理一个详细的图文教程,希望能对新手(包括自己)有所帮助,需要的朋友可以参考下
收藏 0 赞 0 分享

代码自动生成工具ASP.NET Maker 2020安装及激活教程(附注册机下载)

ASP.NET Maker 2020如何激活?ASP.NET Maker 2020一款功能强大的自动化代码生成器,下文中详细的介绍了本软件的安装及激活教程,另附上注册机下载,感兴趣的朋友不妨阅读下文内容,参考一下吧
收藏 0 赞 0 分享

python运行环境搭建和pycharm的安装配置及汉化(零基础小白版)

写这篇文章主要是介绍一下python的环境搭建和pycharm的安装配置,适合零基础的同学观看。这篇文章你会学到python的环境搭建和python比较好用的IDE pycharm的安装与基础配置
收藏 0 赞 0 分享

IntelliJ WebStorm 2020.3.3 最新激活教程 附汉化补丁安装教程

今天脚本小编给大家分享的是IntelliJ WebStorm 2020.3.3最新激活补丁和汉化补丁的安装激活教程,此款软件的激活比较麻烦,每个版本激活方法都不一样,所以小编就给大家分享了详细的安装激活教程,此教程是小编一步一步安装得来,所以真实有效,大家放心按照步骤安装即可
收藏 0 赞 0 分享
查看更多