為Visual Studio 2010 增加多行查找與多行替換功能(VS跨行查找替換)
給 Visual Studio 2010 增加多行查找與多行替換功能(VS跨行查找替換:上篇)

這個(gè)圖是增加多行查找與多行替換功能的效果圖,還不錯(cuò)吧!
接下來,等小牛來介紹一下如何添加多行查找與多行替換功能?
只要幾個(gè)步驟就可加入功能。
1. 在 VS 中打開 Macros Explorer(宏 資源管理器) ,在 工具–宏–宏資源管理器(或者快捷鍵Alt+F8)。
2. 在 宏資源管理器 的 MyMacros 中新建一個(gè)宏 MultilineSearch
3. 雙擊 MultilineSearch ,則顯出 宏的IDE和生成的新宏的vb代碼
4. 把 新宏的vb代碼內(nèi)容都刪除,把如下代碼拷進(jìn)去
- ’1. 在 vs.net 中 打開 Macros Explorer(宏 資源管理器) ,在 工具–宏–宏資源管理器。
- ’2. 在 宏資源管理器 的 MyMacros 中新建一個(gè)宏 MultilineSearch
- ’3. 雙擊 MultilineSearch ,則顯出 宏的IDE和生成的新宏的vb代碼
- ’4. 把 新宏的vb代碼內(nèi)容都刪除,把如下代碼拷進(jìn)去
- ’5. 將 System.Drawing.dll 加入 宏工程 的引用
- ’6. 關(guān)閉宏IDE
- Imports EnvDTE
- Imports System.Diagnostics
- Public Module MultilineSearch
- Sub MultilineSearchReplace()
- Dim sf As New MultilineSearchForm
- sf.ShowDialog()
- If sf.result <> FindReplaceKind.none Then
- ‘ temporarily disable Tools - Options -
- ‘ Environment - Documents - Initialize Find text from editor
- Dim oldFindInit As Boolean
- Try
- Dim props As EnvDTE.Properties
- props = DTE.Properties(“Environment”, “Documents”)
- Dim prop As EnvDTE.Property = props.Item(“FindReplaceInitializeFromEditor”)
- oldFindInit = prop.Value
- prop.Value = False
- Catch ex As System.Exception
- End Try
- DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
- DTE.Find.FindWhat = sf.findText
- DTE.Find.ReplaceWith = sf.replaceText
- Select Case sf.result
- Case FindReplaceKind.find
- DTE.ExecuteCommand(“Edit.Find”)
- Case FindReplaceKind.findInFiles
- DTE.ExecuteCommand(“Edit.FindinFiles”)
- Case FindReplaceKind.replace
- DTE.ExecuteCommand(“Edit.Replace”)
- Case FindReplaceKind.replaceInFiles
- DTE.ExecuteCommand(“Edit.ReplaceinFiles”)
- Case Else
- End Select
- ‘ restore Tools - Options -
- ‘ Environment - Documents - Initialize Find text from editor
- Try
- Dim props As EnvDTE.Properties
- props = DTE.Properties(“Environment”, “Documents”)
- Dim prop As EnvDTE.Property = props.Item(“FindReplaceInitializeFromEditor”)
- prop.Value = oldFindInit
- Catch ex As System.Exception
- End Try
- End If
- End Sub
- End Module
- ”’<summary>Types of find/replace operations.</summary>
- Public Enum FindReplaceKind
- ”’<summary>Find</summary>
- find
- ”’<summary>Find In Files</summary>
- findInFiles
- ”’<summary>Replace</summary>
- replace
- ”’<summary>Replace in Files</summary>
- replaceInFiles
- ”’<summary>None. Cancel was pressed.</summary>
- none
- End Enum
- Public Class MultilineSearchForm
- Inherits System.Windows.Forms.Form
- #Region “ Windows Form Designer generated code ”
- Public Sub New()
- MyBase.New()
- ‘This call is required by the Windows Form Designer.
- InitializeComponent()
- ‘Add any initialization after the InitializeComponent() call
- End Sub
- ‘Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- ‘Required by the Windows Form Designer
- Private components As System.ComponentModel.IContainer
- ‘NOTE: The following procedure is required by the Windows Form Designer
- ‘It can be modified using the Windows Form Designer.
- ‘Do not modify it using the code editor.
- Friend WithEvents FindBox As System.Windows.Forms.TextBox
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents ReplaceBox As System.Windows.Forms.TextBox
- Friend WithEvents FindBtn As System.Windows.Forms.Button
- Friend WithEvents FindInFilesBtn As System.Windows.Forms.Button
- Friend WithEvents ReplaceBtn As System.Windows.Forms.Button
- Friend WithEvents ReplaceInFilesBtn As System.Windows.Forms.Button
- Friend WithEvents CancelBtn As System.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.FindBox = New System.Windows.Forms.TextBox
- Me.Label1 = New System.Windows.Forms.Label
- Me.Label2 = New System.Windows.Forms.Label
- Me.ReplaceBox = New System.Windows.Forms.TextBox
- Me.FindBtn = New System.Windows.Forms.Button
- Me.FindInFilesBtn = New System.Windows.Forms.Button
- Me.ReplaceBtn = New System.Windows.Forms.Button
- Me.ReplaceInFilesBtn = New System.Windows.Forms.Button
- Me.CancelBtn = New System.Windows.Forms.Button
- Me.SuspendLayout()
- ‘
- ‘FindBox
- ‘
- Me.FindBox.Location = New System.Drawing.Point(16, 24)
- Me.FindBox.Multiline = True
- Me.FindBox.Name = “FindBox”
- Me.FindBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.FindBox.Size = New System.Drawing.Size(400, 80)
- Me.FindBox.TabIndex = 0
- Me.FindBox.Text = “”
- ‘
- ‘Label1
- ‘
- Me.Label1.Location = New System.Drawing.Point(16, 8)
- Me.Label1.Name = “Label1″
- Me.Label1.Size = New System.Drawing.Size(160, 16)
- Me.Label1.TabIndex = 2
- Me.Label1.Text = “查找內(nèi)容:”
- ‘
- ‘Label2
- ‘
- Me.Label2.Location = New System.Drawing.Point(16, 112)
- Me.Label2.Name = “Label2″
- Me.Label2.Size = New System.Drawing.Size(160, 16)
- Me.Label2.TabIndex = 4
- Me.Label2.Text = “替換為:”
- ‘
- ‘ReplaceBox
- ‘
- Me.ReplaceBox.Location = New System.Drawing.Point(16, 128)
- Me.ReplaceBox.Multiline = True
- Me.ReplaceBox.Name = “ReplaceBox”
- Me.ReplaceBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.ReplaceBox.Size = New System.Drawing.Size(400, 80)
- Me.ReplaceBox.TabIndex = 3
- Me.ReplaceBox.Text = “”
- ‘
- ‘FindBtn
- ‘
- Me.FindBtn.Location = New System.Drawing.Point(16, 232)
- Me.FindBtn.Name = “FindBtn”
- Me.FindBtn.Size = New System.Drawing.Size(80, 24)
- Me.FindBtn.TabIndex = 5
- Me.FindBtn.Text = “查找”
- ‘
- ‘FindInFilesBtn
- ‘
- Me.FindInFilesBtn.Location = New System.Drawing.Point(104, 232)
- Me.FindInFilesBtn.Name = “FindInFilesBtn”
- Me.FindInFilesBtn.Size = New System.Drawing.Size(96, 24)
- Me.FindInFilesBtn.TabIndex = 6
- Me.FindInFilesBtn.Text = “在文件中查找”
- ‘
- ‘ReplaceBtn
- ‘
- Me.ReplaceBtn.Location = New System.Drawing.Point(216, 232)
- Me.ReplaceBtn.Name = “ReplaceBtn”
- Me.ReplaceBtn.Size = New System.Drawing.Size(80, 24)
- Me.ReplaceBtn.TabIndex = 7
- Me.ReplaceBtn.Text = “替換”
- ‘
- ‘ReplaceInFilesBtn
- ‘
- Me.ReplaceInFilesBtn.Location = New System.Drawing.Point(304, 232)
- Me.ReplaceInFilesBtn.Name = “ReplaceInFilesBtn”
- Me.ReplaceInFilesBtn.Size = New System.Drawing.Size(112, 24)
- Me.ReplaceInFilesBtn.TabIndex = 8
- Me.ReplaceInFilesBtn.Text = “在文件中替換”
- ‘
- ‘CancelBtn
- ‘
- Me.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel
- Me.CancelBtn.Location = New System.Drawing.Point(168, 272)
- Me.CancelBtn.Name = “CancelBtn”
- Me.CancelBtn.Size = New System.Drawing.Size(80, 24)
- Me.CancelBtn.TabIndex = 9
- Me.CancelBtn.Text = “取消”
- ‘
- ‘MultilineSearchForm
- ‘
- Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
- Me.CancelButton = Me.CancelBtn
- Me.ClientSize = New System.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)
- End Sub
- #End Region
- #Region “Properties”
- Private m_result As FindReplaceKind = FindReplaceKind.none
- ”’<summary>Gets result button from this dialog.</summary>
- ”’<value>The value specifying which button was pressed.</value>
- Public ReadOnly Property result() As FindReplaceKind
- Get
- Return m_result
- End Get
- End Property
- Private m_findText As String
- ”’<summary>Gets escaped multiline text to be searched.</summary>
- ”’<value></value>
- Public ReadOnly Property findText() As String
- Get
- Return m_findText
- End Get
- End Property
- Private m_replaceText As String
- ”’<summary>Gets escaped multiline replace text.</summary>
- ”’<value></value>
- Public ReadOnly Property replaceText() As String
- Get
- Return m_replaceText
- End Get
- End Property
- #End Region
- ”’<summary>Transforms the text to regular expression syntax.</summary>
- ”’<param name=”original”>Original text.</param>
- ”’<returns>Text with escaped regex characters.</returns>
- Private Function escapeRegEx(ByVal original As String) As String
- Dim specialChars() As Char = “\.*+^___FCKpd___0gt;<[]|{}:@#()~”.ToCharArray
- Dim c As Char
- For Each c In specialChars
- original = original.Replace(c.ToString, “\” & c.ToString)
- Next
- original = original.Replace(vbCrLf, “\n”)
- Return original
- End Function
- Private Sub MultilineSearchForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Try
- Me.Activate()
- Catch ex As System.Exception
- End Try
- End Sub
- Private Sub CancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
- Try
- m_result = FindReplaceKind.none
- Me.Close()
- Catch ex As System.Exception
- End Try
- End Sub
- Private Sub FindBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindBtn.Click
- Try
- m_findText = escapeRegEx(Me.FindBox.Text)
- m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
- m_result = FindReplaceKind.find
- Me.Close()
- Catch ex As System.Exception
- End Try
- End Sub
- Private Sub FindInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindInFilesBtn.Click
- Try
- m_findText = escapeRegEx(Me.FindBox.Text)
- m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
- m_result = FindReplaceKind.findInFiles
- Me.Close()
- Catch ex As System.Exception
- End Try
- End Sub
- Private Sub ReplaceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceBtn.Click
- Try
- m_findText = escapeRegEx(Me.FindBox.Text)
- m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
- m_result = FindReplaceKind.replace
- Me.Close()
- Catch ex As System.Exception
- End Try
- End Sub
- Private Sub ReplaceInFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceInFilesBtn.Click
- Try
- m_findText = escapeRegEx(Me.FindBox.Text)
- m_replaceText = escapeRegEx(Me.ReplaceBox.Text)
- m_result = FindReplaceKind.replaceInFiles
- Me.Close()
- Catch ex As System.Exception
- End Try
- End Sub
- End Class
5. 將 System.Drawing.dll 加入 宏工程 的引用

