Skip to content

Instantly share code, notes, and snippets.

@favrik
Created March 6, 2013 11:09
Show Gist options
  • Save favrik/5098603 to your computer and use it in GitHub Desktop.
Save favrik/5098603 to your computer and use it in GitHub Desktop.
Replace string with another string at same indentation level

Input

                  =r.text_field :first_name
                .control-group
                  =r.label :middle_name, "Middle Name", class: "control-label"

Output

                  .controls
                    =r.text_field :first_name
                .control-group
                  =r.label :middle_name, "Middle Name", class: "control-label"

VIM regex

:%s/\(^\s\+\)\(.*_field.*\)/\1\.controls\r\1 \2/g

Note: parenthesis in search regex have to be backwhacked in vim ( becomes \(.

  • :%s/ this starts the search command :P
  • \(^\s\+\) this finds the whitespace at the beginning of each line
  • \(.*_field.*\) this finds strings that have "_field" in them
  • \1\.controls\r\1 \2 the replace command with backreferences FTW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment