A sample of the power of Vim.

this power … in vim you need to get…

:%s/X\([^=]*\)=”blankrow”/Xblankrow=”\1″/g

Problem statement:
I have a 1000+ line file, with this pattern: Xabc=”blankrow” …. that I need to change to: Xblankrow=”abc

see the swap?

note: abc is dynamic and viariable and unknown with the exception that I know it follows the syntactical rules of being a DOM attribute

breakdown on that vim command:

  :             start the command line editor mode of vi.
  %             line range selection, from line 0 thru to end of file (also all ranges 
                       here like: 1,100 or .,$ or 'a,'b )
  /             begins the regular expression pattern matching block.
  X             a literal in my sample, eg: "X"
  \             for the next character to follow, remove special meaning (or treat next 
                       character with special meaning).
  (             since preceded by a \, starts the left end of a token block.
  [             begins a set
  ^             reverts the set logic to the NOT set.
  =             is a literal in my example, eg: "="
  ]             closes the definition of the character set, all told [^=] reads as match 
                       any character except an "="
  *             match 0 or many times, the thing left of me, in this example a "set" of 
                any character except an "="
  \             for the next character to follow, remove special meaning (or treat next 
                       character with special meaning).
  )             since preceded by a \, ends the right end of a token block.
  ="blankrow"   are literals in my example, no magic here.
  /             ends the pattern matching block, starts the pattern replace block.
  Xblankrow="   are literals in my example, no magic here.
  \             for the next character to follow, remove special meaning (or treat next 
                       character with special meaning).
  1             since preceded by a \, this defines the "1"st token set matched on the 
                       left side of the pattern in between the () characters.
  /             end the pattern replace block.
  g             perform this search/replace operation on all occurrences of the pattern on 
                       the LINE (don't confuse this with line range)

Leave a Reply