Search code examples
notepad++

How to add comment out multiliple lines in multiple files?


I am trying to comment out an Azure DevOps pipeline stage in multiple yaml files. I tried using

Find in Files

in Notepad++, but unsuccessful.

I am trying to comment out the following stage.

- stage: "check_code"
    jobs:
      - job: "check_code"
        pool:
          vmImage: 'windows-latest'
        steps:
          - template: check_code.yml@templates
            parameters:
              projectName: '$(projectName)'

This is what I am trying to acheive.

# - stage: "check_code"
# jobs:
  # - job: "check_code"
    # pool:
      # vmImage: 'windows-latest'
    # steps:
      # - template: check_code.yml@templates
        # parameters:
          # projectName: '$(projectName)'

When I use notepad++, I can copy the lines in the Find what search box but unable to copy the commented out multiple lines in Replace with box.

enter image description here

Any suggestion on how can I replace it?


Solution

    • Ctrl+H
    • Find what: ^(?:(- stage: "check_code"\R)|\G(\h*)(?!-stage)(.+\R))
    • Replace with: $2# $1$3
    • TICK Match case
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    ^                       # beginning of line
        (?:                     # non capture group
            (                       # group 1
                - stage: "check_code"   # literally
                \R                      # any kind of linebreak
            )                       # end group 1
          |                       # OR
            \G                      # restart from last match position
            (\h*)                   # group 2, 0 or more horizontal spaces
            (?!-stage)              # negative lookahead, make sure we haven't -stage after
            (                       # group 3
                .+                      # 1 or more any character
                \R                      # any kind of linebreak
            )                       # end group 3
        )                       # end group
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here