6. 關(guān)閉宏IDE

操作完上面的6個(gè)步驟之后,你的VS2012就多了個(gè)多行搜索和替換文本的工具,效果圖就是最上面的那個(gè)圖。為了更方便大家學(xué)習(xí),小牛打包了上面的這個(gè)vb代碼,需要的,可以自己下載。
吾樂吧軟件站補(bǔ)充說明:
小編在使用過程中發(fā)現(xiàn),本文提供的這個(gè)方法雖然可以實(shí)現(xiàn)多行查找、多長(zhǎng)替換文本,但是多行替換的時(shí)候,會(huì)出現(xiàn)一個(gè)問題:你不能把多行文本替換為多行文本(除非你手動(dòng)寫正則)。為了解決這個(gè)問題,小編專門想出了另一個(gè)更加有效的方法,請(qǐng)大家移步查看《給 Visual Studio 2010 增加多行查找與多行替換功能(VS跨行查找替換:下篇)》
相關(guān)文章

網(wǎng)絡(luò)工程師必備的6款配置文件編輯神器:替代Notepad++!
本期文章介紹了六種網(wǎng)工必備工具,這些工具是網(wǎng)絡(luò)工程師日常工作中必不可少的,也是那些立志成為網(wǎng)絡(luò)工程師的人應(yīng)該熟悉的2026-04-13
首個(gè)版本v1.111已上線! VS Code開啟周更狂飆模式
026年3月10日,微軟宣布將Visual Studio Code(VS Code)更新節(jié)奏從月更提速至周更,并發(fā)布首個(gè)周更版本v1.1112026-03-11
AI直接調(diào)試瀏覽器! VS Code重大更新發(fā)布
還在手動(dòng)打開瀏覽器、點(diǎn)擊按鈕、檢查控制臺(tái)報(bào)錯(cuò)?太累了!現(xiàn)在,最新的VS Code 1.110中AI可以直接調(diào)試瀏覽器2026-03-10
今天帶大家來共同學(xué)習(xí)VC++6.0無法正常運(yùn)行怎么辦?下面就是詳細(xì)完整的操作步驟,快跟小編一起來學(xué)習(xí)一下2026-02-27
vscode調(diào)試c代碼始終找不到exe可執(zhí)行文件的詳細(xì)及解決辦法
在 VS Code 中調(diào)試 C++ 項(xiàng)目時(shí),常見報(bào)錯(cuò):“無法啟動(dòng)調(diào)試會(huì)話:找不到可執(zhí)行文件”,這個(gè)問題問題通常是由于環(huán)境配置不當(dāng)、缺少編譯步驟、路徑設(shè)置錯(cuò)誤、或是launch.json2026-02-27
vscode入門! 小白都能看懂的VS Code保姆級(jí)級(jí)使用教程
相對(duì)于其它前端工具來說,VSCode顯得更加的流暢,今天就將我的學(xué)習(xí)經(jīng)歷和大家分享一下,希望可以幫助到有需要的人2026-02-27
2026年真正值得裝、用得上、不雞肋的 VSCode 插件,其實(shí)沒那么多——挑10個(gè)“神級(jí)”不難,但得是每天打開就用、關(guān)掉就難受的那種2026-02-10
VS Code 和 VSCodium怎么選? vscode 和 vscodium之間的區(qū)別
咱們今天就來聊聊 Visual Studio Code (VS Code) 和 VSCodium的相似與區(qū)別,詳細(xì)請(qǐng)看下文介紹2026-01-10
如何啟用.NET Framework3.5? 一鍵安裝net3.5解決軟件依賴問題
你是否曾在安裝某些軟件時(shí)遇到過“需要.NET Framework 3.5”的提示?實(shí),只需要幾步簡(jiǎn)單的操作,你就可以輕松解決這個(gè)問題2025-10-28
VS Code中Java擴(kuò)展代碼重構(gòu)功能的使用詳解
在軟件開發(fā)過程中,重構(gòu)是一個(gè)非常重要的環(huán)節(jié),重構(gòu)可以提高代碼的可讀性、可維護(hù)性和可擴(kuò)展性,使代碼更加健壯和易于理解,本文將介紹如何使用 VSCode 進(jìn)行 Java 代碼重構(gòu)2025-09-20





